在Powershell中搜索注册表以获取这些键中的特定键和值

时间:2016-06-28 07:01:35

标签: powershell registry patch

我非常接近解决方案,但我无法实现目标。我想要做的是搜索已安装的MS Office更新。我发现的最好方法是搜索HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall密钥。

我当时想要做的只是查看像*{90140000-001*(表示Office)这样的子键,以及搜索每个子键的"的显示名称属性;(KB *& #34; - 这表明它是Office的更新,而不是组件。

到目前为止我所拥有的是:

$ErrorActionPreference = "SilentlyContinue"
Get-ChildItem "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" -Recurse | Where-Object{$_ -like "*{90140000-001*"} | foreach {
Get-ItemProperty $_.DisplayName}

但它会产生空白输出。

有人能够帮我完成这件事吗?

2 个答案:

答案 0 :(得分:1)

您可以使用当前GetValue的{​​{1}}方法检索DisplayName:

RegistryKey

答案 1 :(得分:1)

如果要查询远程计算机:

#config
$computerName = $env:COMPUTERNAME
$hive = "LocalMachine"

#32-bit Office : SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
#64-bit Office : SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
#Office 2013 : look for keys named "{90140000-001*"
#Office 2016 : look for keys named "{90160000-001*"

$regPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"

#if remote computer is reachable
if(Test-Connection $computerName -Quiet -Count 2 -ErrorAction 0) {
    try {
        #open remote registry
        $base = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::$hive, $ComputerName)

        #open desired key with edit permission
        $key = $base.OpenSubKey($regPath)

        foreach($subkeyName in $key.GetSubKeyNames()) {
            if($subkeyName -match "{90160000-001") {
                $subkey = $key.OpenSubKey($subkeyName)
                $displayName = $subkey.GetValue("DisplayName")
                if($displayName -match "\(KB") {
                    $displayName
                }
            }
        }

        #close subkey, key and registry connection
        $subkey.Close()
        $key.Close()
        $base.Close()
    } catch {
        Throw "Remote registry is not accessible (check `$hive and `$regPath, and run this script as administrator)."            
    }
} else {
    Throw "Remote computer is not reachable."
}