Re: Finetuning: Remote Shutdown with WMI, some errors occur.
- From: Joris van der Struijk <JorisvanderStruijk@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 30 Mar 2006 22:53:01 -0800
Thx, for all the help. I think i am able to solve most of my problems now.
I have one other mojor problem that i am banging my head on. It's about RIS
(Remote Installation Service). Maby an MVP with knowledge about that could
have a look at it. It's that serious that it's holding down our educational
program.
URL:
http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.windowsxp.setup_deployment&mid=1bebfd81-f464-4f1b-88c5-e3681206c301
And
http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.deployment.desktop&mid=0d3c60c2-92cd-4764-8a0a-7fed1d6fdc9f
Thx in advance,
Joris
"Alex K. Angelopoulos [MVP]" wrote:
.
"Joris van der Struijk" <JorisvanderStruijk@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message news:16EB158A-BED4-446E-9303-96675C5CA74B@xxxxxxxxxxxxxxxx
Thx Alex. It realy gave me some better views about things.
About your article in Windows IT Pro, maby it's a good thing to post it
(or
some parts of it) if that's possible and your ok with it. It could give
me,
and others a better inside view of scripting for a lot of computers.
The script - which may have some breakage issues since the XML tags at the
beginning will have line breaks from the newsgroup posting process - is
below.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<job id="main">
<runtime>
<description><![CDATA[
Name: StopComputer.wsf
Description:
Allows shutdown, poweroff, or reboot of multiple remote systems.
Exit errorlevels
1 - generic unhandled script compile or runtime error; see displayed error.
3 - Non-numeric argument supplied where number is needed.
4 - Invalid authentication level.
5 - No single shutdown action was specified; must use one of /V,/P,/R, /S.
Hexadecimal errors returned by WMI of the form 0x8007nnnn can be looked up
by stripping off the leading hexadecimal 0x80070000, converting the trailing
hex numeral nnnn to decimal and using it with net helpmsg.
For example, the return error 0x80070005 has a base value of 5; running
net helpmsg 5
returns the error description
"Access is denied."
Known Bugs, Anomalies, or Limitations:
+ Systems must have WMI support installed.
+ When issuing a poweroff (/P) command to an operating system running in
VMware 3.xx, it may reboot instead of powering off.
+ console-mode use only; StopComputer must be run with cscript as its host.
If you intend to read from stdin, it is not sufficient to have cscript set
as your default host; explicitly specify cscript.exe and the full path to
this script. You can also put this script in a folder within your path along
with a file named StopComputer.cmd containing the single line
@cscript "%~dpn0.wsf" %*
You can then call it directly with just the base name stop-computer and have
it operate correctly.
]]></description>
<example><![CDATA[
To just TEST ability to shut down TS01 and TS02:
StopComputer /W TS01 TS02
To reboot 192.168.0.2
StopComputer /R 192.168.0.2
To shut down 192.168.0.2 preparatory to manual power-off, use /S:
StopComputer /S 192.168.0.2
To reboot 192.168.1.3, specifying verbose output and tracing and a
privileged account "ababa" with password "sesame":
StopComputer /R 192.168.1.3 /T /V+ /U:ababa:sesame
]]></example>
<named name="A" helpstring="Specify authentication level used by WMI. The
default level is Pkt, to ensure successful connection to XP/2003 systems.
Valid authentication levels are Default (0), None (1), Connect (2), Call
(3), Pkt (4), PktIntegrity (5), or PktPrivacy (6)." type="string"
required="false"/>
<named name="D" helpstring="Specify a delay interval in seconds before each
system is given its shutdown command. Note that this is NOT a short-term
scheduled shutdown - the shutdown command is only issued after the delay
expires. Halting the script prior to the delay expiration prevents shutdown
with no need to manually abort it." type="string" required="false"/>
<named name="R" helpstring="Reboots the computer. Only one switch in the set
/W, /R, /P, and /S can be specified." type="simple" required="false"/>
<named name="S" helpstring="Shuts down the computer. Only one switch in the
set /W, /R, /P, and /S can be specified." type="simple" required="false"/>
<named name="P" helpstring="Powers off the computer. Only one switch in the
set /W, /R, /P, and /S can be specified." type="simple" required="false"/>
<named name="T" helpstring="Turns on tracing. This causes various extra
information about the process to be echoed to stderr." type="simple"
required="false"/>
<named name="U" helpstring="Specify username and password explicitly, if
username:password form." type="string" required="false"/>
<named name="V" helpstring="Verbose mode. This causes contextual
information to be sent to stdout." type="simple" required="false"/>
<named name="W" helpstring="WhatIf mode. Instead of actually shutting down
the specified systems, it attempts to connect to them with the specified
privileges and provides success/failure output. This allows confirmation
that machines can be reached and that the account being used has sufficient
privileges to invoke shutdown. Only one switch in the set /W, /R, /P, and /S
can be specified." type="simple" required="false"/>
<unnamed name="Computer" helpstring="Computer(s) to shutdown. If none are
specified and they are not being read from stdin, defaults to the local
system." many="true" required="0"/>
<!--
Delay Seconds of delay before initial shutdown begins; defaults to 30
seconds if not specified. string false
WhatIf Instead of actually performing the reboot, attempts to acquire the
privileges to perform the specified reboot.
-->
</runtime>
<script language="VBScript"><![CDATA[
Option Explicit
Const NAMESPACE = "root/cimv2", WMICLASS = "Win32_OperatingSystem"
Const EWX_SHUTDOWN = 1, EWX_REBOOT = 2, EWX_POWEROFF = 8
Const EWX_FORCE = 4, WHATIF = -1
Const WMIBaseError = 2147024896
Const wbemAuthenticationLevelPkt = 4
' =======================================================================
' Argument Initialization
' For convenience, set global variables based on commandline arguments
' in this section of the script.
' =======================================================================
Dim m_Tracing: m_Tracing = ArgSimple("T")
Dim m_Verbose: m_Verbose = CLng( ArgBoolean("V", 1) )
TraceLine "Verbosity Level: " & m_Verbose
Dim Force: Force = ArgSimple("F")
Traceline "Forced?: " & CStr(Force)
Dim AuthLevel: AuthLevel = ArgLong("A", 4)
If AuthLevel < 0 or AuthLevel > 6 Then
AbortWithReason 4, "Invalid authentication level " & AuthLevel
End If
Traceline "Authorization Level: " & AuthLevel
Dim user: set user = ParseCredentials("U", _
"Enter password (nothing will be displayed):")
Traceline "Username specified: " & user.UserName
' Using False for the default return looks odd but means a clean Delay test.
Dim Delay: Delay = ArgLong("D", 2)
TraceLine "Per-execution delay (in seconds): " & Delay
Dim Action: Action = ArgSwitch( Array("W", "R", "S", "P") )
TraceLine "Alphabetic Action code: " & Action
Select Case Action
Case Empty
AbortWithReason 5, "Specify 1 and only one shutdown action"
Case "W" Action = -1
Case "R" Action = EWX_REBOOT
Case "S" Action = EWX_SHUTDOWN
Case "P" Action = EWX_POWEROFF
End Select
If ( Force And Not(Action = WHATIF) ) Then Action = (Action OR EWX_FORCE)
TraceLine "EWX code for Action (-1 is WhatIf): " & Action
' =======================================================================
' End of Argument Initialization.
' =======================================================================
Dim m_ExitCode: m_ExitCode = 0
dim m_Index: m_Index = 0
dim computer: computer = NextItem()
' WMI Locator setup
' For everything we do here we need to have remote shutdown privileges,
' and in general we need to have at least Pkt-level authentication to
' ensure connection succeeds with XP/2003.
' To minimize work, we set everything once and reuse the same Locator
' for each connection.
Dim Locator: Set Locator = CreateObject("WbemScripting.SWbemLocator")
Locator.Security_.Privileges.AddAsString "SeRemoteShutdownPrivilege", True
Locator.Security_.AuthenticationLevel = AuthLevel
Dim erNumber, erDesc
Do While computer <> vbNull
TraceLine "Entering main execution loop with computer: " & computer
WScript.Sleep Delay * 1000
On Error Resume Next
If user.IsNil Then
Dim Instances: Set Instances = Locator.ConnectServer _
(computer, NAMESPACE).InstancesOf( WMICLASS )
Else
Set Instances = Locator.ConnectServer _
( computer, NAMESPACE, user.UserName, user.Password ). _
InstancesOf( WMICLASS )
End If
erNumber = Err.Number: erDesc = Err.Description
On Error Goto 0
If erNumber <> 0 Then
WriteError computer, WMIBaseError + erNumber, _
"Error on authentication: " & erDesc
ElseIf Action = WHATIF Then
WriteSuccess computer, "WMI authentication succeeded"
Else
' We authenticated successfully and this wasn't a whatif.
' Let's do a shutdown.
Dim Instance
For Each Instance in Instances
If Instance.Primary = True Then
On Error Resume Next
Dim result: result = Instance.Win32Shutdown(Action, vbNull)
erNumber = Err.Number: erDesc = Err.Description
On Error Goto 0
If erNumber <> 0 Then
WriteError computer, erNumber, _
"Error trying to shut down: " & erDesc
ElseIf result <> 0 Then
WriteError computer, result, _
"Non-zero result code returned from shutdown;" _
& " look up error code with NET HLPMSG"
Else
WriteSuccess computer, "Ordering shutdown type: " & Action
End If
Exit For
End If
Next
End If
computer = NextItem()
Loop
' The following forced quit ensures that if any errors were encountered in
results, the script will give a calling script access to the last one as a
normal cmd.exe errorlevel.
WScript.Quit m_ExitCode
' =======================================================================
' Argument Parsing Helper Routines
' =======================================================================
Function ArgString(ByVal argName, ByVal defaultValue)
' If a string named argument was specified on the script's
' commandline, return it. If it wasn't, return a default value.
if WScript.Arguments.Named.Exists(argName) Then
ArgString = WScript.Arguments.Named(argName)
Else
ArgString = defaultValue
End If
End Function
Function ArgLong(ByVal argName, ByVal defaultValue)
' If a string named argument coercible to long was specified on the
' script's commandline, return it. If it wasn't, return a default value.
If WScript.Arguments.Named.Exists(argName) Then
On Error Resume Next
ArgLong = CLng( WScript.Arguments.Named(argName) )
If Err.Number <> 0 Then
AbortWithReason 3, "argument /" & argName _
& " needs a numeric value supplied."
End If
Else
ArgLong = defaultValue
End If
End Function
Function ArgSimple(ByVal argName)
' If a simple argument was specified on the script's commandline,
' return true. If it wasn't, return false.
if WScript.Arguments.Named.Exists(argName) Then
ArgSimple = True
Else
ArgSimple = false
End If
End Function
Function ArgBoolean(ByVal argName, ByVal defaultValue)
' Get an optional boolean named argument if it exists;
' if it does not exist, return the default value.
if WScript.Arguments.Named.Exists(argName) Then
ArgBoolean = CBool(WScript.Arguments.Named(argName))
Else
- References:
- Finetuning: Remote Shutdown with WMI, some errors occur.
- From: Joris van der Struijk
- RE: Finetuning: Remote Shutdown with WMI, some errors occur.
- From: Joris van der Struijk
- Re: Finetuning: Remote Shutdown with WMI, some errors occur.
- From: Jason Gurtz
- Re: Finetuning: Remote Shutdown with WMI, some errors occur.
- From: Joris van der Struijk
- Finetuning: Remote Shutdown with WMI, some errors occur.
- Prev by Date: redirect and ctrl-c
- Next by Date: monad : enumerate shares with permissions ?
- Previous by thread: Re: Finetuning: Remote Shutdown with WMI, some errors occur.
- Next by thread: Re: Finetuning: Remote Shutdown with WMI, some errors occur.
- Index(es):
Relevant Pages
|