Re: remote shutdown, not logged in

From: Torgeir Bakken (MVP) (Torgeir.Bakken-spam@hydro.com)
Date: 02/09/03


From: "Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com>
Date: Sun, 09 Feb 2003 20:19:42 +0100


Steve O'Brien wrote:

> Hi,
>
> We're using a whole bunch of XP clients at our
> organization (using SP1, all the critical updates
> available as of right now). Instead of going around at
> night, we'd like to shut them all down remotely.
>
> I tried:
>
> shutdown -s -m \\pc19
>
> This works when a user (guest, admin, etc) is logged in
> at the console. When the login screen is up, this
> command doesn't work. I'm logged on interactively at the
> machine from which I'm running the remote shutdown
> command, as a domain administrator. I'm not having an
> access denied issue - the error returned is "the device
> is not ready".

Hi

Using VBScript/WMI may work better. Put the following code into a file called
e.g. shutdwn.vbs. If you run it without any input arguments, it will do a forced
power off the local computer. If you add one or more remote computer names/ip
addresses on the command line, the script will do a forced power off on the
remote computer(s). See further down in the script for more shutdown/reboot
options.

Set oArgs = WScript.Arguments

If oArgs.Count = 0 Then
  ' local computer
  ShutDown ".", "PowerOff_Force"
Else
  For i = 0 to oArgs.Count - 1
    ShutDown oArgs(i), "PowerOff_Force"
  Next
End If

' 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

' use "." for local computer

Sub ShutDown(sNode, sAction)

  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
      ' Default value
      iCmd = EWX_POWEROFF
  End Select

  oOS.Win32shutdown iCmd

End Sub

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


Relevant Pages

  • RE: Finetuning: Remote Shutdown with WMI, some errors occur.
    ... Also we would like that a workstation that's locked is also shutdown by this ... script to shutdown all approx. ... Dim strBase, strFilter, strAttributes, strQuery, objRecordSet ... Const OpenAsDefault = -2 ...
    (microsoft.public.windows.server.scripting)
  • Re: Help needed with a js command
    ... The original script allowed to shutdown any computer in the ... computer, and I just need to reboot one computer, anyway. ... Const EWX_SHUTDOWN = 1 ...
    (microsoft.public.scripting.jscript)
  • Finetuning: Remote Shutdown with WMI, some errors occur.
    ... script to shutdown all approx. ... Modified script is found below. ... Dim strBase, strFilter, strAttributes, strQuery, objRecordSet ... Const OpenAsDefault = -2 ...
    (microsoft.public.windows.server.scripting)
  • Re: Remote script not running properly..............
    ... > I have a script on my local computer which calls another script ... > send some data back to my local computer. ... resources on the remote machine where it executes, ... > Const ForReading=1 ...
    (microsoft.public.scripting.vbscript)
  • Re: VBscript that restart the domain comptuer
    ... If you have a shutdown script that cleans up the profiles, ... permissions can restart all computers in a list remotely. ... I have already VB script I ...
    (microsoft.public.windows.server.active_directory)