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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Script to shutdown remote PCs 1

Status
Not open for further replies.

MRSOLO

Technical User
Jan 28, 2003
31
IE
Hello
I am trying to set up a script that I can use to shutdown a number of PCs on the network each night.
I have got the following code from the Microsoft website:

Const SHUTDOWN = 1
strComputer = "f110-00000004"
Set objWMIService = GetObject("winmgmts: {(Shutdown)}" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Win32Shutdown(SHUTDOWN)
Next

However even though it is a script from MS apart from the PC name it gives an error on the third line. Set obj etc.

Can anyone help or even point me in the direction of a website with some good scripting examples.

Thanks
 
Which error ?
Have you WMI or WBEM installed ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
This is a script I have used for the rebooting of PC's to install Computer based Software Deployment via Active Directory. If you have only one OU for all your computers this may be quicker. The script reboots the PC's and writes a log file.

Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 200
objExplorer.Left = 0
objExplorer.Top = 0
Do While (objExplorer.Busy)
Wscript.Sleep 200
'DateInfo = DateInfo & Now & VbCrLf
Loop
'This part of the script goes through the OU and reboots the PC's

objExplorer.Visible = 1
objExplorer.Document.Body.InnerHTML = "Rebooting PC's in Organisational Unit. " _
& "This might take several minutes to complete."

Set objDictionary = CreateObject("Scripting.Dictionary")
i=0
Set objOU = GetObject("LDAP://OU=Accounting Computers, OU=Accounting OU, OU=Corporate Services OU, DC=xxx, DC=com")
objOU.Filter = Array("Computer")
For Each objComputer in objOU
objDictionary.Add i, objComputer.CN
i=i+1
Next
Const ForAppending = 8
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\TrimUpgrade\Logs\AccountingRebootLog.txt", ForAppending)
objFile.WriteLine "COMPUTER " & rounds & ": " & objDictionary.Item(objItem) & " No Reboot " & Now
objFile.Close


For Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)
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

objExplorer.Visible = 1
objExplorer.Document.Body.InnerHTML = "Rebooting " & objDictionary.Item(objItem) & " in Organisational Unit. " _
& " This might take several minutes to complete."


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\TrimUpgrade\Logs\AccountingRebootLog.txt", ForAppending)
objFile.WriteLine "COMPUTER " & rounds & ": " & objDictionary.Item(objItem) & " Rebooted " & Now
objFile.Close

next

Wscript.Sleep 200

objExplorer.Document.Body.InnerHTML = "All computers in Accounting OU rebooted and upgrading to Trim 5.1 in progress"
Wscript.Sleep 3000
Wscript.Quit

Frank Aldridge
IT Professional
New Zealand
 
Hi PHV
I am using Windows 2000 and all the reference sites tell me that WMI is automatically installed in W2K.
Can you please advise a way that I can confirm if WMI is definitely installed.
The error that comes up is "Unspecified error" (not a lot of use) but it is on the line:

Set objWMIService = GetObject("winmgmts: {(Shutdown)}" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Thanks


 
Have you tried this ?
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hello MRSOLO,

Your construction of the moniker string is unusual. Apart from what PHV sugguests, you do need to assert shutdown privileges, more so for a remote m/c. Hence, my proposal would be:
Code:
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(shutdown)}!\\" & strComputer & "\root\cimv2")
Besides, this is also used by namkiwi, though I've not looked into the detail of his.

regards - tsuji
 
Hi
Thanks a million for your help guys. When I made the change you suggested
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
strComputer & "\root\cimv2")
It worked first time.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top