Bor*_*ard 5 syntax powershell powershell-3.0
我已经看到提到改进的PowerShell 3.0语法,但还没有一个例子,它会是什么样子?
许多常见*-Objectcmdlet使用多个参数集来实现简化的语法.在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>]
Run Code Online (Sandbox Code Playgroud)
注意:查看新的操作符-NotIn,-In例如:
C:\PS> 1 -In 1..5
C:\PS> 10 -NotIn 1..5
Run Code Online (Sandbox Code Playgroud)
所以简化的语法对于"普通"情况很好,但请注意,因为你可以很容易地掉入尖锐的岩石和熔岩中,例如:
C:\PS> Get-ChildItem | Where LastWriteTime.Year -eq 2010
Run Code Online (Sandbox Code Playgroud)
没有返回任何内容,更糟糕的是,没有错误,因此您认为结果集"正确"为空,而实际上这种语法并不像您预期的那样有效.也就是说,您无法访问属性的属性.在上面,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
Run Code Online (Sandbox Code Playgroud)
这是一个例子:
dir | where length -lt 10
在3.0之前,它本来就是
dir | where {$_.length -lt 10}
编辑:另一个例子,这次是foreach-object
dir | foreach-object length
Powershell已经有了非常干净的语法,因此没有太多需要改进的地方.
我喜欢的一个新增功能是Hash Table as objects,您可以通过传递hastable及其属性来创建对象:
[<ClassName>]$Variable = @{<Property>=<Value>;<Property>=<Value>}
Run Code Online (Sandbox Code Playgroud)
因此,创建自定义对象的更简洁,更简洁的方法是:
$obj = [PSCustomObject]@{a=1; b=2; c=3; d=4}
Run Code Online (Sandbox Code Playgroud)
重定向得到了加强.除了正常(管道)和错误之外,您现在还拥有详细,调试和警告的流,因此您可以执行重定向5>&1
您可以使用$PSDefaultParameterValues首选项变量为cmdlet设置默认参数值.
有一个新的[ordered]加速器来创建有序的hastable(字典):
$a = [ordered]@{a=1;b=2;d=3;c=4}
Run Code Online (Sandbox Code Playgroud)
从SO的另一个答案,我意识到这-in是Powershell v3.0中的新功能:
所以你做的事情就像1 -in 1,2,3.以前我们只有-contains
cmdlet的:
您可以使用Update-Helpcmdlet 更新帮助.有像网络相关的cmdlet Invoke-
WebRequest.您还可以使用ConverTo-JSON和ConvertFrom-JSONcmdlet 处理JSON .
| 归档时间: |
|
| 查看次数: |
1077 次 |
| 最近记录: |