Home / Software / Computer programming / VBS: Can it shut down a PC?
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: VBS: Can it shut down a PC?  (Read 5352 times)
kpac
Topic Starter
Web moderator
Hacker



Thanked: 180
Posts: 5,874

Certifications: List
Computer: Specs
Experience: Expert
OS: Windows 7
kpac®

1 1 1
« on: December 27, 2008, 08:47:08 AM »

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

Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #1 on: December 27, 2008, 10:06:47 AM »

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.  8)
   
   
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
kpac
Topic Starter
Web moderator
Hacker



Thanked: 180
Posts: 5,874

Certifications: List
Computer: Specs
Experience: Expert
OS: Windows 7
kpac®

1 1 1
« Reply #2 on: December 27, 2008, 10:09:53 AM »

Woah, thanks. :)
IP logged

DFND-jimjim1
Beginner



Posts: 106

What would you do if you knew you could not fail?

Darkroom LTD
« Reply #3 on: December 29, 2008, 07:39:08 AM »

The only VBS I ever heard of it .vbs or VBScript
IP logged

kpac
Topic Starter
Web moderator
Hacker



Thanked: 180
Posts: 5,874

Certifications: List
Computer: Specs
Experience: Expert
OS: Windows 7
kpac®

1 1 1
« Reply #4 on: December 29, 2008, 08:18:12 AM »

What?
IP logged

BC_Programmer
Mastermind


Thanked: 697
Posts: 15,880

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #5 on: December 29, 2008, 10:11:14 AM »

The only VBS I ever heard of it .vbs or VBScript

::)
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
kpac
Topic Starter
Web moderator
Hacker



Thanked: 180
Posts: 5,874

Certifications: List
Computer: Specs
Experience: Expert
OS: Windows 7
kpac®

1 1 1
« Reply #6 on: December 31, 2008, 07:52:46 AM »

Can the second script above be used to restart the PC instead of shutdown...?
IP logged

BC_Programmer
Mastermind


Thanked: 697
Posts: 15,880

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #7 on: December 31, 2008, 07:59:25 AM »

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...

IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
kpac
Topic Starter
Web moderator
Hacker



Thanked: 180
Posts: 5,874

Certifications: List
Computer: Specs
Experience: Expert
OS: Windows 7
kpac®

1 1 1
« Reply #8 on: December 31, 2008, 09:02:04 AM »

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

I'm just starting with VBS... :)
IP logged

Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #9 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.  8)
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
kpac
Topic Starter
Web moderator
Hacker



Thanked: 180
Posts: 5,874

Certifications: List
Computer: Specs
Experience: Expert
OS: Windows 7
kpac®

1 1 1
« Reply #10 on: December 31, 2008, 12:35:09 PM »

Thanks again, I'll test it tomorrow...
IP logged

BC_Programmer
Mastermind


Thanked: 697
Posts: 15,880

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #11 on: December 31, 2008, 01:31:51 PM »

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.  8)

well, if this darn notepad had intellisense like the vb6 IDE...
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
kpac
Topic Starter
Web moderator
Hacker



Thanked: 180
Posts: 5,874

Certifications: List
Computer: Specs
Experience: Expert
OS: Windows 7
kpac®

1 1 1
« Reply #12 on: January 01, 2009, 06:40:18 AM »

well, if this darn notepad had intellisense like the vb6 IDE...

Excuses, excuses.... :D
IP logged

Sidewinder
Guru



Thanked: 97
Posts: 4,342

Experience: Familiar
OS: Windows 7

« Reply #13 on: January 01, 2009, 07:39:07 AM »

well, if this darn notepad had intellisense like the vb6 IDE...

Excuses, excuses.... :D

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. ;D
IP logged

If you don't know where you are going, any road will get you there

                                                                            -Lewis Carroll
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,880

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #14 on: January 01, 2009, 09:31:19 AM »

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().


IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Pages: [1] - (Top) Print 
Home / Software / Computer programming / VBS: Can it shut down a PC? « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.108 seconds with 20 queries.