powershell中的-f是什么意思?

Kau*_*ash 2 powershell

我是 powershell 的新手,并试图从脚本中解码以下行

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

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

我对“ForEach-Object {“{0:x}” -f $_} 更感兴趣

“{0:x}”是什么意思?

-f 对当前对象 $_ 有什么作用?

当我在 Windows Server 上运行脚本时,我看到 NodeWWN 条目之一: NodeWWN : {32, 0, 0, 16...} 现在转换为: 20:00:00:10:9B:17: 9E:28

如果这里的专家能帮助我指出正在发生的事情,我将不胜感激。

Joe*_*oey 5

-f格式运算符,描述于about_operators

-f 格式运算符

使用字符串对象的格式方法格式化字符串。在运算符左侧输入格式字符串,在运算符右侧输入要格式化的对象。

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

# 1 hello      3.14
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅String.Format方法和Composite Formatting

表达方式

<a> -f <b>
Run Code Online (Sandbox Code Playgroud)

相当于调用

[string]::Format(<a>, <b>)
Run Code Online (Sandbox Code Playgroud)

这也意味着格式字符串中的占位符遵循与其他地方相同的规则。普通占位符是{0}{1}等,它们可以为占位符插入的类型设置格式,就像在您的示例中一样{0:x},它将整数格式化为十六进制数。当然,可以在.NET 文档中找到 CLR 中各种类型的预定义和自定义格式字符串。