简体   繁体   English

是什么导致 Write-Host colors 像这样执行?

[英]What causes Write-Host colors to perform like this?

I am currently playing with the colors in PS ISE.我目前正在使用 PS ISE 中的 colors。 It is a few prompts in which some require only reading and some require user input.这是一些提示,其中一些只需要阅读,一些需要用户输入。 I realized that the colors seem to do their own after performing a clear and initializing new write-host commands with color.我意识到 colors 在执行清晰并使用颜色初始化新的写入主机命令后似乎可以自己做。

I have provided the code for testing.我已经提供了测试代码。

Any thoughts?有什么想法吗?

Running PS Script运行 PS 脚本

After pressing 'Enter' on the screen above在上面的屏幕上按“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  
}```


Differences between the ISE and PowerShell console . ISE 和 PowerShell 控制台之间的差异

The ISE is really optimized for a dev environment, whereas the console host is run environment. ISE 确实针对开发环境进行了优化,而控制台主机是运行环境。

You cannot run the interactive executable in the ISE you can in the consolehost.您无法在 ISE 中运行交互式可执行文件,但可以在控制台主机中运行。

To prevent this, PowerShell ISE maintains a list of unsupported console applications and won't run them.为防止这种情况,PowerShell ISE 维护了一个不受支持的控制台应用程序列表,并且不会运行它们。 The list is stored in the variable $psUnsupportedConsoleApplications (which does not exist in the regular PowerShell console).该列表存储在变量 $psUnsupportedConsoleApplications 中(在常规 PowerShell 控制台中不存在)。

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

PowerShell ISE Limitations PowerShell ISE 限制

You cannot run interactive sessions in the ISE, so for example, you cannot run netsh or diskpart interactively.您无法在 ISE 中运行交互式会话,例如,您无法交互式运行 netsh 或 diskpart。 For a partial list of tools the ISE can't run, type the following at the ISE prompt:有关 ISE 无法运行的部分工具列表,请在 ISE 提示符处键入以下内容:

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

In those situations, you should use the classic PowerShell console instead.在这些情况下,您应该改用经典的 PowerShell 控制台。

You can improve this list and add applications that you find won't run well in PowerShell ISE.您可以改进此列表并添加您发现在 PowerShell ISE 中无法正常运行的应用程序。 For example, you could add choice.exe to the list:例如,您可以将 choice.exe 添加到列表中:

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

choice.exe选择.exe

Cannot start “choice.exe”.无法启动“choice.exe”。 Interactive console applications are not supported.不支持交互式控制台应用程序。 To run the application, use the Start-Process cmdlet or use “Start PowerShell.exe” from the File menu.要运行应用程序,请使用 Start-Process cmdlet 或使用“文件”菜单中的“Start PowerShell.exe”。 To view/modify the list of blocked console applications, use要查看/修改被阻止的控制台应用程序列表,请使用

$psUnsupportedConsoleApplications

or consult online help.或查阅在线帮助。

To execute your exe, from the ISE script pane or ISE console, you have to do something like this…要从 ISE 脚本窗格或 ISE 控制台执行您的 exe,您必须执行以下操作……

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

This shells out to the console host, runs the commands, waits so you can see results, then you can close and go back to the ISE.这会输出到控制台主机,运行命令,等待以便您可以看到结果,然后您可以关闭并将 go 返回到 ISE。

I wrote a function I pulled together that I keep available for these exact scenarios.我写了一个 function 我把它放在一起,我可以在这些确切的场景中使用。 That way I call my function, pass it my external exe command string and it runs as expected.这样我就调用了我的 function,将我的外部 exe 命令字符串传递给它,它按预期运行。

You can run these exe's in the ISE, but you must pass them all they need.您可以在 ISE 中运行这些 exe,但您必须通过它们所需的一切。

PowerShell: Running Executables PowerShell:运行可执行文件

Solution: I found that Start-Sleep -Second 1 (between.5 -1 secs) after performing Clear-Host would provide the ISE console a sec to catch up and not shift the colors.解决方案:我发现执行 Clear-Host 后的 Start-Sleep -Second 1(介于.5 -1 秒之间)将为 ISE 控制台提供一秒钟的时间来赶上而不是移动 colors。 It doesn't look as aestheically pleasing as using just the console of course, but it does prevent colors from shifting.当然,它看起来不像只使用控制台那么美观,但它确实阻止了 colors 的移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM