powershell中的-f是什么意思? [英] What does -f in powershell mean?

查看:379
本文介绍了powershell中的-f是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 powershell 的新手,正在尝试从脚本中解码以下行

I am new to powershell and trying to decode the following line from a script

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

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 $_} 更感兴趣

I am more interested in the "ForEach-Object {"{0:x}" -f $_}"

{0:x}"是什么意思?

what does "{0:x}" mean?

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

And what does the -f do to the current object $_ ?

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

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

如果这里的专家能帮我指出发生了什么,我将不胜感激.

Would really appreciate it if the experts here could help me point out what is going on.

推荐答案

-f格式运算符,在 about_operators:

-f is the format operator, described in about_operators:

-f 格式运算符

-f Format operator

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

Formats strings by using the format method of string objects. 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

有关详细信息,请参阅 String.Format 方法和 复合格式.

For more information, see the String.Format method and Composite Formatting.

表达式

<a> -f <b>

相当于调用

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

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

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. Predefined and custom format strings for various types in the CLR can be found in the .NET documentation, of course.

这篇关于powershell中的-f是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆