Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch File - Catching PSEXEC result

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.
)
like image 276
Clarkey Avatar asked Dec 12 '12 16:12

Clarkey


1 Answers

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
like image 182
rud3y Avatar answered Oct 26 '22 17:10

rud3y