JSI Tip 7741. How do I run a batch on every computer in my domain in a specified OU or container?

Jerold Schulman

February 10, 2004

2 Min Read
ITPro Today logo in a gray background | ITPro Today

I have scripted RunOn.bat to run a specified batch file on every domain computer in specified OUs or containers.

The syntax for using RunOn.bat is:

RunOn Account Password Batch Report String1 [String2 ... StringN]

Where:

Account      is the %USERDOMAIN%Account that will be used by PsExec.exe.Password     is the password of %USERDOMAIN%Account.Batch        is the fully qualified name of the batch file to run, using the \ServerSharebatchifle.bat syntax.Report       is the fully qualified name of the log file, using the \ServerSharefilename syntax.Stringn      is the OU or container to match with each computer's DistinguishedName.               NOTE: Prior to matching, all of the equal (=) signs in each computer's DN are replaced with a space.             To run the Batch on every computer in every OU, use a String of "OU ".             To run the Batch on every computer in the Domain Controllers OU,                    use "OU Domain Controllers" or "domain controllers" or "ain con".             To run the Batch on every computer in the domain, use " ".             To run the Batch on every computer in the Computers container, use "CN Computers".

NOTE: RunOn.bat uses PsExec.exe and DSQUERY.

RunOn.bat contains:

@echo offsetlocalif {%5} EQU {} goto syntaxset domacct=%USERDOMAIN%%1set domacct="%domacct:"=%"set pw=%2set pw=%pw:"=%set batch=%3if NOT "%batch:~1,1%" EQU "" goto syntaxif NOT exist %batch% goto syntaxset report=%4set report=%report:"=%if NOT "%report:~1,1%" EQU "" goto syntaxif exist "%report%" del /q "%report%"set query=dsquery * domainroot -filter "(&(objectCategory=Computer)(objectClass=User))" -attr sAMAccountName distinguishedName -limit 0:loopif {%5}=={} goto finishset object=%5set object=%object:"=%shiftfor /f "Skip=1 Tokens=1*" %%c in ('%query%') do ( set machine=%%c set dn=%%d call :computer)goto loop:finishif not exist "%report%" goto MTif exist "%TEMP%RunOn.tmp" del /q "%TEMP%RunOn.tmp"@echo PsExec>"%TEMP%RunOnSearch.tmp"@echo Copyright (C) 2001 Mark Russinovich>>"%TEMP%RunOnSearch.tmp"@echo www.sysinternals.com>>"%TEMP%RunOnSearch.tmp"for /f "Tokens=*" %%l in ('type "%report%" ^|findstr /I /V /G:"%TEMP%RunOnSearch.tmp"') do ( @echo %%l>>"%TEMP%RunOn.tmp")del /q "%report%"call :CopyReport>nul 2>&1del /q "%TEMP%RunOn.tmp"del /q "%TEMP%RunOnSearch.tmp"endlocalexit /b 0:MT@echo RunOn - No matching distinguishedName>"%report%"endlocalexit /b 0:CopyReportcopy "%TEMP%RunOn.tmp" "%report%"goto :EOF:syntax@echo Syntax: RunOn Account Password Batch Report String1 [String2 ... StringN]endlocalexit /b 1:computer:: Strip =for /f "Tokens=*" %%f in ('@echo %dn% ^|find /I "DC"') do set dn=%%fset Found=Nset machine=\%machine:$=%for /f "Tokens=*" %%f in ('@echo %dn% ^|find /I "%object%"') do set found=%%fif "%found%" EQU "N" goto :EOF@echo RunOn: %machine%>>"%report%"PsExec %machine% -u %domacct% -p %pw% %batch%>>"%report%"



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like