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

Trying to Remotely use PsTools (PsExec) to Return a Result on Powershell

我正在尝试 运行 一个远程脚本,该脚本将使用 Powershell 中的 PsExec 仔细检查 IP 地址是否正确。问题是我只希望它 return 结果为 True 或 False,而不显示 Powershell 中的任何其他行。

我也尝试过 运行ning 后台作业,但似乎没有成功,因为当我这样做时,它什么也没有给我。

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

在 运行 之后,我只希望应用程序提供 return:

True

而不是 returning:

Starting cmd on MUC-1800035974... MUC-1800035974...
cmd exited on MUC-1800035974 with error code 0.
True

psexec 将其状态消息打印到 stderr$result = 之类的变量赋值 而不是 捕获,因此这些消息仍会打印到屏幕上。

变量赋值只捕获stdout来自外部程序的输出,例如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

希望对您有所帮助。