Dialog box opening behind other windows

Hi everyone,

I have a powershell script that I am executing via a scheduled task to prompt users to reboot their machines every night.  The script, prompt, and reboot process work just fine now but I am having one last issue with this script.

When the scheduled task executes the dialog box does not pop up above all other windows.  Instead it just has a flashing powershell icon on the taskbar which then brings up the dialog box if you click on it.

Is there a way to ensure that this popup box is displayed on top of all windows (brought to the foreground)?  For reference, the current code is below:

# DEFINE SECONDS TO WAIT FOR RESPONSE
$seconds_to_wait=900
# DEFINE REBOOT POSTPONE TIME IF USER CLICKS NO
$rebootWaitTime = 7200

# OPEN POPUP WINDOW WITH YES/NO PROMPT
$pop = New-Object -ComObject WScript.Shell
$answer = $pop.Popup("Do you want to reboot now?  If you click 'No' your computer will reboot automatically in 2 hours.  If you do not respond to this message within 15 minutes, your computer will reboot automatically.  Please remember to save your work.",$seconds_to_wait,"Nightly Reboot",4)

# IF NO ANSWER AFTER $seconds_to_wait REBOOT
if ($answer -eq "-1")
    {
        shutdown /r /f
    }
# IF USER CLICKS NO, WAIT $rebootWaitTime THEN REBOOT
elseif ($answer -eq "7")
    {
        Start-Sleep -Seconds $rebootWaitTime
        shutdown /r /f
    }
# IF USER CLICKS YES, REBOOT IMMEDIATELY
elseif ($answer -eq "6")
    {
        shutdown /r /f
    }


  • Edited by Cosmos_85 Thursday, June 25, 2015 6:52 PM
June 25th, 2015 6:52pm

Thanks for the suggestion.  This is what I get when I tried it:

Cannot find an overload for "Popup" and the argument count: "6".
At C:\Reboot_Dialog.ps1:9 char:1
+ $answer = $pop.Popup("Do you want to reboot now?  If you click 'No' your compute ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest


  • Edited by Cosmos_85 Thursday, June 25, 2015 9:12 PM
Free Windows Admin Tool Kit Click here and download it now
June 25th, 2015 9:12pm

That is my fault, I thought you had less parameters. On your original, try changing 4 to be 4096
June 26th, 2015 8:16am

I am not sure how to do that with the object you are using.  But one possible alternative is to use the System.Windows.Forms.MessageBox object which has a method call that will do what you want.  The code would like something like this:

$msg = "Do you want to reboot now?  If you click 'No' your computer will reboot automatically in 2 hours.  If you do not respond to this message within 15 minutes, your computer will reboot automatically.  Please remember to save your work"

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$ans = [System.Windows.Forms.MessageBox]::Show($msg , "Nightly Reboot" , "YesNo", "Question", "Button1", "DefaultDesktopOnly")
        
if($ans -eq "Yes") {
    #do stuff
}
else {
    #do other stuff
}

<#
First parameter is message displayed in message box

Second parameter is title of message box

Third parameter indicates the buttons displayed on the message box
"OK" 
"OKCancel"
"AbortRetryIgnore"
"YesNoCancel"
"YesNo"
"RetryCancel"

Fourth parameter indicates the icon displayed:
"Asterisk"
"Error"
"Exclamation"
"Hand"
"Information"
"None"
"Question"
"Stop"
"Warning "

Fifth parameter indicates the default button (that initially has focus). This is required if the DefaultDesktopOnly parameter is used.

Sixth parameter is DefaultDesktopOny which forces message box on top.
#>

One caveat, when called with the DefaultDesktopOnly parameter, this message box will not display if the script is run in ISE.

By the way, I copied this from an older script and the LoadWithPartialName static method call used is now deprecated.  This should probably be rewritten using Add-Type -Assemblyname "System.Windows.Forms" but that is probably getting off topic.  

Free Windows Admin Tool Kit Click here and download it now
June 26th, 2015 9:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics