PowerShell 3.0 应该有更清晰的语法,它是什么样的?

发布于 2024-12-10 13:54:03 字数 50 浏览 0 评论 0原文

我看到提到了改进的 PowerShell 3.0 语法,但还没有示例,它会是什么样子?

I've seen mentions of improved PowerShell 3.0 syntax but not an example yet, how will it look like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懷念過去 2024-12-17 13:54:03

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

A number of the common *-Object cmdlets utilize multiple parameter sets to accomplish the simplified syntax. Take a look at this in 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>]

NOTE: Check out the new operators -NotIn and -In e.g.:

C:\PS> 1 -In 1..5
C:\PS> 10 -NotIn 1..5

So the simplified syntax is nice for the "common" case but watch out as you can fall off into the sharp rocks and lava pretty easily e.g.:

C:\PS> Get-ChildItem | Where LastWriteTime.Year -eq 2010

This returns nothing and even worse, there is no error so you think the result set is "correctly" empty when in fact this syntax just doesn't work as you might expect. That is, you can't access a property of a property. In the above, PowerShell looks for a property called LastWriteTime.Year which doesn't exist.

Also note that as part of the simplified syntax you can now use $PSItem in place of $_ in case you or those you write scripts for have some sort of allergic reaction to $_. :-)

And while this isn't necessarily tied to the simplified syntax I find that it simplifies my life and I love it:

C:\PS> Get-ChildItem -Directory
C:\PS> Get-ChildItem -File
C:\PS> dir -ad
C:\PS> Get-ChildItem -Attributes System+Hidden+Directory+!Archive
皓月长歌 2024-12-17 13:54:03

这是一个示例:

dir |其中 length -lt 10

在 3.0 之前,它会是

dir |其中 {$_.length -lt 10}

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

dir | foreach-对象长度

Here's an example:

dir | where length -lt 10

Before 3.0, it would have been

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

edit: another example, this time with foreach-object

dir | foreach-object length

起风了 2024-12-17 13:54:03

Powershell 确实已经拥有相当干净的语法,因此没有太多需要改进的地方。

我喜欢的一项新功能是作为对象的哈希表,您可以通过传递 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-
Web请求
。您还可以使用 ConverTo-JSONConvertFrom-JSON cmdlet 处理 JSON。

Powershell does have a pretty clean syntax already, so there is not much that needs improvement.

One new addition that I do like is the Hash Table as objects, where you can create objects by passing hastable with its properties:

[<ClassName>]$Variable = @{<Property>=<Value>;<Property>=<Value>}

So the newer, more succinct way of creating custom objects is:

$obj = [PSCustomObject]@{a=1; b=2; c=3; d=4}

The redirection has been beefed up. You have now streams for verbose, debug and warning in addition to normal ( pipeline ) and error and so you can do redirections like 5>&1

You can use $PSDefaultParameterValues preference variable to set default parameter values for cmdlets.

There is the new [ordered] accelerator to create ordered hastable (dictionary):

 $a = [ordered]@{a=1;b=2;d=3;c=4}

From another answer here in SO, I realized that -in was new in Powershell v3.0:

So you do something like 1 -in 1,2,3. Previously we only had -contains

Cmdlets:

You can update help with Update-Help cmdlet. There are web related cmdlets like Invoke-
WebRequest
. You can also handle JSON using ConverTo-JSON and ConvertFrom-JSON cmdlets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文