1.

Solve : VBS: Can it shut down a PC??

Answer»

Can VBScript shut down a PC?
Can it shut down all PCs on a LAN?Quote

Can VBScript shut down a PC?

Yes. This little snippet will shutdown the local computer.

Code: [Select]strComputer = "."
Set objWMIService = GetObject_
("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _
strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(1)
Next

Quote
Can it shut down all PCs on a LAN?

Only if the PC's are part of a domain. This little snippet will shutdown 3 computers on the network.

Code: [Select]arrComputer = Array("Computer1", "Computer2", "Computer3")

For Each strComputer In arrComputer
Set objWMIService = GetObject _
("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _
strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(1)
Next
Next

If none of these are helpful, check out PsTools, specifically PsExec. VBScript can launch a new process with the Run method.

Good luck.

Woah, thanks. The only VBS I EVER heard of it .vbs or VBScriptWhat?Quote from: DFND-jimjim1 on December 29, 2008, 07:39:08 AM
The only VBS I ever heard of it .vbs or VBScript

Can the second script above be used to restart the PC instead of shutdown...?I've never used WMI myself, but you might be able to change:
Code: [Select]For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(1)
Next

into:
Code: [Select]For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Restart(1)
Next

Or, it could require simply changing the parameter to Win32Shutdown from 1 to something else...

Yup, I'd imagine it's something like that.

I'm just starting with VBS... I don't think there is a restart method to the Win32_OperatingSystem class. Try using the REBOOT method.

Code: [Select]For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Reboot(2)
Next

Good luck. Thanks again, I'll test it tomorrow...Quote from: Sidewinder on December 31, 2008, 11:59:23 AM
I don't think there is a restart method to the Win32_OperatingSystem class. Try using the reboot method.

Code: [Select]For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Reboot(2)
Next

Good luck.

well, if this darn notepad had intellisense like the vb6 IDE...Quote from: BC_Programmer on December 31, 2008, 01:31:51 PM
well, if this darn notepad had intellisense like the vb6 IDE...

Excuses, excuses.... Quote from: kpac on January 01, 2009, 06:40:18 AM
Quote from: BC_Programmer on December 31, 2008, 01:31:51 PM
well, if this darn notepad had intellisense like the vb6 IDE...

Excuses, excuses....

I'm not sure the VB6 IDE would be helpful here. For the WMI CLASSES I've always used WbemTest which gets installed on systems with WMI.

The program itself is a quagmire, but if you're looking for classes with their methods and properties, this is the place to go.

Before getting involved with this utility, a stop at Don's is suggested. actually, I shouldn't of said Intellisense, but rather the BUILT in object browser. Besides, using WMI to shutdown windows via VB6 would be silly, since it would be far easier to make a simple API call to ShutdownWindowsEx().




Discussion

No Comment Found