I am trying to show a message box from PowerShell with yes and no buttons.
I can display a message box with an OK button:
[system.windows.forms.messagebox]::show("Hello, world!")
And I can create a variable $buttons with the buttons I want:
$buttons=[system.windows.forms.messageboxbuttons].yesno
And I can see that the Show() static method is overloaded and that one of the options is to give three parameters:
Show(String, String, MessageBoxButtons) Displays a message box with specified text, caption, and buttons.
So naturally(?) I decided to call this:
[system.windows.forms.messagebox]::show("Are you sure?","",$buttons)
And this results in an error:
Cannot find an overload for “Show” and the argument count: “3”.
But there IS an overload for “Show” that accepts three arguments!
What am I doing wrong?
(And can someone tell me why calling a method in PowerShell is usually done by using the dot syntax: object.method() but requires “::” for the MessageBox class? It’s confusing.)
Correct way of doing this can be
Notice “::” instead of “.” in the first line. YesNo value is defined staticly on System.Windows.Forms.Messageboxbuttons, so you must use “::” (static call) instead of “.”
Note that “[system.windows.forms.messageboxbuttons].yesno” is an attempt to call a “YesNo” property on an instance of System.Type, which does not exist and therefore result in a $null
Hope it helps !
Cédric
Edit —
Keith solution using an implicit cast made by powershell for the enum is more elegant.
It just does not work on PS V2 CTP 3 which I still use but work fine on RTM version.
The complete explication was worth giving, though…