【问题标题】:powershell, change -WhatIf message text colorpowershell,更改-WhatIf消息文本颜色
【发布时间】:2023-01-13 01:31:32
【问题描述】:

通过更改相应的$host.PrivateData.<MessageType>ForegroundColor$host.PrivateData.<MessageType>BackgroundColor 的值,很容易更改错误、警告、调试、详细、进度消息方面,但我发现没有办法将-WhatIf 消息的颜色更改为平淡无奇以外的任何颜色灰色,以使其更明显。

我错过了什么还是不可能的?

【问题讨论】:

标签: powershell


【解决方案1】:

由于无法捕获或重定向-WhatIf,我看到的唯一可能的解决方案是我不提倡, 就是注入VT sequences在到达$PSCmdlet.ShouldProcess之前.为了学习而分享这个,但请不要认为它很可靠。

0..10 | & {
    [CmdletBinding(SupportsShouldProcess)]
    param([Parameter(ValueFromPipeline)] [string] $Test)

    process {
        # if using WhatIf
        if($PSBoundParameters.ContainsKey('WhatIf')) {
            # inject VT sequence for bright yellow
            $Host.UI.Write("$([char] 27)[93m")
        }
        if($PSCmdlet.ShouldProcess($Test, 'waving')) {
            "hello $Test"
        }
        # inject VT sequence for Reset console color
        $Host.UI.Write("$([char] 27)[0m")
    }
} -WhatIf

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多