gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:52:56 28 4
gpt4 key购买 nike

我正在尝试远程运行脚本,它将使用Powershell中的PsExec再次检查IP地址是否正确。问题是我只希望它返回结果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)的stdout输出,在本例中为ipconfig的输出。

因此,答案是抑制stderr,您可以使用2>$null来做到这一点(2是stderr映射到的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

希望能帮助到你。

关于powershell - 尝试远程使用PsTools(PsExec)在Powershell上返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54449931/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com