Need a batch file to perform an action, executable, or script for online domain members only?
Here, for simplicity, our do-something is "echo computer-name is online":
Save the following to a batch file: (two % signs are needed in batch files, one % if at the command-prompt)
@echo off
dsquery * domainroot -filter "(&(objectCategory=Computer))" -limit 0 -attr cn > computers.txt
for /f %%i in (computers.txt) do (
ping -n 1 -w 500 %%i | find "Lost = 0">NUL && echo %%i is online || echo %%i is offline
)
del computers.txt
This can easily be changed to work on a specific OU and skip sub-OU's (-scope onelevel):
dsquery * "OU=EXAMPLE_OU,DC=EXAMPLE,DC=COM" -filter "(&(objectCategory=Computer))" -limit 0 -attr cn -scope onelevel > computers.txt
So your do-something could be powerful when combined with commands like:
psexec.exe \\%%i some-command
or
START /B some-batch-calling-psexec.bat
You could get fancy and do multiple things by enclosing them in ( ) but separating them with &:
( echo %%i online & script.bat %%i)
So here is an example batch file that runs "gpudate /target:computer /force" on the Computers OU and it's sub OU's (This assumes you are logged in with administrative privileges):
@echo off
setlocal enableextensions enabledelayedexpansion
dsquery * "OU=Computers,DC=EXAMPLE,DC=COM" -filter "(&(objectCategory=Computer))" -limit 0 -attr cn > computers.txt
for /f %%i in (computers.txt) do (
ping -n 1 -w 500 %%i | find "Lost = 0">NUL && ( echo !time! %%i ONline, Launching... & START /B psexec.exe \\%%i -e -d -s -low gpupdate /target:computer /force ) || echo !time! %%i OFFline
)
del computers.txt
echo.
--------------
Please consider crypto tipping:
Thanks me!!! Fighting windows batch limitations is a b1tch!!!
ReplyDelete