我正在尝试更改get-service输出的颜色。它似乎不允许我在get-service之后使用write-host -forgroundcolor参数。有没有办法解决?
我尝试了以下配置:
get-service write-host -foregroundcolor "green"
get-service -foregroundcolor "green"
如果将get-service保存到一个变量中,然后将write-host放在前面,就可以使它正常工作:
$var = get-service
write-host $var -foregroundcolor "green"
问题是输出格式不正确,变成一行。
最佳答案
Get-Service | Out-String [-Stream] | Write-Host -ForegroundColor Green
根据
Out-String
的帮助主题:The Out-String cmdlet converts the objects that Windows PowerShell manages into an
array of strings. By default, Out-String accumulates the strings and returns them as
a single string, but you can use the stream parameter to direct Out-String to return
one string at a time. This cmdlet lets you search and manipulate string output as
you would in traditional shells when object manipulation is less convenient.
通过使用
-Stream
参数,您无需等待Get-Service
完成执行,但它会在获取输入时接受输入。关于powershell - Powershell获取服务-forgroundcolor,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47833962/