我见过提及改进的PowerShell 3.0语法,但还没有示例,它会是什么样子?
答案 0 :(得分:9)
许多常见*-Object
cmdlet使用多个参数集来完成简化语法。在V3中看一下这个:
C:\PS> Get-Command Where-Object -Syntax
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] [-EQ] [<CommonParameters>]
Where-Object [-FilterScript] <scriptblock> [-InputObject <psobject>] [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CGT [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CNE [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -LT [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CEQ [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -NE [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -GT [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CLT [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -GE [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CGE [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -LE [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CLE [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -Like [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CLike [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -NotLike [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CNotLike [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -Match [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CMatch [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -NotMatch [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CNotMatch [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -Contains [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CContains [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -NotContains [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CNotContains [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -In [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CIn [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -NotIn [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -CNotIn [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -Is [<CommonParameters>]
Where-Object [-Property] <string> [[-Value] <Object>] [-InputObject <psobject>] -IsNot [<CommonParameters>]
注意:查看新的运营商-NotIn
和-In
,例如:
C:\PS> 1 -In 1..5
C:\PS> 10 -NotIn 1..5
所以简化的语法对于“常见”的情况很好,但请注意,因为你很容易陷入尖锐的岩石和熔岩,例如:
C:\PS> Get-ChildItem | Where LastWriteTime.Year -eq 2010
这什么都不返回,更糟糕的是,没有错误,所以你认为结果集“正确”为空,实际上这个语法实际上并不像你期望的那样工作。也就是说,您无法访问属性的属性。在上文中,PowerShell查找名为LastWriteTime.Year
的属性,该属性不存在。
另请注意,作为简化语法的一部分,您现在可以使用$PSItem
代替$_
,以防您或您编写脚本的人对$_
产生某种过敏反应。 : - )
虽然这不一定与简化语法有关,但我发现它简化了我的生活,我喜欢它:
C:\PS> Get-ChildItem -Directory
C:\PS> Get-ChildItem -File
C:\PS> dir -ad
C:\PS> Get-ChildItem -Attributes System+Hidden+Directory+!Archive
答案 1 :(得分:7)
以下是一个例子:
dir | where length -lt 10
在3.0之前,它本来是
dir | where {$_.length -lt 10}
编辑:另一个例子,这次使用foreach-object
dir | foreach-object length
答案 2 :(得分:5)
Powershell已经有了非常干净的语法,因此没有太多需要改进的地方。
我喜欢的一个新增加的是Hash Table as objects
,您可以通过传递hastable及其属性来创建对象:
[<ClassName>]$Variable = @{<Property>=<Value>;<Property>=<Value>}
因此,更简洁,更简洁的创建自定义对象的方法是:
$obj = [PSCustomObject]@{a=1; b=2; c=3; d=4}
重定向已得到加强。除了正常(管道)和错误之外,您现在还拥有详细,调试和警告的流,因此您可以执行重定向,例如5>&1
您可以使用$PSDefaultParameterValues
首选项变量为cmdlet设置默认参数值。
有新的[ordered]
加速器来创建有序的hastable(字典):
$a = [ordered]@{a=1;b=2;d=3;c=4}
在SO的另一个答案中,我意识到-in
在Powershell v3.0中是新的:
所以你做了类似1 -in 1,2,3
的事情。以前我们只有-contains
的cmdlet:
您可以使用Update-Help
cmdlet更新帮助。有与Invoke-
WebRequest
类似的网络相关cmdlet。您还可以使用ConverTo-JSON
和ConvertFrom-JSON
cmdlet处理JSON。