0

when the script is running, when it finds a machine to be unreachable I want it to skip it. and do psexec command for the online PCs only

i created a bat file as below but it's not working

 @echo off
setlocal
:begin
for /f %%a in (computerlist.txt) do (
    ping -n 1 %%a >NUL 2>NUL
    if %errorlevel%==0 (
        psexec \\%%a -u user -p password -i -d "d:\path\command.exe"
    ) else echo Skipping unreachable host %%a
)
endlocal

2 Answers2

1
wmic /node:"@computerlist.txt" /failfast:on process call create "c:\\windows\\notepad.exe"

It doesn't wait for computers that don't answer quickly. Notepad will be invisible on remote machine but not a local machine.

Trigger
  • 114
  • 1
0

You need to add Setlocal EnableDelayedExpansion to your code and access the errorlevel as !errorlevel!:

@echo off
Setlocal EnableDelayedExpansion
for /f %%a in (computerlist.txt) do (
    ping -n 1 %%a >NUL 2>NUL
    if !errorlevel!==0 (
        psexec \\%%a -u user -p password -i -d "d:\path\command.exe"
    ) else echo Skipping unreachable host %%a
)

The problem is that usually variables are being evaluated at parse time. So if you use %errorlevel% it will always contain the first set value. EnableDelayedExpansion will force evaluation at execution time instead so !errorlevel! will always contain the last assigned value (while %errorlevel% will still contain the first one). In your case the variable might change on each iteration of the loop so you obviously need !errorlevel!.

For more information check http://ss64.com/nt/delayedexpansion.html.

MichaelS
  • 5,941
  • 6
  • 31
  • 46