5

如果我成功运行 PSEXEC 命令,它会显示...
“cmd 在工作站上退出,错误代码为 0。”

有什么办法可以防止这种情况并做类似的事情

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

2 回答 2

6

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

发布的原始代码是更大脚本的一部分吗?如果是这样,那么您是否将您的错误代码设置为与 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 并使用 == 时,您需要将变量和 valuecheck 放在单个“'”标记中。上面的代码为您解决了这个问题,并将 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
于 2012-12-12T19:13:22.373 回答
3

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

确定程序结果的第一步是确定所有返回值以及是否设置了错误级别。

@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
于 2012-12-12T18:29:35.913 回答