我想在脚本完成时显示一些 ASCII 艺术品来增加趣味。
对于如何在控制台输出 ASCII 艺术字符,我有两个想法。希望比我更了解的人能指导我们使用哪个命令和哪种方法更好。
通过多行
Write-Host
输出?我之前尝试过这样做,但没有成功。它抛出了一堆错误。也许我没有使用“多行”版本(如果有的话)。将 ASCII 艺术品保存到 .txt 文件中,然后在我的脚本中以某种方式获取并读取该 .txt 文件的内容,然后在指定区域将其内容写入控制台。
哪种方法更好?如何实现这个效果?
我想在脚本完成时显示一些 ASCII 艺术品来增加趣味。
对于如何在控制台输出 ASCII 艺术字符,我有两个想法。希望比我更了解的人能指导我们使用哪个命令和哪种方法更好。
通过多行 Write-Host
输出?我之前尝试过这样做,但没有成功。它抛出了一堆错误。也许我没有使用“多行”版本(如果有的话)。
将 ASCII 艺术品保存到 .txt 文件中,然后在我的脚本中以某种方式获取并读取该 .txt 文件的内容,然后在指定区域将其内容写入控制台。
哪种方法更好?如何实现这个效果?
如果你有一个源文件,请使用一个以换行分隔的字符串、Here-String,或 Get-Content -Raw
。这两个方法都可以轻松地给你一个多行字符串。Here-String 的一个好处是不必担心你使用的引号(这可能会影响 ASCII 艺术)。以下是使用 Here-String 的示例:
$text = @"
"ROFL:ROFL:ROFL:ROFL"
_^___
L __/ [] \
LOL===__ \
L \________]
I I
--------/
"@
或者,如果您选择采用源文件方法:
$text = Get-Content -Raw $path
如果你只有PowerShell 2.0,$text = Get-Content $path | Out-String
可以实现相同的效果。
无论哪种方式,你都可以在之后使用Write-Host
或其他任何你想用的东西。
使用颜色会需要一些特殊的逻辑,在我所知道的范围内,目前不存在这样的逻辑。制作彩色输出随机器倒是很简单。为了获得一个基本的想法,我提供Get-Funky
。
function Get-Funky{
param([string]$Text)
# Use a random colour for each character
$Text.ToCharArray() | ForEach-Object{
switch -Regex ($_){
# Ignore new line characters
"`r"{
break
}
# Start a new line
"`n"{
Write-Host " ";break
}
# Use random colours for displaying this non-space character
"[^ ]"{
# Splat the colours to write-host
$writeHostOptions = @{
ForegroundColor = ([system.enum]::GetValues([system.consolecolor])) | get-random
# BackgroundColor = ([system.enum]::GetValues([system.consolecolor])) | get-random
NoNewLine = $true
}
Write-Host $_ @writeHostOptions
break
}
" "{Write-Host " " -NoNewline}
}
}
}
这将获取一个以换行符分隔的字符串,并使用随机主机颜色显示输出。我们使用 $writeHostOptions
的展开语法,因此您可以轻松控制颜色。您甚至可以有一些参数强制使用其中一个颜色或禁用某个颜色等。以下是一些示例输出:
$art = " .:::. .:::.`n:::::::.:::::::`n:::::::::::::::
':::::::::::::'`n ':::::::::'`n ':::::'`n ':'"
Get-Funky $art
在 asciiworld.com 上找到的心形ASCII艺术。
$PROFILE
的一部分:# Personalize the console
$Host.UI.RawUI.WindowTitle = "Windows Powershell " + $Host.Version;
# Draw welcome screen
Write-Host -ForegroundColor DarkYellow " _oo0oo_"
Write-Host -ForegroundColor DarkYellow " o8888888o"
Write-Host -ForegroundColor DarkYellow " 88`" . `"88"
Write-Host -ForegroundColor DarkYellow " (| -_- |)"
Write-Host -ForegroundColor DarkYellow " 0\ = /0"
Write-Host -ForegroundColor DarkYellow " ___/`----'\___"
Write-Host -ForegroundColor DarkYellow " .' \\| |// '."
Write-Host -ForegroundColor DarkYellow " / \\||| : |||// \"
Write-Host -ForegroundColor DarkYellow " / _||||| -:- |||||- \"
Write-Host -ForegroundColor DarkYellow " | | \\\ - /// | |"
Write-Host -ForegroundColor DarkYellow " | \_| ''\---/'' |_/ |"
Write-Host -ForegroundColor DarkYellow " \ .-\__ '-' ___/-. /"
Write-Host -ForegroundColor DarkYellow " ___'. .' /--.--\ `. .'___"
Write-Host -ForegroundColor DarkYellow " .`"`" '< `.___\_<|>_/___.' >' `"`"."
Write-Host -ForegroundColor DarkYellow " | | : `- \`.;`\ _ /`;.`/ - ` : | |"
Write-Host -ForegroundColor DarkYellow " \ \ `_. \_ __\ /__ _/ .-` / /"
Write-Host -ForegroundColor DarkYellow " =====`-.____`.___ \_____/___.-`___.-'====="
Write-Host -ForegroundColor DarkYellow " `=---='"
# Create frequent commands
New-Alias -Name vsc -Value "D:\Program Files\VSCode\Code.exe";
$HOSTS = "$env:SystemRoot\system32\drivers\etc\hosts";
$Desktop = "$env:USERPROFILE\Desktop"
$Documents = "$env:USERPROFILE\Documents"
$TimestampServer = "http://timestamp.digicert.com"
Set-Location D:\Scripts;
只是一个参考。
@'blabla'@
更易于阅读,我只是从互联网上复制了它。 :) - Chenry LeePowerShell可以处理多行字符串(包括各种类型的字符串;这不仅限于here-strings),因此实际上应该可以使用以下方法:
Write-Host 'first line
second line
third line'
Write-Host
。如果您正在从外部文件中读取,则同样可以使用Get-Content
:Get-Content ascii.txt
如果你真的需要使用Write-Host,则可以使用以下方法:
Get-Content | % { Write-Host $_ }
Write-Host " *** ** ** ** ** * ********
>> **** ******* ********* *
>> ** ***** ******** *
>> * *** ****** *
>> * **** *
>> **
>> *
>> "
更新: 删除反斜杠,因为在PowerShell中不需要它们。
Here-strings(这里字符串)也是传递给 Write-Host
的完美参数:
$multiplelines = @"
Multi-lines?!
Well ... Why
not?
"@
Write-Host $multiplelines