Re: Script: Remote shutdown of all domain computers



Also with this script: What if the IPadress of the PC changed recently? The
first we have to do an "ipconfig /flushdns" to clear the cache to ensure it
has te right IP.

"Richard Mueller" wrote:

Hi,

You can use ADO to retrieve the computer names. If the computers are Windows
2000 or above and allow shutdown, you can use WMI. Otherwise you can try the
shutdown.exe utility (I have not tried to use it remotely). The below 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. It assumes you have permissions to shutdown and
the computers support this.

=========================
Option Explicit

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

Const SHUTDOWN = 1

' 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=Hosts,dc=MyDomain,dc=com>"

' 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, 1, 750) Then
' Connect to computer with WMI.
' If WMI cannot be used, run shutdown.exe here.
Set objWMIService = GetObject("winmgmts: {(Shutdown)}" _
& "{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 objRootDSE = Nothing
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
IsConnectible = False
Case Else
IsConnectible = True
End Select
End Function
===================

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net

"Joris van der Struijk" <JorisvanderStruijk@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message news:18E53AC5-1748-4B2A-A5DB-84595C8BB498@xxxxxxxxxxxxxxxx
We realy need a script to automaticaly and remotly shutdown all of our 300
WXP SP2 client PC's in our domain located in the OU: Hosts.
I'v searched through the newsgroups for a complete script but to no
result.
Found some interresting scripts but nothing to directly inplement.

Because i can't realy script in VB, i am unable to create my own scripts.
I
am able to adjust tho.

I'v found a message here that i would like to edit so it fits our needs.
URL:
http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.windows.server.scripting&mid=a836c120-8c3f-41bc-9f16-115bfcd324ba

This message involves the shutdown command. It also suggests to loop
through
AD to read the computernames and then execute a remote shutdown with the
resolved computername.
I would realy like to use this but can't script it myself.
I'v also found a script to search trough AD for the computers and added
the
script below.

Hope someone can help me with this.
Thx,
Joris

SCRIPT:
-------------------------------------
Public Function AllComputers() As String()

'PURPOSE: Gets all Computers for the current domain
'and returns them in a string array, using LDAP

'Requires: ADSI, LDAP provider
'This function tested on Windows 2000 RC2

'RETURNS: String array containing all
'Computers for the current domain

'Requires VB6 because in lower versions
'array cannot be return type for a
'function

'EXAMPLE
'Dim sArray() As String
'Dim iCtr As Integer

'sArray = AllComputers
'For iCtr = 0 To UBound(sArray)
' Debug.Print sArray(iCtr)
'Next

Dim conn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim oRoot As IADs
Dim oDomain As IADs
Dim sBase As String
Dim sFilter As String
Dim sDomain As String

Dim sAttribs As String
Dim sDepth As String
Dim sQuery As String
Dim sAns() As String
Dim iElement As Integer

On Error GoTo errhandler:

Set oRoot = GetObject("LDAP://rootDSE";)
sDomain = oRoot.Get("defaultNamingContext")
Set oDomain = GetObject("LDAP://"; & sDomain)
sBase = "<" & oDomain.ADsPath & ">"
sFilter = "(&(objectCategory=Computer))"
sAttribs = "name"
sDepth = "subTree"

sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth

conn.Open _
"Data Source=Active Directory Provider;Provider=ADsDSOObject"

Set rs = conn.Execute(sQuery)
ReDim sAns(0) As String

With rs
Do While Not .EOF
iElement = IIf(sAns(0) = "", 0, iElement + 1)
ReDim Preserve sAns(iElement) As String
sAns(iElement) = rs("name")
.MoveNext
Loop
End With
AllComputers = sAns

errhandler:

On Error Resume Next
If rs.State <> 0 Then rs.Close
If conn.State <> 0 Then conn.Close
Set rs = Nothing
Set conn = Nothing
Set oRoot = Nothing
Set oDomain = Nothing

End Function



.



Relevant Pages

  • Script: Remote shutdown of all domain computers
    ... I'v searched through the newsgroups for a complete script but to no result. ... I'v also found a script to search trough AD for the computers and added the ... Public Function AllComputersAs String() ... 'Dim sArray() As String ...
    (microsoft.public.windows.server.scripting)
  • 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: Script: Remote shutdown of all domain computers in spec. OU
    ... I'v searched through the newsgroups for a complete script but to no ... I'v also found a script to search trough AD for the computers and added ... Public Function AllComputersAs String() ... 'Dim sArray() As String ...
    (microsoft.public.scripting.vbscript)
  • Script: Remote shutdown of all domain computers in spec. OU
    ... I'v searched through the newsgroups for a complete script but to no result. ... I'v also found a script to search trough AD for the computers and added the ... Public Function AllComputersAs String() ... 'Dim sArray() As String ...
    (microsoft.public.scripting.vbscript)
  • 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)

Loading