PowerShell: String Formatting

当我看到下面的命令的时候,我有点不解,由于里面出现了占位符 
shell

Get-WmiObject Win32_Service |ForEach-Object { "{0} ({1}): Path: {2}" `windows

-f $_.Name, $_.StartMode, $_.PathName }ide


那占位符究竟是什么做用,咱们看下这个示例,占位符从0开始,而后1...10之类this

占位符的做用:帮变量占位置,这样咱们就可让格式化输出spa

When I saw the first drafts of the scripts, they had a lot of Write-Verbose calls like this one:orm

$VerbosePreference = “Continue”blog

$filepath = “C:ps-testtest.txt”ip

$owner = “juneb”ci

$result = $trueget

Write-Verbose (“Created {0} by {1}. Result is {2}.” –f $filepath, $owner, $result)

Here’s the result:

VERBOSE: Created C:ps-testtest.txt by juneb. Result is True.

参考连接:

https://blogs.technet.microsoft.com/heyscriptingguy/2014/02/15/string-formatting-in-windows-powershell/

http://social.technet.microsoft.com/wiki/contents/articles/4250.powershell-string-formatting.aspx

相关文章
相关标签/搜索