gpt4 book ai didi

PowerShell 3.0 应该有更清晰的语法,它看起来像什么?

转载 作者:行者123 更新时间:2023-12-04 16:25:01 25 4
gpt4 key购买 nike

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

最佳答案

一些常见的*-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代替 $_如果您或您编写脚本的人对 $_ 有某种过敏 react . :-)

虽然这不一定与简化的语法有关,但我发现它简化了我的生活,我喜欢它:
C:\PS> Get-ChildItem -Directory
C:\PS> Get-ChildItem -File
C:\PS> dir -ad
C:\PS> Get-ChildItem -Attributes System+Hidden+Directory+!Archive

关于PowerShell 3.0 应该有更清晰的语法,它看起来像什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7800207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com