Author Posts

April 1, 2016 at 12:34 am

Hi
I have been working on a PS script which contained a message box.
This all worked without issues when testing a few weeks ago, so I commented the message box part out of the script, while I was working on other things.
Now when I'm ready to test, I can't for the science of me get the message box working again.

I have stripped out the part below:

$MsgboxTitle = 'Title'
$MsgboxTxt = "Message"
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null

##This Works
#[System.Windows.Forms.MessageBox]::Show($MsgboxTxt, $MsgboxTitle, [Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Stop, [System.Windows.Forms.MessageBoxDefaultButton]::Button1)

##Neither of the two below works
#[System.Windows.Forms.MessageBox]::Show($MsgboxTxt, $MsgboxTitle, [Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Stop, [System.Windows.Forms.MessageBoxDefaultButton]::Button1, [System.Windows.Forms.MessageBoxOptions]::DefaultDesktopOnly)
#[System.Windows.Forms.MessageBox]::Show($MsgboxTxt, $MsgboxTitle, [Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Stop, [System.Windows.Forms.MessageBoxDefaultButton]::Button1, [System.Windows.Forms.MessageBoxOptions]::ServiceNotification)

When I run the snippet above with the line containing DefaultDesktopOnly or ServiceNotification, I immediately get and output on 'No' and no messagebox of course.
I have tried running this same snippet on Windows7/8.1/10 x64 and get the same results on them all.

The reason I am using this message box option, is that I have a service running as SYSTEM and it will run a PS script and display the messagebox to the user, which I need the users feedback(Yes or No).

I have no hair on my head left, because I have pulled them all out by now 🙂
Seriously, any help is much appreciated.
Thank you

Links:https://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxoptions%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

April 3, 2016 at 11:02 pm

Anyone?
Could you at least just try it out, if you get a MessageBox(what OS version and what version of .NET do you have)?

April 4, 2016 at 1:12 pm

Well the code appears to be correct. While it doesn't run when you click 'Play' in the ISE, if you save the script and then call the script from somewhere else, the message box does display. However, it does not seem to display when called from a PowerShell host.

Testing:

Saved this as msg.ps1

$MsgboxTitle = 'Title'
$MsgboxTxt = "Message"
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null

##This Works
#[System.Windows.Forms.MessageBox]::Show($MsgboxTxt, $MsgboxTitle, [Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Stop, [System.Windows.Forms.MessageBoxDefaultButton]::Button1)

##Neither of the two below works
[System.Windows.Forms.MessageBox]::Show($MsgboxTxt, $MsgboxTitle, [Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Stop, [System.Windows.Forms.MessageBoxDefaultButton]::Button1, [System.Windows.Forms.MessageBoxOptions]::DefaultDesktopOnly)
#[System.Windows.Forms.MessageBox]::Show($MsgboxTxt, $MsgboxTitle, [Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Stop, [System.Windows.Forms.MessageBoxDefaultButton]::Button1, [System.Windows.Forms.MessageBoxOptions]::ServiceNotification)

1. Ran in ISE- didn't work.

2. Ran .\msg.ps1 – didn't work.

3. From cmd prompt: powershell.exe F:\__temp\msg.ps1 – worked.

4. From PowerShell prompt: powershell.exe F:\__temp\msg.ps1 – worked

5. 'Compiled' powershell.exe F:\__temp\msg.ps1 as CallTest.exe with ps2exe and ran CallTest.exe – worked from cmd prompt, PowerShell prompt and by double clicking. However, Yes or No is displayed in the window depending which button you press.