2009-11-05 114 views

回答

1

在PowerShell中,你可以使用$ LastExitCode变量来测试,如果PSEXEC成功与否例如为:

$results = psexec <some command on remote system> 
if ($LastExitCode -ne 0) { 
    throw "PSExec failed with error code $LastExitCode" 
} 
return 0 
1

在一个批处理文件,使用%ERRORLEVEL%变量,或IF ERRORLEVEL n命令。例如:

psexec \\host -i findstr.exe "test" c:\testfile 
if errorlevel 1 (
    echo A problem occurred 
) 

如果ERRORLEVEL检查返回值是否等于或高于您指定的数值。

这不同于捕获命令的输出。如果你真的想要的输出,你需要包括重定向到命令行上的输出文件:

psexec \\host -i cmd.exe /c findstr "test" c:\testfile ^> c:\output.txt 

的^要躲避>字符,或重定向会在本地发生,而不是在远程计算机上。 cmd.exe是必需的,因为重定向是由cmd处理的。

+0

'%errorlevel%'只是一个伪变量,由shell进行扩展。就像'%time%'','%random%'等等。 – Joey 2009-11-05 22:49:09