直接引用Powershell 2.0中的属性
我在今天早上编写的脚本中遇到了一个bug,我没有从Select字符串表达式中获得输出。玩了一段时间后,我意识到这个表达式在v2.0中不会返回匹配值,但在我最初编写它的v4.0中会返回直接引用Powershell 2.0中的属性,powershell,powershell-2.0,powershell-4.0,Powershell,Powershell 2.0,Powershell 4.0,我在今天早上编写的脚本中遇到了一个bug,我没有从Select字符串表达式中获得输出。玩了一段时间后,我意识到这个表达式在v2.0中不会返回匹配值,但在我最初编写它的v4.0中会返回 ($log | Select-String "\[CIsoCreator\] Creating iso file" -AllMatches | Select-Object -ExpandProperty line -Last 1 | Select-String "([A-Z]\:)(.*\\)*.*\.iso").
($log | Select-String "\[CIsoCreator\] Creating iso file" -AllMatches | Select-Object -ExpandProperty line -Last 1 | Select-String "([A-Z]\:)(.*\\)*.*\.iso").matches.value
在尝试了一些事情之后,我最终得到了这个结果,它确实如预期的那样返回
($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版中引入的语言行为发生了变化-来自: Windows PowerShell语言增强功能 Windows PowerShell 3.0包含许多旨在简化其语言的功能, 易于使用,避免常见错误。这些改进包括 属性枚举,标量对象上的计数和长度属性, 新的重定向操作符,$Using范围修饰符,PSItem automatic 变量、灵活的脚本格式、变量属性、, 简化的属性参数、数字命令名和 停止解析运算符、改进的数组布局、新的位运算符、, 有序字典、pscustomobjectcasting和改进的 基于注释的帮助 (重点由我补充) 属性枚举允许
引用运算符解析数组表达式的单个成员的属性,即使数组本身没有此类属性:
$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
请参阅有关成员枚举的博客文章。我读过这篇文章,当时没有任何意义。我刚刚返回并再次检查,返回了一个长度为1的数组,所以这确实是个问题。