2016-12-27 254 views
2

試圖通過使用DisplayName值的匹配進行搜索來掃描某些軟件的註冊表的卸載字符串。我可以通過一個軟件來實現它,但是我有麻煩找出如何使它與軟件列表一起工作。例如,我將Java,Silverlight,Shockwave,Adobe Reader和Adobe Font Pack設置爲變量,但無法弄清楚如何通過foreach命令循環它們。循環內循環?

$PATHS = @("HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall", 
      "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall") 

# Java 8 Update 102 | 8.0.1020.14 
$Javax86 = "Java 8" 

# Microsoft Silverlight | 5.1.50901.0 
$MSsilverlight = "Microsoft Silverlight" 

# Adobe Shockwave Player 12.2 | 12.2.5.195 
$AdobeShockwave = "Adobe Shockwave Player" 

# Extended Asian Language font pack for Adobe Acrobat Reader DC | 15.007.20033 
$AdobeReaderFont = "font pack for Adobe Acrobat Reader" 

# Adobe Acrobat Reader DC MUI | 15.020.20042 
$AdobeReader = "Adobe Acrobat Reader DC MUI" 

$SOFTWARE = "" 
#@($Javax86,$MSsilverlight,$AdobeShockwave,$AdobeReaderFont,$AdobeReader) 

foreach ($path in $PATHS) { 
    $installed = Get-ChildItem -Path $path | 
       foreach { Get-ItemProperty $_.PSPath } | 
       Where-Object { $_.DisplayName -match "$MSsilverlight" } | 
       Select-Object -Property DisplayName,DisplayVersion 

    foreach ($app in $installed) { 
     $DisplayName = "$($app.DisplayName)" 
     $DisplayVersion = "$($app.DisplayVersion)" 
    } 

    if ($app.DisplayName) { 
     $InstalledYes = "$DisplayName ($DisplayVersion) is installed" 
    } elseif ($app.DisplayName -eq $NULL) { 
     $InstalledNo = "$MSsilverlight is not Installed" 
     #Write-Host "$SOFTWARE is not installed" 
    } 
} 

Write-Host "$InstalledYes" 
Write-Host "$InstalledNo" 

回答

3

當您使用-match運算符時,可以使用正則表達式OR運算符。而不是建立一個數組,使用管道分隔字符串:

$SOFTWARE = "$Javax86|$MSsilverlight|$AdobeShockwave|$AdobeReaderFont|$AdobeReader" 

ForEach ($path in $PATHS) { 

    $installed = Get-ChildItem -Path $path | 
       ForEach { Get-ItemProperty $_.PSPath } | 
       Where-Object { $_.DisplayName -match $SOFTWARE } | 
       Select-Object -Property DisplayName,DisplayVersion 

    ForEach ($app in $installed) { 
     $DisplayName = "$($app.DisplayName)" 
     $DisplayVersion = "$($app.DisplayVersion)" 

    } 

    .... 
+2

你不需要管道後面的刻度線。請不要使用刻度線(永遠)繼續行。 –