最新的strictmode找不到Powershell对象 在严格模式下访问不存在的属性时避免错误:

问题描述

我正试图在set-strictmode -version latest下工作,它在没有严格模式的情况下完全可以正常工作,很遗憾,这是在我的环境中拥有最新严格模式的要求。

功能:通过注册表查找具有EEELinkAdvertisement的条目,并将其分配给$ findEeeLinkAd

$findEeeLinkAd = Get-ChildItem -LiteralPath 'hklm:\SYstem\ControlSet001\Control\Class' -Recurse -ErrorAction SilentlyContinue | `
   % {Get-ItemProperty -Path $_.pspath -ErrorAction SilentlyContinue | `
  ? {$_.EeeLinkAdvertisement} -ErrorAction SilentlyContinue }

尽管在Administrator中运行,我仍然收到以下错误消息:

The property 'EEELinkAdvertisement' cannot be found on this object. Verify that the property exists.
At line:3 char:12
+         ? {$_.EEELinkAdvertisement} }
+            ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [],PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

任何帮助将不胜感激!

解决方法

EeeLinkAdvertisement是您抓取的许多物品的未定义属性,因此这很正常。 你可以这样作弊:

Get-ChildItem -LiteralPath 'hklm:\SYSTEM\ControlSet001\Control\Class' -Recurse -ErrorAction SilentlyContinue | `
   % {Get-ItemProperty -Path $_.pspath -ErrorAction SilentlyContinue | `
  ? {try{$_.EeeLinkAdvertisement}catch{}} -ErrorAction SilentlyContinue }
,

注意

  • 除非每次您升级到新的PowerShell版本时都准备好全面测试以及每个现有脚本/函数/ PowerShell编写的每个模块,否则我建议避免使用
    Set-StrictMode -Version Latest在生产代码中
    -在开发过程中很好,但是在发布代码时,应明确指定当时的最高版本,然后将其锁定。否则,在更高的PowerShell版本中引入新的严格性检查时,现有代码可能会中断。

  • 底部显示了如何一般处理在严格模式有效的情况下访问不存在的属性的尝试。

您可以按以下方式简化代码,这隐式地绕过尝试访问所有输入对象中不存在的属性的问题:

$rootKey = 'hklm:\SYSTEM\ControlSet001\Control\Class'

# Find all keys that have a 'EeeLinkAdvertisement' value.
$findEeeLinkAd = 
  Get-ChildItem -LiteralPath $rootKey -Recurse -ErrorAction Ignore | 
    ForEach-Object { if ($null -ne $_.GetValue('EeeLinkAdvertisement')) { $_ } }

请注意从-ErrorAction SilentlyContinue-ErrorAction Ignore的切换:后者悄悄地丢弃任何错误,而前者不显示任何错误,但仍将它们记录在自动$Error集合中。

这利用了Microsoft.Win32.RegistryKey.GetValue()方法悄悄地忽略尝试检索不存在的值的数据并返回$null的事实。

顺便说一句:将common -ErrorAction parameter应用于Where-Object?)和ForEach-Object%)cmdlet实际上是没有意义的,因为参数 not 不适用于传递给这些命令的脚本块({ ... })中运行的代码。


在严格模式下访问不存在的属性时避免错误:

Set-StrictMode具有-Version 2或更高版本(包括Latest)会导致尝试访问对象上不存在的属性以报告语句终止错误。 / p>

  • 请注意,这意味着默认情况下,仅手头的语句被终止,而总体脚本执行则继续

  • 还要注意,-Off是默认值;也就是说,默认情况下执行 no 严格性检查,这意味着即使尝试引用不存在的变量 也会默认情况下 not 触发错误; Set-StrictMode -Version 1检查不存在的变量,但不检查不存在的 properties

有几种避免此类错误的方法:

  • 在资源访问权限周围使用Try / Catch statement ,如CFou's answer所示;这也使得在没有属性的情况下指定默认值很容易,但是与下面基于反射的方法相比,捕获异常的速度是 slow

    $o = [pscustomobject] @{ foo = 1 }
    
    $propValue = try { $o.NoSuchProperty } catch { 'default value' }
    
  • 暂时子范围中禁用严格模式(这与try / { {1}}方法):

    catch
  • 通过所有对象上可用的隐藏$o = [pscustomobject] @{ foo = 1 } # $propValue will effectively receive $null. # Strictly speaking: [System.Management.Automation.Internal.AutomationNull]::Value $propValue = & { Set-StrictMode -Off; $o.NoSuchProperty } 成员,使用 reflection 来测试属性的存在;这是最快方法:

    .psobject.Properties

PowerShell [Core] 7.1 + 中,您可以通过null-conditional operator ?.来简化操作:

$o = [pscustomobject] @{ foo = 1 }

$propValue = if ($o.psobject.Properties['NoSuchProperty']) { $o.NoSuchProperty }
  • 自v7.1起的陷阱:如果将$o = [pscustomobject] @{ foo = 1 } # Note the `?.`,which only tries to access the property if the expression # to the left isn't $null. $propValue = $o.psobject.Properties['NoSuchProperty']?.Value 直接应用于变量,则必须意外地将其名称包含在?.中,例如 {...} ${var}?.Property不能按预期工作,因为PowerShell会假定变量名称为$var?.Property-请参见GitHub issue #11379来尝试更改它。

类似地,null-coalescing operator,??(在v7.0中已提供)可以简化提供 default 值(在早期版本中,您可以通过添加var?来实现)跳转到上面的else语句):

if