Powershell 标志和方法背后的逻辑是什么?

Powershell 标志和方法背后的逻辑是什么?,powershell,Powershell,例如: > "a,b,c" -split "," a b c > "a,b,c".split(",") a b c > "a,b,c".length 5 > "a,b,c" -length At line:1 char:9 + "a,b,c" -length + ~~~~~~~ Unexpected token '-length' in expression or statement. + CategoryInfo : ParserError: (

例如:

> "a,b,c" -split "," a b c > "a,b,c".split(",") a b c > "a,b,c".length 5 > "a,b,c" -length At line:1 char:9 + "a,b,c" -length + ~~~~~~~ Unexpected token '-length' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken >“a、b、c”-拆分” A. B C >“a、b、c”。拆分(“,”) A. B C >“a,b,c”,长度 5. >“a、b、c”-长度 第1行字符:9 +“a、b、c”-长度 + ~~~~~~~ 表达式或语句中出现意外标记'-length'。 +CategoryInfo:ParserError:(:)[],ParentContainerErrorRecordException +FullyQualifiedErrorId:意外终止 因此,并非每个方法都可以表示为标志/参数列表。我甚至不确定
.split
-split
是同一件事,也不确定这是否是偶然的

我应该在什么时候使用标志,什么时候使用方法?如何发现所有可用的标志(字符串、数字等)

另一件事是
ls-?
返回帮助文本,但
“foo”-?
不返回帮助文本。因此,虽然它接受标志,但它实际上并没有被视为命令,归结起来就是这样

这个…“a,b,c”-长度

关于运营商 运算符是可以在命令或表达式中使用的语言元素。 由于存在多种操作员类型,因此上述不是单一的源单据引用

与此相对的是“a,b,c”长度

关于方法 描述如何使用方法对PowerShell中的对象执行操作。 方法允许您检查、比较和格式化PowerShell对象的许多属性,以及执行操作。

从你的例子来看:

String.Split方法 返回一个字符串数组,该数组包含此实例中由指定字符串或Unicode字符数组的元素分隔的子字符串

关于分裂 Split运算符将一个或多个字符串拆分为子字符串。

String.Length属性 Length属性返回此实例中的字符对象数,而不是Unicode字符数

您会注意到,如果在PowerShell_ISE.exe或VSCode中打开此项,您会立即看到,即使在运行之前,项4也会立即显示为语法错误。这由红色的波形表示。该标记表示它将永远无法工作,因此没有真正的理由尝试它

仅仅因为您可以键入它,并不意味着它是正确的。如果您在任何内容的空格后键入“-”,则会得到一个预期内容的列表。如果您使用的是PowerShell或Visual Studio代码。如果您使用的是PowerShell控制台主机,则必须按tab键来标记列表,或者使用CRTL+空格键查看完整列表,然后单击tab or箭头指向您要使用的内容

('a,b,c').length # this is an array, and this is returning the count of the elements in the array
5
('a,b,c','d,e,f').length # note the element count difference
2

('a,b,c').Length # property use of a .Net class
5

('a,b,c') -length # attempted unknown / invalid switch (PowerShell operator) 
要知道你可以对一个对象做什么,不能做什么,或者如何做,你必须知道它支持什么。这就是Get Member的作用

所以数组允许这样做

('a,b,c') | Get-Member 
还有这个

('abc')   | Get-Member

Most common listed 

Name             MemberType            Definition
----             ----------            ----------
...
Split            Method                string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] Sp...
...
Substring        Method                string Substring(int startIndex), string Substring(int startIndex, int length)                      
...
ToLower          Method                string ToLower(), string ToLower(cultureinfo culture)                                               
...
ToString         Method                string ToString(), string ToString(System.IFormatProvider provider), string IConvertible.ToString...
...
ToUpper          Method                string ToUpper(), string ToUpper(cultureinfo culture)
...
Trim             Method                string Trim(Params char[] trimChars), string Trim()
TrimEnd          Method                string TrimEnd(Params char[] trimChars)
TrimStart        Method                string TrimStart(Params char[] trimChars)
Chars            ParameterizedProperty char Chars(int index) {get;}
Length           Property              int Length {get;}
至于这个。。。 我应该在什么时候使用标志,什么时候使用方法? 如何发现所有可用的标志(字符串、数字等)

您必须阅读试图使用的cmdlet上的帮助文件,或者至少阅读其示例

    Get-Help -Name Get-ItemProperty -Full

    Get-Help -Name Get-ItemProperty -Examples
然后提供有关您尝试使用的cmdlet/函数的信息

    (Get-Command -Name Get-ItemProperty).Parameters 
     switches (flags) which will expect a value to the right of it or not, see the property values line below
然后,您可以在其上使用cmdlet/函数

    Get-ItemProperty -Path D:\Temp | Format-Table -AutoSize -Wrap
    Get-ItemProperty -Path D:\Temp | Format-List
    Get-ItemProperty -Path D:\Temp | Format-List -Force
    Get-ItemProperty -Path D:\Temp | Select-Object -Property * # property values
    (Get-ItemProperty -Path D:\Temp) | Get-Member
至于这个。。。 另一件事是ls-?返回帮助文本,但“foo”-?不返回。 因此,虽然它接受标志,但实际上并没有将其视为命令

Foo不是PowerShell中任何内容的有效名称,除非您创建了Foo函数或模块。因此,它应该热返回任何内容。 再说一次,仅仅因为您可以键入它,并不能使它正确

在大多数情况下,如果您正在执行上述操作,并且没有自动获得intellisense,那么您所做的可能是错误的

要查看系统上所有要使用的cmdlet、函数等,请执行以下操作

Get-Command

-split
不是标志。它是运算符:
获取有关\u运算符的帮助
。否,
split()
-split
不要做同一件事。前者在参数中的任何字符处拆分字符串,后者在给定的正则表达式处拆分字符串。在您的情况下,看起来它们做的是相同的,因为您在正则表达式中拆分单个字符,没有特殊意义。您的解释是f为什么
“foo”-?
不起作用是不正确的。实际原因是PowerShell不会将字符串解释为命令,除非您使用调用运算符(
&
)。裸字符串会自动回显到成功输出流。如果您将以连字符开头的内容放在解释为运算符的字符串之后(但运算符
-?
不存在)。
和“foo”-?
如果
foo
是有效的命令,则可以工作。例如,比较
“复制项”-?
和“复制项”-?
的输出。