Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: VBS: Can it shut down a PC?  (Read 18610 times)

0 Members and 1 Guest are viewing this topic.

kpac

    Topic Starter
  • Web moderator


  • Hacker

  • kpac®
  • Thanked: 184
    • Yes
    • Yes
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
VBS: Can it shut down a PC?
« on: December 27, 2008, 08:47:08 AM »
Can VBScript shut down a PC?
Can it shut down all PCs on a LAN?

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: VBS: Can it shut down a PC?
« 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)
   
   
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

kpac

    Topic Starter
  • Web moderator


  • Hacker

  • kpac®
  • Thanked: 184
    • Yes
    • Yes
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
Re: VBS: Can it shut down a PC?
« Reply #2 on: December 27, 2008, 10:09:53 AM »
Woah, thanks. :)

DFND-jimjim1



    Beginner

  • What would you do if you knew you could not fail?
    Re: VBS: Can it shut down a PC?
    « Reply #3 on: December 29, 2008, 07:39:08 AM »
    The only VBS I ever heard of it .vbs or VBScript

    kpac

      Topic Starter
    • Web moderator


    • Hacker

    • kpac®
    • Thanked: 184
      • Yes
      • Yes
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 7
    Re: VBS: Can it shut down a PC?
    « Reply #4 on: December 29, 2008, 08:18:12 AM »
    What?

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS: Can it shut down a PC?
    « Reply #5 on: December 29, 2008, 10:11:14 AM »
    The only VBS I ever heard of it .vbs or VBScript

    ::)
    I was trying to dereference Null Pointers before it was cool.

    kpac

      Topic Starter
    • Web moderator


    • Hacker

    • kpac®
    • Thanked: 184
      • Yes
      • Yes
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 7
    Re: VBS: Can it shut down a PC?
    « Reply #6 on: December 31, 2008, 07:52:46 AM »
    Can the second script above be used to restart the PC instead of shutdown...?

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS: Can it shut down a PC?
    « 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...

    I was trying to dereference Null Pointers before it was cool.

    kpac

      Topic Starter
    • Web moderator


    • Hacker

    • kpac®
    • Thanked: 184
      • Yes
      • Yes
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 7
    Re: VBS: Can it shut down a PC?
    « 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... :)

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: VBS: Can it shut down a PC?
    « 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)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    kpac

      Topic Starter
    • Web moderator


    • Hacker

    • kpac®
    • Thanked: 184
      • Yes
      • Yes
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 7
    Re: VBS: Can it shut down a PC?
    « Reply #10 on: December 31, 2008, 12:35:09 PM »
    Thanks again, I'll test it tomorrow...

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS: Can it shut down a PC?
    « 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...
    I was trying to dereference Null Pointers before it was cool.

    kpac

      Topic Starter
    • Web moderator


    • Hacker

    • kpac®
    • Thanked: 184
      • Yes
      • Yes
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 7
    Re: VBS: Can it shut down a PC?
    « Reply #12 on: January 01, 2009, 06:40:18 AM »
    well, if this darn notepad had intellisense like the vb6 IDE...

    Excuses, excuses.... :D

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: VBS: Can it shut down a PC?
    « 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
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS: Can it shut down a PC?
    « 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().


    I was trying to dereference Null Pointers before it was cool.