Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBscript to Restart Computer

Status
Not open for further replies.

jorty

IS-IT--Management
Nov 29, 2007
1
0
0
US
Can someone show me a script that would ask the user (either by typing or clicking a button) if they wish to restart their computer, if they answer YES then I would like the computer to restart. If they answer NO I would like a message to be displayed "You must Restart before Changes will take effect!". I have little to no experience with writing VBscripts so trying to write something like this is over my head.

Thanks
 
Take a look at this Microsoft page for an example script to restart a computer:



You could wrap this into a script with a popup message, something like this:

host="."
set oshell = wscript.createobject("wscript.shell")

mess = oshell.popup("Restart now?",0,"System Restart Required",4)
'if user clicks Yes then reboot otherwise display message
if mess=6 then
Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & host & "\root\cimv2")
Set colOS = objWMI.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOS in colOS
objOS.Reboot()
Next
else
msgbox "You must restart before changes take effect"
end if
 
Code:
strComputer = "it-nb2"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Reboot()
Next

I use this one to reboot servers in windows scheduled tasks
Just change "it-nb2" to the computer name to reboot

I have one from Microsoft also, but its much more complex, gives you more options and extensive error control.
I'll put that up if you want, although its a bit messy.

Bill
 
Take a peak at my version that prompts you for the machine to be restarted.

thread329-1062279

Or the version to reboot a list of computers:

thread329-954265

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top