我在脚本中使用格式运算符,示例如下:
$current = 1
$total = 1250
$userCN = "contoso.com/CONTOSO/Users/Adam Smith"
"{0, -35}: {1}" -f "SUCCESS: Updated user $current/$total", $userCN
Run Code Online (Sandbox Code Playgroud)
这 expectedly 显示以下输出:
SUCCESS: Updated user 1/1250 : contoso.com/CONTOSO/Users/Adam Smith
格式运算符用于将目标输出文本保持在原位,当前/总运行数字在 1-99999 之间变化。如果没有格式运算符,我可以像这样突出显示成功行:
Write-Host -BackgroundColor Black -ForegroundColor Green "SUCCESS: Updated user $current/$total: $userCN"
但问题是如何将高亮颜色与格式运算符结合使用?只有-f参数,它不允许颜色参数,因为它Write-Host与最初的不同。