PowerShell: String Formatting

當我看到下面的命令的時候,我有點不解,因爲裏面出現了佔位符 

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

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


那佔位符到底是什麼作用,我們看下這個示例,佔位符從0開始,然後1...10之類

佔位符的作用:幫變量佔位置,這樣我們就可以讓格式化輸出

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

$VerbosePreference = “Continue”

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

$owner = “juneb”

$result = $true

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章