Batch File - Catching PSEXEC result

20,549

Solution 1

Since my edit's were rejected...

Is the original code posted part of a larger script? If so then do you set your errcode to match the ERRORLEVEL environment variable?

psexec \\workstation.domain -u username -p password cmd /c "assoc.pdf= "
IF '%ERRORLEVEL%'=='0' (
  echo Success!
) else (
  REM display psexec error here.
)

Whenever attempting to detemine IF / THEN in batch and you use == you need to surrond the variable and the valuecheck in single " ' " marks. The above code corrects that issue for you as well as replaces errcode with ERRORLEVEL, which is the default environment variable for Windows.

Also, in practice I always use the following before any ERRORLEVEL check to drop the initial value to properly catch the error.

verify >nul

In this case I would do the following:

verify >nul
psexec \\workstation.domain -u username -p password cmd /c "assoc.pdf= "
    IF '%ERRORLEVEL%'=='0' (
      echo Success!
    ) else (
      echo.Error is %ERRORLEVEL%; please see http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx for more details.
    )

I added a weburl for checking on the error received.

Alternatively you could open the URL to the corresponding page automatically:

@ECHO OFF
    verify >nul
    set ERRCODE=0
    psexec \\workstation.domain -u username -p password cmd /c "assoc.pdf= "
        IF '%ERRORLEVEL%'=='0' (
          echo Success!
        ) else (
          set ERRCODE=%ERRORLEVEL%
        )
IF %ERRCODE% LEQ 499 set MSERROR=681382
IF %ERRCODE% GTR 500 set MSERROR=681388
IF %ERRCODE% GTR 1000 set MSERROR=681383
IF %ERRCODE% GTR 1300 set MSERROR=681385
IF %ERRCODE% GTR 1700 set MSERROR=681386
IF %ERRCODE% GTR 4000 set MSERROR=681387
IF %ERRCODE% GTR 6000 set MSERROR=681389
IF %ERRCODE% GTR 8200 set MSERROR=681390
IF %ERRCODE% GTR 9000 set MSERROR=681391
IF %ERRCODE% GTR 12000 set MSERROR=681384

IF ERRCODE NEQ 0 start http://msdn.microsoft.com/en-us/library/ms%MSERROR%(v=vs.85).aspx
IF ERRCODE NEQ 0 echo.This failed with ERROR: %ERRCODE%
pause

Solution 2

For reference to psexec http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

The first step when determining program results is to identify all of the return values and if it sets errorlevel.

@echo off

:: Method 1, Handle a single line of output. No errorlevel support
for /f "usebackq delims=" %%A in (`psexec \\workstation.domain -u username -p password cmd /c "assoc.pdf= " ^| find /v "error code 0"`) do (
    rem Display the error
    echo.%%A
    goto Failed
)
echo.Success
:Failed


:: Method 2, Handle multiple lines of output. No errorlevel support
for /f "usebackq delims=" %%A in (`psexec \\workstation.domain -u username -p password cmd /c "assoc.pdf= "`) do (
    rem Check the status
    for /f "usebackq delims=" %%X in (`echo."%%~A" ^| find /v "error code 0"`) do (
        echo.%%X
    )
    for /f "usebackq delims=" %%X in (`echo."%%~A" ^| find "error code 0"`) do (
        echo.Success
    )
)


:: Method 3, Supports error level variable; only works if the called program supports it.
verify > nul
psexec \\workstation.domain -u username -p password cmd /c "assoc.pdf= "> nul
if %ERRORLEVEL% EQU 0 echo.Success
if %ERRORLEVEL% NEQ 0 echo.Error


:: Method 4, specific error message with error level, requires delayed expansion.
setlocal enabledelayedexpansion
verify > nul
for /f "usebackq delims=" %%A in (`psexec \\workstation.domain -u username -p password cmd /c "assoc.pdf= "`) do (
    if !ERRORLEVEL! EQU 0 echo.Success
    if !ERRORLEVEL! NEQ 0 echo.%%A
)
endlocal

pause
Share:
20,549
Clarkey
Author by

Clarkey

Updated on May 02, 2020

Comments

  • Clarkey
    Clarkey about 4 years

    If I run a successful PSEXEC command, it says this...
    "cmd exited on workstation.domain with error code 0."

    Is there any way I can prevent this and do something like

    psexec \\workstation.domain -u username -p password cmd /c "assoc.pdf= "
    if %errorlevel%==0 (
      echo Success!
    ) else (
      REM display psexec error here.
    )