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

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 \\Server\Share\batchifle.bat syntax.

Report       is the fully qualified name of the log file, using the \\Server\Share\filename 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 off
setlocal
if \{%5\} EQU \{\} goto syntax
set domacct=%USERDOMAIN%\%1
set domacct="%domacct:"=%"
set pw=%2
set pw=%pw:"=%
set batch=%3
if NOT "%batch:~1,1%" EQU "\" goto syntax
if NOT exist %batch% goto syntax
set report=%4
set report=%report:"=%
if NOT "%report:~1,1%" EQU "\" goto syntax
if exist "%report%" del /q "%report%"
set query=dsquery * domainroot -filter "(&(objectCategory=Computer)(objectClass=User))" -attr sAMAccountName distinguishedName -limit 0
:loop
if \{%5\}==\{\} goto finish
set object=%5
set object=%object:"=%
shift
for /f "Skip=1 Tokens=1*" %%c in ('%query%') do (
 set machine=%%c
 set dn=%%d
 call :computer
)
goto loop
:finish
if not exist "%report%" goto MT
if 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>&1
del /q "%TEMP%\RunOn.tmp"
del /q "%TEMP%\RunOnSearch.tmp"
endlocal
exit /b 0
:MT
@echo RunOn - No matching distinguishedName>"%report%"
endlocal
exit /b 0
:CopyReport
copy "%TEMP%\RunOn.tmp" "%report%"
goto :EOF
:syntax
@echo Syntax: RunOn Account Password Batch Report String1 \[String2 ... StringN\]
endlocal
exit /b 1
:computer
:: Strip =
for /f "Tokens=*" %%f in ('@echo %dn% ^|find /I "DC"') do set dn=%%f
set Found=N
set machine=\\%machine:$=%
for /f "Tokens=*" %%f in ('@echo %dn% ^|find /I "%object%"') do set found=%%f
if "%found%" EQU "N" goto :EOF
@echo RunOn: %machine%>>"%report%"
PsExec %machine% -u %domacct% -p %pw% %batch%>>"%report%"



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish