在 Powershell 2.0 中直接引用属性
Directly Referencing Properties in Powershell 2.0
我在今天早上写的脚本中遇到了一个错误,我没有从我的 Select-String 表达式中获得输出。玩了一会儿后,我意识到这个表达式不会 return 我在 v2.0 中匹配的值,但会在我最初编写它的 v4.0 中。
($log | Select-String "\[CIsoCreator\] Creating iso file" -AllMatches | Select-Object -ExpandProperty line -Last 1 | Select-String "([A-Z]\:)(.*\)*.*\.iso").matches.value
在尝试了一些事情之后,我最终得到了这个 return 符合预期的效果。
($log | Select-String "\[CIsoCreator\] Creating iso file" -AllMatches | Select-Object -ExpandProperty line -Last 1 | Select-String "([A-Z]\:)(.*\)*.*\.iso").matches | select -expandproperty value
在我看来,v2.0 中有一些不同的规则来管理您何时可以直接引用属性,但我一直找不到相关内容。
有没有人知道这在不同版本之间是如何工作的?
这是由于 PowerShell 版本 3.0 中引入的语言行为发生了变化 - 来自 "What's new in PowerShell 3.0" release notes:
Windows PowerShell Language Enhancements
Windows PowerShell 3.0 includes many features that are designed to make its language simpler,
easier to use, and to avoid common errors. The improvements include
property enumeration, count and length properties on scalar objects,
new redirection operators, the $Using scope modifier, PSItem automatic
variable, flexible script formatting, attributes of variables,
simplified attribute arguments, numeric command names, the
Stop-Parsing operator, improved array splatting, new bit operators,
ordered dictionaries, PSCustomObject casting, and improved
comment-based help.
(我加的重点)
属性 枚举允许 .
引用运算符解析数组表达式的各个成员的属性,即使数组本身没有这样的 属性:
$Things = 1..3 |%{ New-Object psobject -Property @{Prop = $_} }
$Things.Prop # Starting with version 3.0, this outputs the array 1,2,3
# In PowerShell version 2.0, this will throw an error
# because [Object[]] ($Things) has no such property
我在今天早上写的脚本中遇到了一个错误,我没有从我的 Select-String 表达式中获得输出。玩了一会儿后,我意识到这个表达式不会 return 我在 v2.0 中匹配的值,但会在我最初编写它的 v4.0 中。
($log | Select-String "\[CIsoCreator\] Creating iso file" -AllMatches | Select-Object -ExpandProperty line -Last 1 | Select-String "([A-Z]\:)(.*\)*.*\.iso").matches.value
在尝试了一些事情之后,我最终得到了这个 return 符合预期的效果。
($log | Select-String "\[CIsoCreator\] Creating iso file" -AllMatches | Select-Object -ExpandProperty line -Last 1 | Select-String "([A-Z]\:)(.*\)*.*\.iso").matches | select -expandproperty value
在我看来,v2.0 中有一些不同的规则来管理您何时可以直接引用属性,但我一直找不到相关内容。
有没有人知道这在不同版本之间是如何工作的?
这是由于 PowerShell 版本 3.0 中引入的语言行为发生了变化 - 来自 "What's new in PowerShell 3.0" release notes:
Windows PowerShell Language Enhancements
Windows PowerShell 3.0 includes many features that are designed to make its language simpler, easier to use, and to avoid common errors. The improvements include property enumeration, count and length properties on scalar objects, new redirection operators, the $Using scope modifier, PSItem automatic variable, flexible script formatting, attributes of variables, simplified attribute arguments, numeric command names, the Stop-Parsing operator, improved array splatting, new bit operators, ordered dictionaries, PSCustomObject casting, and improved comment-based help.
(我加的重点)
属性 枚举允许 .
引用运算符解析数组表达式的各个成员的属性,即使数组本身没有这样的 属性:
$Things = 1..3 |%{ New-Object psobject -Property @{Prop = $_} }
$Things.Prop # Starting with version 3.0, this outputs the array 1,2,3
# In PowerShell version 2.0, this will throw an error
# because [Object[]] ($Things) has no such property