badbiddy
badbiddy

Reputation: 11

batch file creation pse

I'm trying to run a batch file that will connect to the list of ip's on my network, open up cmd then run a list of commands: like ipconfig /all, nbstat -c, arp -a. Then it must save the results into a folder renamed as that "computername".

I already have a batch file made that can do the commands I want and create a folder with the computer, then input the different commands into txt files within that folder.

Here is the WindowsCommands batch file:

md %computername%

echo off

echo ARP Command
arp -a >> %cd%\%computername%\arp-a.txt

echo NBSTAT Command
nbtstat -c  >> %cd%\%computername%\nbstat.txt

echo Ipconfig Command
ipconfig /all  >> %cd%\%computername%\ipconfig-all.txt

echo Ipconfig DNS Command
ipconfig /displaydns  >> %cd%\%computername%\ipconfig-displaydns.txt

echo Netstat Command
netstat -ano  >> %cd%\%computername%\netstat-ano.txt

echo Tasklist Command
tasklist /v  >> %cd%\%computername%\tasklist.txt

echo LG Admin Command
net localgroup administrators >> %cd%\%computername%\netlocalgroupadmin.txt

echo Directory Command 
dir C:\Windows\Prefetch >> %cd%\%computername%\prefetch.txt

exit

I also created a hosts.txt file that contains my local Ip addresses that I want to run the commands on.

I also created another batch file name psexec for running a For loop.

Now my troubles and arising when trying to run the psexec batch file.

Here is my psexec file:

for /f %%a in (hosts.txt) do (
psexec \\%%a C:\Users\ISSG\Documents\WindowsCommands.bat
)

Now that is just a rough draft I'm not entirely sure if that is how it should be coded. This is one of the first automated scripts I have ever wrote.

So in a nutshell i need to be able to run this batch file from my local computer- psexec into the IP's. Gather the information and output it into txt files on my local computer.

If anyone could point me in the right direction that would be great! Thanks!

Upvotes: 1

Views: 1139

Answers (1)

Matt Williamson
Matt Williamson

Reputation: 7095

If your Batch file doesn't exist in the system directory on all of the computers, you have to use the -c switch to copy it to them in order to run it. It's a good idea to try to ping the computer first to save time trying to connect to it.

for /f %%a in (hosts.txt) do (
  for /f "tokens=2 delims=:" %%b in ('ping -n 1 %%a ^| find "TTL="') do (
    if errorlevel 0 (
      psexec \\%%a -c -f -u username -p password C:\Users\ISSG\Documents\WindowsCommands.bat
    )
  )
)

Also, keep in mind that this will create the folder in the System32 directory on the remote computer. If you want it on your local drive, do something like this:

@echo off

FOR /F "tokens=2" %%A IN (
  'net use * "\\computer\share"'
) DO IF NOT %%A.==command. SET dl=%%A

if not exist "%dl%\%computername%" md "%dl%\%computername%"

echo ARP Command
arp -a >> %dl%\%computername%\arp-a.txt

echo NBSTAT Command
nbtstat -c  >> %dl%\%computername%\nbstat.txt

echo Ipconfig Command
ipconfig /all  >> %dl%\%computername%\ipconfig-all.txt

echo Ipconfig DNS Command
ipconfig /displaydns  >> %dl%\%computername%\ipconfig-displaydns.txt

echo Netstat Command
netstat -ano  >> %dl%\%computername%\netstat-ano.txt

echo Tasklist Command
tasklist /v  >> %dl%\%computername%\tasklist.txt

echo LG Admin Command
net localgroup administrators >> %dl%\%computername%\netlocalgroupadmin.txt

echo Directory Command 
dir C:\Windows\Prefetch >> %dl%\%computername%\prefetch.txt

Net use %dl% /delete

Upvotes: 1

Related Questions