我试图远程运行一个脚本,将仔细检查IP地址是正确的PowerShell中使用PSEXEC。问题是,我只希望它返回的结果true或false不显示在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
psexec
打印其状态信息发送到stderr,其中一个变量赋值,如$result =
不捕获,因此这些消息仍将输出到屏幕上。
变量赋值只捕获来自外部程序如psexec
,在这种情况下是ipconfig
的输出标准输出输出。
因此,答案是抑制标准错误,您可以用2>$null
做(2
是PowerShell的错误流,其中标准错误映射到数) - 见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
希望能帮助到你。