You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

远程GCI命令结果过滤异常问题求助

这问题我碰到过好几次啦!核心原因是你本地定义的$softwareName变量并没有传递到远程计算机的会话里,远程脚本块里的$softwareName其实是未定义的,所以Where-Object的过滤条件相当于匹配所有内容,自然就返回了所有程序的UninstallString。下面给你两种靠谱的解决办法:

解决方案1:使用Using:作用域(PowerShell 3.0及以上推荐)

直接在远程脚本块里的变量前加上Using:前缀,让远程会话能引用本地的变量值:

$computer="Test1" 
$softwareName="Solarwinds" 
$uninstall32=Invoke-Command -ComputerName $computer -ScriptBlock {
    Get-ChildItem "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" | 
    Get-ItemProperty | 
    Where-Object {$_.DisplayName -like "*$Using:softwareName*"} | 
    Select UninstallString
}

$Using:softwareName会告诉PowerShell把本地的$softwareName值传递到远程会话中,这样过滤条件就能精准匹配你指定的软件名称了。

解决方案2:通过-ArgumentList传递参数

如果你的PowerShell版本比较旧(低于3.0),可以用参数传递的方式实现:

$computer="Test1" 
$softwareName="Solarwinds" 
$uninstall32=Invoke-Command -ComputerName $computer -ScriptBlock {
    param($targetSoftware)
    Get-ChildItem "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" | 
    Get-ItemProperty | 
    Where-Object {$_.DisplayName -like "*$targetSoftware*"} | 
    Select UninstallString
} -ArgumentList $softwareName

先在脚本块里用param()定义接收参数$targetSoftware,再通过-ArgumentList把本地的$softwareName值传递过去,远程脚本块就能用这个参数完成过滤。

运行修改后的命令,就能得到远程机器上仅包含Solarwinds的程序UninstallString,和你本地运行的效果完全一致啦!

内容的提问来源于stack exchange,提问作者Jason Williams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:57:03