gpt4 book ai didi

powershell - Powershell获取服务-forgroundcolor

转载 作者:行者123 更新时间:2023-12-02 23:54:41 28 4
gpt4 key购买 nike

我正在尝试更改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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com