Solved

Scheduled VBS script for Automatic Reboots with option to Cancel

Posted on 2014-09-10
5
474 Views
Last Modified: 2014-09-16
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
0
Comment
Question by:InformationSystems
  • 3
5 Comments
 
LVL 78

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 500 total points
Comment Utility
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
 

Author Comment

by:InformationSystems
Comment Utility
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
 
LVL 13

Expert Comment

by:Gabriel Clifton
Comment Utility
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
 

Accepted Solution

by:
InformationSystems earned 0 total points
Comment Utility
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
 

Author Closing Comment

by:InformationSystems
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now