Scheduled VBS script for Automatic Reboots with option to Cancel

I am trying to get the below .VBS script for Automatic Reboots to work properly as a scheduled task on Windows XP and Windows 7 PCs. The script works perfectly if I save it as a .VBS and just double-click, acting as expected.

Now i want to push out a scheduled task via Group Policy that would call this script at 2AM each night in order to restart the computers. If someone is logged on, it should prompt them if they want to cancel the reboot with a Yes/No option. However, when this is called via scheduled task it does not display that prompt and the end user would have no option to cancel.

The only way the prompt shows up is if the Task is set to run as the user who is currently logged onto the computer AND the "Run only when user is logged on" is checked instead of of "Run whether user is logged on or not."
Pushing out through Group Policy, we would want the user set to a Domain Admin, and we want that "or not" option to be the default, because we want the PCs to reboot even when no one is logged on.

Any thoughts or ideas are appreciated.. Thanks!



Option Explicit
Dim objShell, intShutdown
Dim strShutdown, strAbort

' -r = reboot, -t 300 = 5 minutes, -f = force programs to close
strShutdown = "shutdown.exe -r -t 302 -f"
set objShell = CreateObject("WScript.Shell")
objShell.Run strShutdown, 0, false

'go to sleep so message box appears on top
WScript.Sleep 2000

' Input Box to abort shutdown
intShutdown = (MsgBox("Computer will reboot in 5 minutes. Do you want to cancel computer reboot?",vbYesNo+vbExclamation+vbApplicationModal,"Cancel Reboot"))
If intShutdown = vbYes Then
  ' Abort Shutdown
  strAbort = "shutdown.exe -a"
  set objShell = CreateObject("WScript.Shell")
  objShell.Run strAbort, 0, false
End if

Wscript.Quit
InformationSystemsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
my way of thinking is that you will need 2 scripts
1 for when a user is logged on and another when no user is logged on. If a user is not logged on then a reboot with a timeout of 0 seconds is sufficient.
0
InformationSystemsAuthor Commented:
Okay, I like where that's headed, but still leaves one problem..
I could have one script that runs whether or not anyone is logged on calling a timed shutdown.
Then the second script would only run when someone is logged on prompting them to cancel. The problem is still that I can't get a scheduled task to display a message box promting them to cancel, unless the the task has that specific user assigned to run it. I don't know of a way to assign that to each computer, especially shared computers where a number of different users could be on it at that given time.
0
Gabriel CliftonNet AdminCommented:
I do know with batch script you can get current logged in user with
for /f "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq explorer.exe" /FO LIST /V') do if /i "%%a %%b"=="User Name:" set _currdomain_user=%%c
for /f "TOKENS=1,2 DELIMS=\" %%a in ("%_currdomain_user%") do set _currdomain=%%a & set _curruser=%%b
echo %_currdomain% \ %_curruser%

Open in new window

now how to translate that into VBS to make it interact with a message, I do not know.
0
InformationSystemsAuthor Commented:
I think I may be all set.. pending some more testing. I went with having two sepearate Scheduled Tasks pushed through Group Policy, one being Computer Configured and the other User Configured.
 
One is a Computer Configuration to run shutdown with arguments -r -f -t 240 daily at 2AM. This way it tries to reboot no matter if someone is logged on or not, just as long as the PC is on.
 
Also at 2AM, another Scheduled Task is run as User Configed to execute the below script. This allows it to run in the context of whoever is logged on, thus allowing the MsgBox to display on the screen. It delays 5 seconds to try and get the MsgBox to display in front of the reboot notification (which works hit or miss..) Thanks guys!

 

Option Explicit
 Dim objShell, intShutdown
 Dim strShutdown, strAbort

 'go to sleep so message box appears on top
 WScript.Sleep 5000

 ' Input Box to abort shutdown
 intShutdown = (MsgBox("Computer will reboot in 4 minutes. Do you want to cancel computer reboot?",vbYesNo+vbExclamation+vbApplicationModal,"Cancel Reboot"))
 If intShutdown = vbYes Then
 ' Abort Shutdown
 strAbort = "shutdown.exe -a"
 set objShell = CreateObject("WScript.Shell")
 objShell.Run strAbort, 0, false
 End if

 Wscript.Quit

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InformationSystemsAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.