If you need to shutdown computers per OU in Active Directory here is one way of doing it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
On Error Resume Next Const ADS_SCOPE_SUBTREE = 2 Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCOmmand.ActiveConnection = objConnection objCommand.CommandText = "Select Name, Location from 'LDAP://<strong>XXXXXXXXXXXXXXXXXXXXXXXXXXXX</strong>' " _ & "Where objectClass='computer'" objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE Set objRecordSet = objCommand.Execute objRecordSet.MoveFirst Do Until objRecordSet.EOF 'Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value 'Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value txtPCName = objRecordSet.Fields("Name").Value txtshutdown = "shutdown -t 15 -s -f -m \\" & txtPCName set objShell = CreateObject("WScript.Shell") objShell.Run txtShutdown objRecordSet.MoveNext Loop |
The only change you have to make for this script to work is to provide an LDAP string/path to the OU in AD where you’d like to shut the PCs.
In my company we had to come up with a solution to comply with the “green” policy enforced upon us. Initially I have tried the built-in shutdown command and feeding in computer names using Excel and CONCATENATE function. Issue with that approach was that my script was trying to reach out to each machine to check if its on the network and pingable taking too long to complete. Script above doesn’t have the same issues – it literally fires up shutdown request simultaneously to all machines on the specified OU – much quicker and definitely more effective!
If you guys have other ideas or have other solutions please let me know in comments below!