简体   繁体   中英

Get-ChildItem and registry key access not allowed

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

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 ($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}\0000
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0001
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0002
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0003
(...)
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0024

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

If you just don't want to "see" the error then use the -ErrorAction on Get-ChildItem eg:

$path = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\" +
        "{4D36E972-E325-11CE-BFC1-08002BE10318}"
Get-Childitem $path -ErrorAction SilentlyContinue | Foreach {$_.Name}

The SilentlyContinue value 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM