Running a command on all computers within an AD OU

The below VBS script will run a command on all computers in the Active Directory OU defined by LDAPPath =

The script depends on the Windows Sysinternals tool PsExec, you will need to download and exact PsExec and place in in the same folder as the script.

In this case I am using the script and PsExec to run the command “IPConfig /flushdns” on all computers in the my “Servers” OU, the script could easily be adapted to to run something different.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const ADS_SCOPE_SUBTREE = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
Set objShell = CreateObject("WScript.Shell")

LDAPPath = "OU=Servers,DC=domain,DC=co,DC=uk"

tmpDate = date
tmpTime = time

startDate = Replace(tmpDate, "/", "-")
startTime = Replace(tmpTime, ":", "-")

currentScriptPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
LogPath = currentScriptPath & "FlushDNS " & startDate & " " & startTime & " " & userName & ".log"

strMbox = MsgBox("Are you sure you want to flush DNS on all computers under " & LDAPPath ,4,"Are you sure?")
 
if strMbox = 6 Then

 OutputLog "IPConfig /flushdns for all computers under an OU", 1, false
 OutputLog "=======================================", 1, false
 OutputLog "Started: " & " " & tmpData & " at " & tmpTime, 1, false
 OutputLog "=======================================", 1, false

 objConnection.Provider = "ADsDSOObject"
 objConnection.Open "Active Directory Provider"

 OutputLog "OU to Process :: " & LDAPPath, 1, true

 Set objCOmmand.ActiveConnection = objConnection
 objCommand.CommandText = _
 "Select Name from '" & LDAPPath & "' Where objectCategory='computer' ORDER BY Name"

 objCommand.Properties("Page Size") = 1000
 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

 Set objRecordSet = objCommand.Execute

 objRecordSet.MoveFirst

 Do Until objRecordSet.EOF
 
 OutputLog "Started on :: " & objRecordSet.Fields("Name").Value, 2, false
 objShell.Run "PsExec.exe  \\" & objRecordSet.Fields("Name").Value & " -e ipconfig /flushdns", 0, true
 
 objRecordSet.MoveNext
 Loop
 
 tmpDate = date
 tmpTime = time

 startDate = Replace(tmpDate, "/", "-")
 startTime = Replace(tmpTime, ":", "-")

 OutputLog "=======================================", 1, true
 OutputLog "Complete: " & tmpData & " at " & tmpTime, 1, false
 OutputLog "=======================================", 1, false

 wscript.echo "Complete"
else 
 'User clicked no
 MsgBox "FlushDNS cancelled"
End if 

Sub OutputLog (LogLine, intLevel, startNewLine)
 strSpaces = ""
 strStartNewLine = ""
 
 If objFSO.FileExists(LogPath) then
 Set objLogFile = objFSO.OpenTextFile(LogPath, ForAppending)
 Else
 Set objLogFile = objFSO.CreateTextFile(LogPath)
 End if
 
 if intLevel = 1 then 
 strSpaces = ""
 Elseif intLevel = 2 then 
 strSpaces = chr(9)
 Elseif intLevel = 3 then 
 strSpaces = chr(9) & chr(9)
 Elseif intLevel = 4 then 
 strSpaces = chr(9) & chr(9) & chr(9)
 Else
 strSpaces = ""
 End if
 
 if startNewLine = TRUE then
 strStartNewLine = chr(13) & chr(10)
 End if
 
 objLogFile.Write(strStartNewLine & strSpaces & LogLine & chr(13) & chr(10))
 
 objLogFile.Close
End Sub

1 thought on “Running a command on all computers within an AD OU”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.