代码之家  ›  专栏  ›  技术社区  ›  rad_

Powershell-检查foreach中的变量值-如果没有值,则记录其他输出

  •  0
  • rad_  · 技术社区  · 7 年前

    $UninstallKeys = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
    foreach($Key in $UninstallKeys){
    
    if($Key.GetValue("DisplayName") -Match "BeyondTrust"){
    
        $PBW = $Key.GetValue("DisplayName")
        $PBWV = $Key.GetValue("DisplayVersion")
    
    
            if ($PBW) {
    
                $PBW = $PBW, $PBWV
    
            }
            else {
    
                $PBW = "PowerBroker not installed."
                $installsmissing = "True"
            }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   gvee    7 年前

    $displayName = "BeyondTrust"
    
    $uninstallKeys = Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    
    # Filter the keys down by their display name property
    $specificUninstallKeys = $uninstallKeys |
        Where-Object {
            $_.GetValue("DisplayName") -eq $displayName
        }
    
    # Did we find any keys of that name?
    if ($specificUninstallKeys) {
        Write-Output "Keys found: $($specificUninstallKeys.Length)"
    }
    else {
        Write-Output "Sorry pal, no keys by that name here!"
    }
    
    # There may be more than one; hence the loop-y requirement here.
    foreach ($specificUninstallKey in $specificUninstallKey) {
        Write-Output $displayName
        Write-Output $specificUninstallKey.GetValue("DisplayVersion")
    }
    
    推荐文章