我正在尝试远程运行一个脚本,该脚本将在Powershell中使用PsExec,再次检查IP地址是否正确。问题是我只希望它返回结果真或假,而不显示Powershell中的任何其他行。
我也尝试过运行后台作业,但似乎没有做到这一点,因为当我这样做时,它只是给了我什么。
function remoteIPTest($Computer) {
$result = & cmd /c PsExec64.exe \\$Computer -s cmd /c "ipconfig"
if ($result -like "*10.218.5.202*") {
return "True"
}
}
$Computer = "MUC-1800035974"
remoteIPTest $Computer运行此程序后,我只希望应用程序返回:
True而不是返回:
Starting cmd on MUC-1800035974... MUC-1800035974...
cmd exited on MUC-1800035974 with error code 0.
True发布于 2019-01-31 06:23:21
psexec将其状态消息打印到stderr,而像$result =这样的变量赋值并不捕获这些状态消息,因此这些消息仍然打印到屏幕上。
变量赋值只捕获来自外部程序(如psexec )的标准输出,在本例中是ipconfig的输出。
因此,答案是抑制stderr,您可以使用2>$null (2是PowerShell的错误流( stderr映射到的)的数量)-参见Redirecting Error/Output to NULL。
请注意,这也将抑制真正的错误消息。
此外,不需要cmd /c调用,因为如果路径配置正确,可以使用psexec直接调用其他程序。
而不是这个:
$result = & cmd /c PsExec64.exe \\$Computer -s cmd /c "ipconfig"这样做:
$result = PsExec64.exe \\$Computer -s ipconfig 2>$null希望能帮上忙。
https://stackoverflow.com/questions/54449931
复制相似问题