windows 不允许 Get-ChildItem 和注册表项访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3997415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:27:42  来源:igfitidea点击:

Get-ChildItem and registry key access not allowed

windowspowershellregistry

提问by rfgamaral

I'm trying to do a simple PowerShell script to access the registry and I'm doing it like this:

我正在尝试执行一个简单的 PowerShell 脚本来访问注册表,我正在这样做:

Foreach ($key in Get-Childitem HKLM:\SYSTEM\CurrentControlSet\Control\Class\"{4D36E972-E325-11CE-BFC1-08002BE10318}") {
    $key.name
}

There's a bunch of keys that are just digits (the ones I want) but then there's one named "Properties" which I don't have access (I don't need to) and that key is giving me the following error executing the Foreach command:

有一堆只是数字的键(我想要的键),但是有一个名为“属性”的键,我无权访问(我不需要),并且该键在执行 Foreach 时给了我以下错误命令:

Foreach ($key in Get-Childitem HKLM:\SYSTEM\CurrentControlSet\Control\Class\"{4D36E972-E325-11CE-BFC1-08002BE10318}") {
    $key.name
}
Get-ChildItem : Requested registry access is not allowed.
At line:3 char:31
+ Foreach ($key in Get-Childitem <<<<  HKLM:\SYSTEM\CurrentControlSet\Control\Class\"{4D36E972-E325-11CE-BFC1-08002BE10318}") {
    + CategoryInfo          : PermissionDenied: (HKEY_LOCAL_MACH...318}\Properties:String) [Get-ChildItem], SecurityException
    + FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.PowerShell.Commands.GetChildItemCommand

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}
$path = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\" +
        "{4D36E972-E325-11CE-BFC1-08002BE10318}"
Get-Childitem $path -ErrorAction SilentlyContinue | Foreach {$_.Name}
00 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}##代码##01 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}##代码##02 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}##代码##03 (...) HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}##代码##24

How can I exclude the "Properties" key and get rid of that error?

如何排除“属性”键并消除该错误?

回答by Keith Hill

If you just don't want to "see" the error then use the -ErrorActionon Get-ChildItem e.g.:

如果您只是不想“看到”错误,请使用-ErrorActionon Get-ChildItem 例如:

##代码##

The SilentlyContinuevalue tells PowerShell not to display this non-terminating error. If you want to have PowerShell actually display the value for this key, you'll have to adjust the perms on the registry key.

SilentlyContinue值告诉 PowerShell 不要显示此非终止错误。如果您想让 PowerShell 实际显示此键的值,则必须调整注册表项上的权限。