我的$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*"}
答案 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*"}