批处理文件 - 捕获PSEXEC结果

时间:2012-12-12 16:30:59

标签: batch-file psexec

如果我运行一个成功的PSEXEC命令,它会说明这一点...... “cmd退出于workstation.domain,错误代码为0。”

有什么方法可以阻止这种情况并做一些像

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

2 个答案:

答案 0 :(得分:6)

由于我的编辑被拒绝了......

原始代码是否是较大脚本的一部分?如果是,那么您是否将errcode设置为与ERRORLEVEL环境变量匹配?

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

每当试图批量确定IF / THEN并使用==时,您需要在单个“'”标记中记录变量和值检查。上面的代码为您纠正了这个问题,并用ERRORLEVEL替换了errcode,ERRORLEVEL是Windows的默认环境变量。

另外,在实践中,我总是在任何ERRORLEVEL检查之前使用以下内容来删除初始值以正确捕获错误。

verify >nul

在这种情况下,我会做以下事情:

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.
    )

我添加了一个weburl来检查收到的错误。

或者,您可以自动打开相应页面的URL:

@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

答案 1 :(得分:3)

参考psexec http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

确定程序结果的第一步是识别所有返回值以及是否设置了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