Where-object传递变量不适用于块脚本

时间:2017-01-23 10:32:09

标签: powershell

我的$var变量无法在远程计算机上运行。我使用此链接In powershell passing variable to where-object not working中的过滤器。但我的剧本仍然无法从我的输入中找到应用程序。

$var = "application" 

Invoke-command -ComputerName $cpu {
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* ,
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object {$_.DisplayName -like "*$var*"}

1 个答案:

答案 0 :(得分:5)

根据您的PowerShell版本,您必须在变量上使用$using:前缀,或者必须使用-ArgumentList参数传递变量。这是一个例子:

Invoke-command -ComputerName $cpu {
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* ,
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object {$_.DisplayName -like "*$using:var*"}