我正在使用 -Match "$software" 在列表中搜索 $software。我还想将 $software 写入输出到屏幕上。
对于 Notepad++,我必须转义第二个 +。我尝试过
$software=Notepad+{backtick}+
以下错误:parsing "Notepad++" - Nested quantifier +.
Notepad+\\+
这不会出错,但会使用 Write-Output 写入 Notepad+\\+
完整代码
$software="Notepad++"
Check-if-Installed $software
Function Check-if-Installed{
param (
[parameter(Mandatory=$true)]$software_to_check
)
Write-Output "Checking if $software_to_check is installed"
if ((gp HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*).DisplayName -Match "$software_to_check" -or
(gp HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*).DisplayName -Match "$software_to_check")
{Write-Output "$software_to_check Installed"
} else {Write-Output "$software_to_check Not Installed"}
}
答案1
你可以写类似的东西:
Function Replace-SpecialChars {
param($InputString)
$SpecialChars = '[+]'
$Replacement = '\+'
$InputString -replace $SpecialChars,$Replacement
}
Function Check-if-Installed {
param ([parameter(Mandatory=$true)]$software_to_check)
Write-Host "Checking if $software_to_check is installed"
$OS_Architecture = $env:PROCESSOR_ARCHITECTURE
if($OS_Architecture -eq 'x86')
{
#32-bit
$key = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
}
else
{
#64-bit
$key = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
}
$software = Replace-SpecialChars -InputString "$software_to_check"
If((Get-ItemProperty $Key).DisplayName -Match "$software") {
Write-Host "$software_to_check is Installed" -Fore Green
}
else
{ Write-Host "$software_to_check is not Installed" -Fore Red}
}
$software="Notepad++"
Check-if-Installed $software
# If you want to check within array :
$Software_Array = @("Notepad++","firefox","chrome","Bitdefender","Powershell","7-zip","winrar","winzip","Hackoo")
ForEach ($Soft in $Software_Array) {
Check-if-Installed $soft
}
答案2
该-Match
运算符使用正则表达式。Write-Output
采用文字或扩展字符串。
Escape
类的方法将Regex
返回一个所有特殊字符都已转义的字符串。使用此字符串进行匹配操作。
$software = 'Notepad++'
Function Check-if-Installed{
param (
[parameter(Mandatory=$true)]$software_to_check
)
$UnInstall64 = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'
$UnInstall32 = 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
Write-Output "Checking if $software_to_check is installed"
$MatchString = [Regex]::Escape($software_to_check)
If (( gp $UnInstall64 ).DisplayName -Match $MatchString -or
( gp $UnInstall32 ).DisplayName -Match $MatchString )
{
Write-Output "$software_to_check Installed"
}
else
{
Write-Output "$software_to_check Not Installed"
}
}
# In PowerShell, you have to define a fucdtion before you refence it.
Check-if-Installed $software
附言
虽然通配符在路径中很容易使用,但它们似乎不是处理注册表路径的最有效方法。尝试以下代码来测试获取相同数据的三种不同方法:
$UnInstall64 = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'
$UnInstall64B = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
Measure-Command { (gp $Uninstall64).DisplayName }
Measure-Command { (gci $Uninstall64B | gp).DisplayName }
Measure-Command { (gci $Uninstall64B).ForEach({ $_.GetValue('DisplayName') }) }
在这种情况下可能微不足道,但在挖掘类似的东西时可能会产生很大的不同HKCR\CLSID\...
。