The following code works (i.e. returns the expected data from the registry), however it throws a non-terminating error due to a lack of permissions to one of the target key's subkeys, which is benign for this use case.
$adapterKeys = $null
$adapterKeys = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"
$adapters = $null
$adapters = $adapterKeys | Get-ItemProperty
$adapters | Select -First 1
When specifying -ErrorAction "Stop", a terminating error is thrown, but no data is returned:
$adapterKeys = $null
$adapterKeys = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}" -ErrorAction "Stop"
$adapters = $null
$adapters = $adapterKeys | Get-ItemProperty
$adapters | Select -First 1
So I can catch the error with ErrorAction "Stop" and try{} catch{}, but this defeats the purpose since the data is no longer returned.
So with these constraints, how can I return the necessary data, but also catch and ignore the error? Note that I don't want to ignore ALL errors, otherwise I would just use -ErrorAction "SilentlyContinue".
Is this expected behavior for Get-ChildItem and/or -ErrorAction "Stop"? I don't recall ever running into such an issue before.
Just for reference, the error thrown is:
Get-ChildItem : Requested registry access is not allowed.
At line:2 char:16
+ … apterKeys = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Control\Cla …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (HKEY_LOCAL_MACHINE\…BE10318}\Properties:String) [Get-ChildItem], SecurityException
+ FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.PowerShell.Commands.GetChildItemCommand
This is due to the Properties subkey of the target registry key being permissioned such that it is inaccessible to even local administrators. As noted, this is irrelevant for this use case because I don't need the data from this Properties subkey; only from the other subkeys.
One workaround would be to use Get-ItemProperty directly on all of the target subkeys, thus never encountering the Properties key or this specific error at all. However I cannot know how many subkeys there will be, and while they likely will always have a consistent, numerical naming convention, I would like to avoid making these assumptions. Also this question is more about what I'm misunderstanding about the behavior of Get-ChildItem and/or terminating errors.
Edit: some related questions and info I've come across:
- This question suggests using
-ErrorAction "SilentlyContinue"and-ErrorVariablefor post-processing errors, however this doesn't make a lot of sense to me. - This question suggests that there is a poorly-documented "semi-terminating" error type, though I suspect that's not applicable here. Also discusses the
trapkeyword, which I've never heard of, nor had a use for before.
FWIW, my target environment is Windows PowerShell 5.1, although I have the same issue on PowerShell 7+, and on the latest versions of both Win10 and Win11.
try { ... } catch { ... }(and also by the obsolescenttrapstatement). This GitHub docs issue tries to provide a comprehensive overview of PowerShell's - bewilderingly complex - error handling.