Re: Shutdown from a VBS file?



Nelson Rodriguez wrote:

How can I shutdown my XP (or 2K-9X) from a VBS script file
at a sheduled time?
Hi,

You can e.g. use a VBScript/WMI solution. Win2k and WinXP comes with
WMI builtin, on Win9x you will need to install the WMI core first.

Put the code below into a file called e.g. shutdwn.vbs, run it with
the following command line to do a power off:

wscript.exe "<path-to-vbs-file>"

If you want a forced power off:

wscript.exe "<path-to-vbs-file>" PowerOff_Force

See the ShutDown sub for more supported input parameters


'--------------------8<---------------------- Set oArgs = WScript.Arguments

If oArgs.Count = 0 Then
  ' PowerOff as default value
  ShutDown ".", "PowerOff"
Else
  ShutDown ".", oArgs(i)
End If


Sub ShutDown(sNode, sAction)

' First parameter:
' use "." for local computer

' Second parameter:
' Use "PowerOff" for a poweroff
' Use "PowerOff_Force" for a forced poweroff
' Use "Shutdown" for a shutdown
' Use "Shutdown_Force" for a forced shutdown
' Use "Reboot" for a reboot
' Use "Reboot_Force" for a forced reboot
' Use "LogOff" for a logoff
' Use "LogOff_Force" for a forced logoff

  Const EWX_LOGOFF = 0
  Const EWX_SHUTDOWN = 1
  Const EWX_REBOOT = 2
  Const EWX_FORCE = 4
  Const EWX_POWEROFF = 8

  On Error Resume Next
  Set oWMI = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate,(Shutdown)}!\\" _
     & sNode & "\root\cimv2")

  Set colOperatingSystems = oWMI.ExecQuery _
     ("Select * from Win32_OperatingSystem")
  For Each obj in colOperatingSystems
    Set oOS = obj :  Exit For
  Next
  If Err.Number <> 0 Then
    WScript.Echo "Could not connect to " & sNode
    Exit Sub
  End If

  sAction = LCase(sAction)

  Select Case sAction
    Case "logoff"
      iCmd = EWX_LOGOFF
    Case "logoff_force"
      iCmd = EWX_LOGOFF + EWX_FORCE
    Case "shutdown"
      iCmd = EWX_SHUTDOWN
    Case "shutdown_force"
      iCmd = EWX_SHUTDOWN + EWX_FORCE
    Case "reboot"
      iCmd = EWX_REBOOT
    Case "reboot_force"
      iCmd = EWX_REBOOT + EWX_FORCE
    Case "poweroff"
      iCmd = EWX_POWEROFF
    Case "poweroff_force"
      iCmd = EWX_POWEROFF + EWX_FORCE
    Case Else
       MsgBox "Error: invalid input parameter!", _
           vbExclamation + vbSystemModal, "ShutDown"
  End Select

  oOS.Win32shutdown iCmd

End Sub
'--------------------8<----------------------



--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx
.



Relevant Pages

  • Re: how to shutdown computer?
    ... ' Use "PowerOff" for a poweroff ... ' Use "Shutdown" for a shutdown ... ' Use "Reboot" for a reboot ... iCmd = EWX_LOGOFF + EWX_FORCE ...
    (microsoft.public.windows.server.scripting)
  • Re: Stop multiple services and restart server
    ... ' Use "PowerOff" for a poweroff ... ' Use "Shutdown" for a shutdown ... ' Use "Reboot" for a reboot ... iCmd = EWX_LOGOFF + EWX_FORCE ...
    (microsoft.public.windows.server.scripting)
  • Solaris 10 11/06 x86_64: almost impossible to shutdown the system
    ... After upgrade to 11/06 it became almost impossible to shutdown the ... I do not have console login avaiable when this ... so I can type "poweroff" from there - machine ... Single user mode also doesn't work. ...
    (comp.unix.solaris)
  • Re: Introducing a poweroff(8) command
    ... > script that runs shutdown or as a link to shutdown... ... Here's a version of the poweroff command implemented as a hard ... retrieving revision 1.8 ... diff -u -r1.23 shutdown.8 ...
    (freebsd-arch)
  • Re: shutdown wont shutdown
    ... Nothing will seem to poweroff my PC. ... > commands, ... run lilo again and then reboot. ... to shutdown and have the computer actually turn itself off. ...
    (Debian-User)