5

我已经看到过改进的 PowerShell 3.0 语法的提及,但还不是一个示例,它会是什么样子?

4

3 回答 3

9

许多常见的*-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>]

注意:查看新的运算符-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.Yearwhich 不存在的属性。

另请注意,作为简化语法的一部分,您现在可以使用$PSItem它来代替,$_以防您或您为其编写脚本的人对$_. :-)

虽然这不一定与简化的语法有关,但我发现它简化了我的生活,我喜欢它:

C:\PS> Get-ChildItem -Directory
C:\PS> Get-ChildItem -File
C:\PS> dir -ad
C:\PS> Get-ChildItem -Attributes System+Hidden+Directory+!Archive
于 2011-10-18T02:01:06.230 回答
7

这是一个例子:

dir | where length -lt 10

在 3.0 之前,它本来是

dir | where {$_.length -lt 10}

编辑:另一个例子,这次是 foreach-object

dir | foreach-object length

于 2011-10-18T01:32:41.427 回答
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更新帮助。有与 Web 相关的 cmdlet,例如Invoke- WebRequest. 您还可以使用ConverTo-JSONConvertFrom-JSONcmdlet 处理 JSON。

于 2011-10-17T22:21:21.377 回答