【问题标题】:What causes Write-Host colors to perform like this?是什么原因导致 Write-Host 颜色像这样执行?
【发布时间】:2020-12-12 16:50:05
【问题描述】:

我目前正在使用 PS ISE 中的颜色。这是一些提示,其中一些只需要阅读,一些需要用户输入。在执行 clear 并使用颜色初始化新的写入主机命令后,我意识到颜色似乎是自己做的。

我已经提供了测试代码。

有什么想法吗?

Running PS Script

After pressing 'Enter' on the screen above

function logo {
    Write-Host ("==" * 23) -ForegroundColor Red -BackgroundColor DarkRed
    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed -NoNewLine
    Write-Host (" _________  _______   ________  _________   ") -ForegroundColor White -NoNewline
    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed

    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed -NoNewLine
    Write-Host ("|\___   ___\\  ___ \ |\   ____\|\___   ___\ ") -ForegroundColor White -NoNewline
    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed

    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed -NoNewLine
    Write-Host ("\|___ \  \_\ \   __/|\ \  \___|\|___ \  \_| ") -ForegroundColor White -NoNewline
    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed

    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed -NoNewLine
    Write-Host ("     \ \  \ \ \  \_|/_\ \_____  \   \ \  \  ") -ForegroundColor White -NoNewline
    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed

    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed -NoNewLine
    Write-Host ("      \ \  \ \ \  \_|\ \|____|\  \   \ \  \ ") -ForegroundColor White -NoNewline
    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed

    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed -NoNewLine
    Write-Host ("       \ \__\ \ \_______\____\_\  \   \ \__\") -ForegroundColor White -NoNewline
    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed

    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed -NoNewLine
    Write-Host ("        \|__|  \|_______|\_________\   \|__|") -ForegroundColor White -NoNewline
    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed

    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed -NoNewLine
    Write-Host ("                        \|_________|        ") -ForegroundColor White -NoNewline
    Write-Host ("=") -ForegroundColor Red -BackgroundColor DarkRed

    Write-Host ("==" * 23) -ForegroundColor Red -BackgroundColor DarkRed
    Write-Host ("`n")
}

# - WARNING Read Host Confirmation
function warningConfirm() {
    param
    (
        [Parameter(Position = 0, ValueFromPipeline = $true)]
        [string]$msg,
        [string]$BackgroundColor = "White",
        [string]$ForegroundColor = "Red"
    )


    Write-Host -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -NoNewline $msg;
    return Read-Host
}

# - Read Host Confirmation
function confirm() {
    param
    (
        [Parameter(Position = 0, ValueFromPipeline = $true)]
        [string]$msg,
        [string]$BackgroundColor = "Yellow",
        [string]$ForegroundColor = "Black"

    )
    Write-Host -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -NoNewline $msg;
    return Read-Host
}

# - Text Colors
function text() {
    param
    (
        [Parameter(Position = 0, ValueFromPipeline = $true)]
        [string]$msg,
        [string]$BackgroundColor = "Yellow",
        [string]$ForegroundColor = "Black"
    )


    Write-Host -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -NoNewline $msg;
}
clear

logo
$continue = 0
while ( $continue -eq 0 ) {
    $opt = (Get-Host).PrivateData
    $opt.WarningBackgroundColor = "Red"
    $opt.WarningForegroundColor = "White"
    Write-Warning "This version of Test is for use on 2008R2 servers only."
    warningConfirm ("`nConfirm you are using the correct version by pressing 'Enter'")
    clear
    logo
    confirm ("`nTest is initiating procedures... To being processing Test jobs press > > Enter:")
    text ("Gathering information to run checks. . . `(Approx. 43 Jobs`)`n")
    $continue = 1  
}```


【问题讨论】:

  • 似乎只是 ISE 的事情,如果作为脚本运行则不会这样做。
  • ISE 控制台不是真正的控制台。它是一个主要和输出窗口。 IT 从来没有被设计成按照真正控制台的设计方式来做事。在 ISE 的早期,它有 3 个窗格 editor, output window/ console 和 MS,无论出于何种原因拉出了控制台窗格。您可以在 ISE 中编写所有内容,但如果您需要在控制台中查看它的外观,只需将您的代码/脚本从 ISE 传递到控制台即可。当控制台关闭时,您将返回到 I

标签: powershell colors write-host


【解决方案1】:

Differences between the ISE and PowerShell console.

ISE 确实针对开发环境进行了优化,而控制台主机是运行环境。

您无法在 ISE 中运行交互式可执行文件,但可以在控制台主机中运行。

为防止出现这种情况,PowerShell ISE 会维护一个不受支持的控制台应用程序列表,并且不会运行它们。列表存储在变量中 $psUnsupportedConsoleApplications(在常规 PowerShell 控制台中不存在)。

PowerShell ISE Limitations (Windows) | Microsoft Docs

PowerShell ISE 限制

您无法在 ISE 中运行交互式会话,例如,您 不能以交互方式运行 netsh 或 diskpart。对于部分列表 ISE 无法运行的工具,请在 ISE 提示符处键入以下内容:

$psUnsupportedConsoleApplications # 结果

在这些情况下,您应该改用经典的 PowerShell 控制台。

您可以改进此列表并添加您发现在 PowerShell ISE 中无法正常运行的应用程序。例如,您可以将choice.exe 添加到列表中:

$psUnsupportedConsoleApplications.Add('choice.exe') 选择.exe

选择.exe

无法启动“choice.exe”。交互式控制台应用程序不是 支持的。要运行应用程序,请使用 Start-Process cmdlet 或 使用“文件”菜单中的“启动 PowerShell.exe”。查看/修改 被阻止的控制台应用程序列表,使用

$psUnsupportedConsoleApplications

或查阅在线帮助。

要从 ISE 脚本窗格或 ISE 控制台执行您的 exe,您必须执行以下操作……

$ConsoleCommand = 'whatever exe you want to use and all its params / switches'
{Start-Process powershell -ArgumentList "-NoExit","-Command  & { $ConsoleCommand }" -Wait}

这会输出到控制台主机,运行命令,等待以便您查看结果,然后您可以关闭并返回 ISE。

我编写了一个我汇总的函数,我可以在这些确切的场景中使用它。这样我调用我的函数,将我的外部 exe 命令字符串传递给它,它就会按预期运行。

您可以在 ISE 中运行这些 exe,但您必须通过它们所需的一切。

PowerShell: Running Executables

【讨论】:

    【解决方案2】:

    解决方案:我发现执行 Clear-Host 后的 Start-Sleep -Second 1(介于 0.5 -1 秒之间)将为 ISE 控制台提供一秒钟的时间来赶上而不改变颜色。当然,它看起来不像只使用控制台那么美观,但它确实可以防止颜色变化。

    【讨论】:

      猜你喜欢
      • 2022-06-18
      • 2012-07-05
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多