【发布时间】:2020-12-12 16:50:05
【问题描述】:
我目前正在使用 PS ISE 中的颜色。这是一些提示,其中一些只需要阅读,一些需要用户输入。在执行 clear 并使用颜色初始化新的写入主机命令后,我意识到颜色似乎是自己做的。
我已经提供了测试代码。
有什么想法吗?
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