0

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

我已经提供了测试代码。

有什么想法吗?

运行 PS 脚本

在上面的屏幕上按“Enter”后

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  
}```


4

2 回答 2

0

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

于 2021-01-22T00:54:49.580 回答
0

ISE 和 PowerShell 控制台之间的差异

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

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

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

PowerShell ISE 限制 (Windows) | 微软文档

PowerShell ISE 限制

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

$psUnsupportedConsoleApplications # 结果 <# wmic wmic.exe cmd cmd.exe diskpart diskpart.exe edit.com netsh netsh.exe nslookup nslookup.exe powershell powershell.exe #>

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

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

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

选择.exe

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

$psUnsupportedConsoleApplications

或查阅在线帮助。

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

$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:运行可执行文件

于 2020-12-13T05:37:29.770 回答