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

相關文章
相關標籤/搜索