RE: Finetuning: Remote Shutdown with WMI, some errors occur.



Addon:
Also we would like that a workstation that's locked is also shutdown by this
script. But i have no idea how to accomplice this.

"Joris van der Struijk" wrote:

Thx to Richard Mueller(Microsoft MVP Scripting and ADSI) i'v got my hand on a
script to shutdown all approx. 300 Windows XP domain computers in a specific
OU, and changed it to fit my needs. Modified script is found below.
It uses ADO to enumerate the computers in the specified hardcoded OU, pings
each
computer to see if it is online (and avoid a lengthy timeout if it is not),
then uses WMI to shutdown.

Most of the time it workes, but sometimes it stops in error saying something
like: "External server not available" or "shutdown.vbs(49, 9) (null):
0x80041021)" or "(57, 13) SWbemObjectEx: The RPC Server is Unavailable".
Also sometimes it can't ping a computer but i know it turned on, and
available. When i try pinging myself it works.

What i would like is for the script to have some error handling so it
doesn't stop executing and take out those nasty bugs. Im no great programmer,
so could realy use some help.
Thx in advance, Joris.

Script:
-----------
Option Explicit

Dim objCommand, objConnection
Dim strBase, strFilter, strAttributes, strQuery, objRecordSet
Dim strComputer, objWMIService, colOperatingSystems, objOperatingSystem
Dim strTempFile, objShell, objFSO

Const SHUTDOWN = 5

' Objects for function IsConnectible.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
strTempFile = objShell.ExpandEnvironmentStrings("%TEMP%")
strTempFile = strTempFIle & "\PingResult.tmp"

' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

' Specify base of search.
strBase = "<LDAP://ou=Test,dc=domain,dc=nl>"

' Filter on computer objects.
strFilter = "(objectCategory=computer)"

' Comma delimited list of attributes to retrieve.
strAttributes = "Name"

' Construct the ADO query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False

' Query AD and return recordset.
Set objRecordSet = objCommand.Execute

' Enumerate the recordset.
Do Until objRecordSet.EOF
' Retrieve computer name.
strComputer = objRecordSet.Fields("Name")
' Make sure computer online.
If IsConnectible(strComputer, 2, 750) Then
WScript.Echo strComputer & " is going down NOW."
' Connect to computer with WMI.
' If WMI cannot be used, run shutdown.exe here.
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem In colOperatingSystems
objOperatingSystem.Win32Shutdown(SHUTDOWN)
Next
End If
objRecordSet.MoveNext
Loop

' Clean up.
objRecordset.Close
objConnection.Close
Set objCommand = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing
Set objShell = Nothing
Set objFSO = Nothing


Function IsConnectible(strHost, intPings, intTO)
' Returns True if strHost can be pinged.
' Based on a program by Alex Angelopoulos and Torgeir Bakken.
Dim objFile, strResults

If intPings = "" Then intPings = 2
If intTO = "" Then intTO = 750

Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1

objShell.Run "%comspec% /c ping -n " & intPings & " -w " & intTO _
& " " & strHost & ">" & strTempFile, 0, True

Set objFile = objFSO.OpenTextFile(strTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)
strResults = objFile.ReadAll
objFile.Close

Select Case InStr(strResults, "TTL=")
Case 0
WScript.Echo strHost & " did NOT respond to ping."
IsConnectible = False
Case Else
WScript.Echo strHost & " did respond to ping."
IsConnectible = True
End Select
End Function
.



Relevant Pages

  • Re: Avoid loading images - Avoid sounds
    ... able to browse normally while my script runs, ... Const cVBS = "Fetched.vbs" ... Dim strDIR ... Set objXML = CreateObject ...
    (microsoft.public.scripting.vbscript)
  • Re: Avoid loading images - Avoid sounds
    ... able to browse normally while my script runs, ... Const cVBS = "Fetched.vbs" ... Dim strDIR ... Set objXML = CreateObject ...
    (microsoft.public.scripting.vbscript)
  • Re: Script: Remote shutdown of all domain computers
    ... When trying to run the script from a XP workstation, where i am logged in as ... Dim strBase, strFilter, strAttributes, strQuery, objRecordSet ... 2000 or above and allow shutdown, ... Public Function AllComputersAs String() ...
    (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)