尝试远程使用PsTools(PsExec)在Powershell上返回结果

时间:2019-01-30 21:37:18

标签: powershell psexec

我试图远程运行一个脚本,将仔细检查该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

1 个答案:

答案 0 :(得分:1)

psexec将其状态消息打印到 stderr ,变量$result =不会捕获 ,因此这些消息仍打印到屏幕。

变量分配仅捕获来自psexec之类的外部程序的 stdout 输出,在这种情况下,它们是ipconfig的输出。

因此,答案是抑制stderr ,您可以使用2>$null2是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

希望有帮助。

相关问题