[玩转系统] 星期五乐趣 - PowerShell 圣诞节提示
作者:精品下载站 日期:2024-12-14 08:00:36 浏览:13 分类:玩电脑
星期五乐趣 - PowerShell 圣诞节提示
是时候写一篇新的周五有趣文章了。这些文章用“有趣”的方式教你如何使用PowerShell。最终目标可能很愚蠢,但希望这些技术和概念有用。今天我更新了提示功能。您可以通过创建一个名为“prompt”的函数并将其加载到 PowerShell 会话中来自定义 PowerShell 提示符。
提示功能仅在您的 PowerShell 会话运行时持续。跑步:
get-content function:prompt
查看您当前的提示。
因为圣诞节即将到来,并且考虑到 2020 年的情况,我们可能想要一些东西来点亮我们的一天。我有一个 PowerShell 提示功能,可以为您提供圣诞节倒计时,并包含表情符号和颜色。
<#
display a colorful Christmas countdown prompt
?? Christmas in 17.09:44:21 ⛄✨ PS C:\scripts>
this prompt requires a TrueType font
you would put this in your profile script so that it
only runs in December 1-24
#>
if ((Get-Date).Month -eq 12 -AND (Get-Date).Day -lt 25) {
#dot source the emoji script
. C:\scripts\PSEmoji.ps1
#load the Christmas prompt
Function Prompt {
#get current year
$year = (Get-Date).year
#get a timespan between Christmas for this year and now
$time = [datetime]"25 December $year" - (Get-Date)
#turn the timespan into a string and strip off the milliseconds
$timestring = $time.ToString().Substring(0, 11)
#get random string of decorative characters
#they can be pasted emojis or created from values
$Snow = "❄️"
# $snow = ConvertTo-Emoji 0x2744
# $sparkles = "✨"
$sparkles = ConvertTo-Emoji 0x2728
#$snowman = "⛄"
$snowman = ConvertTo-Emoji 0x26C4
$santa = ConvertTo-Emoji 0x1F385
$mrsClaus = ConvertTo-Emoji 0x1F936
$tree = ConvertTo-Emoji 0x1F384
$present = ConvertTo-Emoji 0x1F381
$notes = ConvertTo-Emoji 0x1F3B5
$bow = ConvertTo-Emoji 0x1F380
$star = ConvertTo-Emoji 127775
$shootingStar = ConvertTo-Emoji 127776
$myChars = $santa, $mrsClaus, $tree, $present, $notes, $bow, $star, $shootingStar, $snow, $snowman,$sparkles
#get a few random elements for the prompt
$front = -join ($myChars | Get-Random -Count 2)
$back = -join ($myChars | Get-Random -Count 2)
#the text to display
$text = "Christmas is coming in $timestring"
#get each character in the text and randomly assign each a color using an ANSI sequence
$colorText = $text.tocharArray() | ForEach-Object {
$i = Get-Random -Minimum 1 -Maximum 50
switch ($i) {
{ $i -le 50 -AND $i -ge 45 } { $seq = "$([char]0x1b)[1;5;38;5;199m" }
{ $i -le 45 -AND $i -ge 40 } { $seq = "$([char]0x1b)[1;5;38;11;199m" }
{ $i -le 40 -AND $i -ge 30 } { $seq = "$([char]0x1b)[1;38;5;50m" }
{ $i -le 20 -and $i -gt 15 } { $seq = "$([char]0x1b)[1;5;38;5;1m" }
{ $i -le 16 -and $i -gt 10 } { $seq = "$([char]0x1b)[1;38;5;47m" }
{ $i -le 10 -and $i -gt 5 } { $seq = "$([char]0x1b)[1;5;38;5;2m" }
default { $seq = "$([char]0x1b)[1;37m" }
}
"$seq$_$([char]0x1b)[0m"
} #foreach
#write the prompt text to the host on its own line
Write-Host "$front $($colortext -join '') $back" #-NoNewline #-foregroundcolor $color
#the function needs to write something to the pipeline
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
} #end function
} #If December
您可以点源脚本文件,如果日期在 12 月 12 日和圣诞节之间,它将加载新的提示。提示将计算一个时间跨度,指示距离圣诞节还有多长时间。
$year = (Get-Date).year
#get a timespan between Christmas for this year and now
$time = [datetime]"25 December $year" - (Get-Date)
#turn the timespan into a string and strip off the milliseconds
$timestring = $time.ToString().Substring(0, 11)
函数的其余部分是围绕此文本进行修饰:
#the text to display
$text = "Christmas is coming in $timestring"
我将显示从一组字符中随机选择的表情符号。您可以将表情符号粘贴到代码中,也可以使用我的 ConvertTo-Emoji 函数来转换十六进制值。我正在使用的粘贴表情符号可能无法在此处正确显示,因此我添加了等效表情符号。这是提示脚本中点源的脚本。
#These commands work best in a PowerShell session running in Windows Terminal.
# http://www.unicode.org/emoji/charts/full-emoji-list.html
Function ConvertTo-Emoji {
[cmdletbinding()]
[alias("cte")]
Param(
[parameter(Position = 0, Mandatory, ValueFromPipeline, HelpMessage = "Specify a value like 0x1F499 or 128153")]
[int]$Value
)
Process {
if ($env:wt_Session -OR ($host.name -match "studio")) {
[char]::convertfromutf32($Value)
}
else {
Write-Warning 'This command is only supported when running in Windows Terminal at this time.'
}
}
}
Function Show-Emoji {
[cmdletbinding()]
Param(
[parameter(Position = 0, Mandatory,HelpMessage = "Enter the starting Unicode value")]
[int32]$Start,
[Parameter(HelpMessage = "How many items do you want to display?")]
[int]$Count = 20
)
Write-verbose "Starting at $Start and getting $count items"
$counter = 1
Do {
for ($i=1;$i -le 5;$i++) {
write-verbose "Counter = $counter i = $i"
$item = "{0} {1} " -f (ConvertTo-Emoji ($start)),$start
if ($counter -le $count) {
write-host $item -NoNewline
$counter++
$start++
}
}
write-host "`r"
} until ($counter -ge $count)
}
提示的最后一部分是获取字符串并使用 ANSI 转义序列显示每个字符。我已经编写了该序列,因此它可以在 Windows PowerShell 和 PowerShell 7 中运行。
#get each character in the text and randomly assign each a color using an ANSI sequence
$colorText = $text.tocharArray() | ForEach-Object {
$i = Get-Random -Minimum 1 -Maximum 50
switch ($i) {
{ $i -le 50 -AND $i -ge 45 } { $seq = "$([char]0x1b)[1;5;38;5;199m" }
{ $i -le 45 -AND $i -ge 40 } { $seq = "$([char]0x1b)[1;5;38;11;199m" }
{ $i -le 40 -AND $i -ge 30 } { $seq = "$([char]0x1b)[1;38;5;50m" }
{ $i -le 20 -and $i -gt 15 } { $seq = "$([char]0x1b)[1;5;38;5;1m" }
{ $i -le 16 -and $i -gt 10 } { $seq = "$([char]0x1b)[1;38;5;47m" }
{ $i -le 10 -and $i -gt 5 } { $seq = "$([char]0x1b)[1;5;38;5;2m" }
default { $seq = "$([char]0x1b)[1;37m" }
}
"$seq$_$([char]0x1b)[0m"
} #foreach
一些序列包括闪烁的字符。整行文本被写入主机。
Write-Host "$front $($colortext -join '') $back"
提示使用默认位置代码结束。
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
最初,我将 Write-Host 与 -NoNewLine 一起使用,以便文本位于位置之前。但我喜欢它独立的外观。想看吗?
将文件保存到同一目录,然后点源提示脚本。
. c:\scripts\christmasprompt.ps1
由于 ANSI 颜色是随机的,因此每次按 Enter 键都会得到不同的结果。闪烁的字符使您的提示闪闪发光。
是的,这很愚蠢,没有任何实际目的,除非你是一个严重的拖延症患者并且需要提醒!但看看 PowerShell 技术:点源、开关、函数、子表达式、时间跨度、If 语句。这些都是您在实际 PowerShell 工作中将使用的所有内容。如果您仍在学习 PowerShell,有时查看以不同方式演示这些内容会有所帮助。
我希望你至少能尝试一下代码。该提示仅在 PowerShell 运行时持续,因此不存在风险。享受!
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag