PowerShell 技能连载 - 读取注册表键值(临时解决办法)

在前一个技能中我们演示了 Get-ItemProperty 无法读取数据错误的注册表键值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PS> $key =  "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group  Policy\History\{35378EAC-683F-11D2-A89A-00C04FBBCFA2}\0"


PS> Get-ItemProperty -Path $key
Get-ItemProperty : Specified cast is not valid.
At line:1 char:1
+ Get-ItemProperty -Path $key
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ItemProperty], InvalidCastException
+ FullyQualifiedErrorId : System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyComma
nd


PS>

有一个变通办法,您可以使用 Get-Item 代替,来存取注册表键,这将使用它的 .NET 成员来读取所有值:

1
2
3
4
5
6
7
8
9
10
11
$key = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\History\{35378EAC-683F-11D2-A89A-00C04FBBCFA2}\0"

$key = Get-Item -Path $key

$hash = @{}
foreach ($prop in $key.Property)
{
$hash.$prop = $key.GetValue($prop)
}

$hash

结果看起来如下:

1
2
3
4
5
6
7
8
9
10
11
12
Name                           Value
---- -----
Extensions [{35378EAC-683F-11D2-A89A-00C04FBBCFA2}{0F6B957E-509E-11D1-A7CC-0000F87571E3}]
Link Local
Options 0
GPOLink 1
Version 65537
GPOName Guidelines of the local group
lParam 0
DSPath LocalGPO
FileSysPath C:\WINDOWS\System32\GroupPolicy\Machine
DisplayName Guidelines of the local group

本文国际来源:Reading Registry Values (Workaround)

评论