5

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.
)
Clarkey
  • 698
  • 3
  • 11
  • 28
  • Edited this - When checking IF %var% = Value both the %var% and Variable should be surrounded with " ' " (ticks) – rud3y Dec 12 '12 at 18:21
  • Also, just to note, not sure if you tried this yet, but errcode isn't an environement variable, but ERRORLEVEL is. I'm not sure if this is part of a larger script, but if not, try to substitute IF '%ERRORLEVEL%'=='0' (... instead of '%errcode%' – rud3y Dec 12 '12 at 18:24
  • 1
    Shouldn't the sentence be "Is there any way..."? ("IS" instead of "IF") – Jocelyn Dec 12 '12 at 18:41

2 Answers2

6

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
rud3y
  • 2,282
  • 2
  • 21
  • 30
3

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
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • 2
    Method 3 does not work. if ERRORLEVEL 0 echo.Success will correctly echo Success, but is not correct. if not ERRORLEVEL 0 echo.%%A will never occur, because even if your error is 1 or higher the syntax is wrong. – rud3y Dec 12 '12 at 19:17
  • @rud3y Could you be more specific about the issues with my example? What part of the syntax for `if not ERRORLEVEL 0 echo.%%A` wrong because I have used this successfully in many of my scripts. – David Ruhmann Dec 12 '12 at 19:35
  • Sure thing, I made a simple batch file to test this: copy a directory that does not exist to another that does not exist, then echo my errorlevel (it was, as expected, 1) included both of your commands (IF ERRORLEVEL 0 echo.Success) and (IF NOT ERRORLEVEL 0 echo.ERROR) and I consistently got SUCCESS and 1 – rud3y Dec 12 '12 at 19:40
  • 3
    @DavidRuhmann - I also get Success when a failure should be present. –  Dec 12 '12 at 19:44
  • 3
    @rud3y - Good catch, is this specific to operating systems? Are you running Windows 7 as well? (was this different in Windows XP or on Server 2003+ ?) –  Dec 12 '12 at 19:44
  • @Mechanic12386 - Not that I'm aware of. – rud3y Dec 12 '12 at 19:47
  • @DavidRuhmann - David, correct me if I'm wrong, but do you not get the same false positive? – rud3y Dec 12 '12 at 19:48
  • 3
    @rud3y @Mechanic12386 Thanks for the catch; I had not tested my answer. I looked into it some more and it is an issue with ERRORLEVEL. When using `if ERRORLEVEL # do` it is actually performing the following comparison `if ERRORLEVEL >= # do`. However, there is an issue with this depending on the windows version. The recommended method for NT4+ (2000/XP+), due to negative numbers, is to use if `if %ERRORLEVEL% EQU #`. Updated my Answer. Reference: http://www.robvanderwoude.com/errorlevel.php – David Ruhmann Dec 12 '12 at 20:11
  • @Mechanic12386 I was not getting the false positive because I was using it differently than how I posted it. I was using it switched like this `if not ERRORLEVEL 1 goto label1` followed by `if ERRORLEVEL 0 goto label2` which hid the issue in my usage. – David Ruhmann Dec 12 '12 at 20:18