在以下脚本中:
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where {$_.DisplayName -like '*Visual*' } | Select-Object -ExpandProperty DisplayName
$productName="Visual"
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where {$_.DisplayName -like '*$productName*' } | Select-Object -ExpandProperty DisplayName
第一个Get-ItemProperty返回正确的结果,第二个不返回任何内容。
我尝试使用ScriptBlock:
[ScriptBlock]$whereClause = [ScriptBlock]::Create("$_.DisplayName -like '*$productName*'")
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | where $whereClause | Select-Object -ExpandProperty DisplayName
但是错误与"术语' .DisplayName'不被识别为cmdlet的名称...."
我尝试了其他各种变体,但我似乎无法让它发挥作用,而且我不确定我错过了什么。我希望能够在Where-Object cmdlet中使用参数变量。我该怎么做?
答案 0 :(得分:0)
您正在尝试将字符串插值与字符串常量一起使用:
'*$productName*'
是一个字符串常量。要在powershell中使用字符串插值,您需要使用双引号:
"*$productName*"