Logoff, Reboot Or Shutdown All Computers In An OU

By | August 15, 2006

Being an Network Admin isn't always easy, at some point in time you will need to reboot or shutdown all of your computers.
This script will shutdown, reboot or loggoff all computers in an OU.
———-COPY EVERYTHING BELOW THIS LINE———-

'This script will shut down all computers on the domain
'Created by C.E. Harden August 14 2006

Const ADS_SCOPE_SUBTREE = 2
Const OPEN_FILE_FOR_WRITING = 2
Const ForReading = 1

Dim oFSO, objFSO1, oTS, sClient, oWindows, oLocator, oConnection, oSys
Dim strUser, strPassword, strFile, strWritePath, strDirectory

'issue shutdown to OS
 ' 4 = force logoff
 ' 5 = force shutdown
 ' 6 = force rebooot
 ' 12 = force power off
strInput = InputBox("Type in 4 to force logoff" & vbCrLf _
& "Type in 5 to force shutdown"& vbCrLf _
& "Type in 6 to force reboot"& vbCrLf _
& "Type in 12 to force poweroff")
IF strInput > "" Then
strUser = InputBox("Type in your login!")
IF strUser > "" Then
strPassword = InputBox("Type in your password!")
If strPassword > "" Then
strFile = "Computers.txt"
strWritePath = "\\SERVER_NAME_HERE\files\Scripts\" & strFile
strDirectory = "\\SERVER_NAME_HERE\files\Scripts\"

'#########
Set objFSO1 = CreateObject("Scripting.FileSystemObject")

If objFSO1.FileExists(strWritePath) Then
    Set objFolder = objFSO1.GetFile(strWritePath)

Else
    Set objFile = objFSO1.CreateTextFile(strDirectory & strFile)
    'Wscript.Echo "Just created " & objFolder & "\" & strFile
    objFile = ""

End If
'#########

Set fso = CreateObject("Scripting.FileSystemObject")
Set textFile = fso.OpenTextFile(strWritePath, OPEN_FILE_FOR_WRITING)

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://OU=workstations,OU=detroit,DC=contoso,DC=com' " _
        & "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
    textFile.WriteLine(objRecordSet.Fields("Name").Value)
    objRecordSet.MoveNext
Loop

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objArgs = Wscript.Arguments
Set objTextFile = objFSO.OpenTextFile(strWritePath, ForReading)

Do Until objTextFile.AtEndOfStream
    strReg = objTextFile.Readline
    'Wscript.Echo strReg & " From Text File"
Loop
'$$$$$$$$$$$$

'open list of client names
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile(strWritePath)

Do Until oTS.AtEndOfStream
 
 'get next client name
 sClient = oTS.ReadLine
 
 'get WMI locator
 Set oLocator = CreateObject("WbemScripting.SWbemLocator")

 'Connect to remote WMI
 Set oConnection = oLocator.ConnectServer(sClient, "root\cimv2", strUser, strPassword)

 
 Set oWindows = oConnection.ExecQuery("Select " & "Name From Win32_OperatingSystem")
 For Each oSys In oWindows
   oSys.Win32ShutDown(strInput) ' This was a number "5"
 Next

Loop

'close the text file
oTS.Close
WScript.Echo "All done!"

Else
Wscript.Quit

End If

Else
Wscript.Quit

End If

Else
Wscript.Quit

End If

———-COPY EVERYTHING ABOVE THIS LINE———-

PLEASE MAKE SURE NO WORD WRAPPING IS HAPPENING IN YOUR SCRIPT!!!

To make this script work you will need three things!

1.A path to the file where the script will write computer names to.
Edit the variable "strWritePath"
(e.g., strWritePath = "\\SERVER_NAME_HERE\files\Scripts\" & strFile)

2.A path to the directory where the script will write computer names to.
Edit the variable "strDirectory"
(e.g., strDirectory = "\\SERVER_NAME_HERE\files\Scripts\")

3. Lastly, you will need the OU in which your computer accounts are kept.
Edit the string LDAP://
(e.g., 'LDAP://OU=workstations,OU=sales,DC=contoso,DC=com')

*Make sure you are an administrator to run this.

This information is provided "AS IS" with no warranties expressed or implied.

Leave a Reply

Your email address will not be published. Required fields are marked *