Sometimes you want to run a simple command against every machine in a list or otherwise run a simple command and change one piece of info. Here is a quick and dirty script to do it.
For instance, lets say you want to check the AD Site every one of your Exchange Servers is in… You basically want to run
nltest /dsgetsite /server:*
where * is all of your exchange servers.
So if you have a file called exchangeservers.txt which has one exchange server per line you can use the perl script below with this simple command
forevery.pl exchangeservers.txt "nltest /dsgetsite /server:~server~" output.txt
Here is the script forevery.pl
$infile=shift; $cmd=shift; $outfile=shift; print "InFile: $infile\n"; print "OutFile: $outfile\n"; print "CMD: $cmd\n"; open IFH,"< $infile" or die("ERROR: Couldn't open infile - $infile : $!\n"); open OFH,">$outfile" or die("ERROR: Couldn't open outfile - $outfile : $!\n"); @servers=; close IFH; chomp @servers; foreach $thisserver (sort @servers) { next unless $thisserver=~/\w/; print "$thisserver\n"; print OFH "$thisserver\n"; print OFH "========================\n"; $thiscmd=$cmd; $thiscmd=~s/~server~/$thisserver/i; print " $thiscmd\n"; @out=`$thiscmd 2>&1`; print OFH "@out\n"; print OFH "==============================================================\n"; }
hi joe 😉
I know you are not a batch fan, but wouldn’t something like:
for /f “delims=XXXX” %i IN (file.txt) do @echo “===%i===” & nltest /dsgetsite /server:%i
do the job and save couple lines of code ?
Oh it isn’t that I am not a batch fan. I just don’t use it personally. 🙂
I like white space.