简体   繁体   English

powershell中的-f是什么意思?

[英]What does -f in powershell mean?

I am new to powershell and trying to decode the following line from a script我是 powershell 的新手,并试图从脚本中解码以下行

Get-WmiObject -class MSFC_FCAdapterHBAAttributes -namespace “root\\WMI” | Get-WmiObject -class MSFC_FCAdapterHBAAttributes -namespace “root\\WMI” | ForEach-Object {(($ .NodeWWN) | ForEach-Object {“{0:x}” -f $ })} ForEach-Object {(($ .NodeWWN) | ForEach-Object {“{0:x}” -f $ })}

The script is available at: https://gallery.technet.microsoft.com/scriptcenter/Find-HBA-and-WWPN-53121140该脚本位于: https : //gallery.technet.microsoft.com/scriptcenter/Find-HBA-and-WWPN-53121140

I am more interested in the "ForEach-Object {“{0:x}” -f $_}"我对“ForEach-Object {“{0:x}” -f $_} 更感兴趣

what does "{0:x}" mean? “{0:x}”是什么意思?

And what does the -f do to the current object $_ ? -f 对当前对象 $_ 有什么作用?

when I run the script on my Windows Server, I see that one of the NodeWWN entry: NodeWWN : {32, 0, 0, 16...} is now converted to: 20:00:00:10:9B:17:9E:28当我在 Windows Server 上运行脚本时,我看到 NodeWWN 条目之一: NodeWWN : {32, 0, 0, 16...} 现在转换为: 20:00:00:10:9B:17: 9E:28

Would really appreciate it if the experts here could help me point out what is going on.如果这里的专家能帮助我指出正在发生的事情,我将不胜感激。

-f is the format operator , described in about_operators : -f格式操作符 ,在about_operators描述:

-f Format operator -f格式运算符

Formats strings by using the format method of string objects. 使用字符串对象的format方法格式化字符串。 Enter the format string on the left side of the operator and the objects to be formatted on the right side of the operator. 在操作员的左侧输入格式字符串,在操作员的右侧输入要格式化的对象。

 PS> "{0} {1,-10} {2:N}" -f 1,"hello",[math]::pi # 1 hello 3.14 

For more information, see the String.Format method and Composite Formatting . 有关更多信息,请参见String.Format方法和Composite Formatting

The expression 表达方式

<a> -f <b>

is equivalent to calling 相当于打电话

[string]::Format(<a>, <b>)

This also means that placeholders in the format string follow the same rules as elsewhere. 这也意味着格式字符串中的占位符遵循与其他位置相同的规则。 Normal placeholders are {0} , {1} , etc. and they can have a format for the type inserted for the placeholder, like in your example with {0:x} which would format an integer as a hexadecimal number. 正常的占位符是{0}{1}等,并且它们可以具有为占位符插入的类型的格式,例如在您的示例中,使用{0:x}会将整数格式设置为十六进制数。 Predefined and custom format strings for various types in the CLR can be found in the .NET documentation , of course. 当然,可以在.NET文档中找到CLR中各种类型的预定义和自定义格式字符串。

The -f operator takes its left argument as a description of the desired output, and its right argument as an array of values to be inserted into that string. -f运算符将其左参数用作所需输出的描述,并将其右参数用作要插入该字符串的值的数组。 The {0:x} specifies how to format the argument; {0:x}指定如何格式化参数; a description can be found at SS64's page on the -f operator . 可以-f运算符的SS64页面上找到描述。

That makes complete sense. 完全有道理。 I tried printing out the contents of NodeWWN as is: {32, 0, 0 , 16, 155, 23, 158, 40} 我尝试按如下方式打印出NodeWWN的内容:{32,0,0,16,16,155,23,158,40}

With the help of the String fomrat operation where it converts from integer to Hex, I see the output 20, 0, 0, 10, 9b, 17, 9e , 28 which is the answer from my question posted initially. 在String fomrat操作的帮助下,它从整数转换为十六进制,我看到输出20、0、0、10、9b,17、9e,28,这是我最初发布的问题的答案。

Thanks a lot for helping me figure this out. 非常感谢您帮助我解决这个问题。 Appreciate all the help! 感谢所有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM