使用Powershell检索注册表值时出错

Mat*_*ttH 4 registry powershell

我正在尝试使用Powershell从注册表项读取值.这很简单,但是,一个特定的注册表项给我带来了麻烦.

如果我运行以下命令,我无法获得(默认)"$ setting"的值.

C:\Program Files\PowerGUI> $setting = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping\Autorun.inf" 

C:\Program Files\PowerGUI> $setting


PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\
               CurrentVersion\IniFileMapping\Autorun.inf
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\
               CurrentVersion\IniFileMapping
PSChildName  : Autorun.inf
PSDrive      : HKLM
PSProvider   : Microsoft.PowerShell.Core\Registry
(default)    : @SYS:DoesNotExist
Run Code Online (Sandbox Code Playgroud)

通常,我会做$ setting.Attribute,或$ setting.(默认).但是,这会导致以下错误:

C:\Program Files\PowerGUI> $setting.(default)
The term 'default' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At :line:1 char:17
+ $setting.(default <<<< )
Run Code Online (Sandbox Code Playgroud)

如何获取"(默认)"属性的值?

提前致谢.

Jar*_*Par 8

编辑不得不通过和旧脚本来解决这个问题.

诀窍是你需要查看底层的PSObject来获取值.特别是看属性包

$a = get-itemproperty -path "HKLM:\Some\Path"
$default = $a.psobject.Properties | ?{ $_.Name -eq "(default)" }
Run Code Online (Sandbox Code Playgroud)

您也可以使用索引器而不是使用过滤器技巧

$default = $a.psobject.Properties["(default)"].Value;
Run Code Online (Sandbox Code Playgroud)

  • 使用$ setting.'(默认)'来转义括号也有效. (2认同)