当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 周五乐趣:另一个圣诞节提示

作者:精品下载站 日期:2024-12-14 07:44:29 浏览:15 分类:玩电脑

周五乐趣:另一个圣诞节提示


[玩转系统] 周五乐趣:另一个圣诞节提示

Function Prompt {

#only change background color if in December
if ((Get-Date).Month -eq 12 -And (Get-Date).Day -lt 25) {
Switch ($host.ui.RawUI.BackgroundColor) {

"DarkGreen" { $c = "DarkRed" }
"DarkRed"   { $c = "DarkGreen" }
 Default    { 
            #get a random color
            $c = Get-Random -InputObject "DarkRed","DarkGreen"
            }

} #switch

$host.ui.RawUI.BackgroundColor = $c
}

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

}

我添加了一些逻辑,以便我的定制仅在 12 月和圣诞节之前发生。该提示将控制台的背景颜色在深红色和深绿色之间更改。第一次运行时,提示会随机选择一种颜色。您可能希望在加载提示后运行 Clear-Host 或 CLS。

之后背景颜色将切换,留下如下效果:

[玩转系统] 周五乐趣:另一个圣诞节提示

看看不同的命令如何写入控制台是很有趣的。如果您心情特别喜庆,您甚至可以结合我的两个提示。

Function Prompt {

#only change background color if in December
if ((Get-Date).Month -eq 12 -And (Get-Date).Day -lt 25) {
Switch ($host.ui.RawUI.BackgroundColor) {

"DarkGreen" { $c = "DarkRed" }
"DarkRed"   { $c = "DarkGreen" }
 Default    { 
            #get a random color
            $c = Get-Random -InputObject "DarkRed","DarkGreen"
            }

} #switch

$host.ui.RawUI.BackgroundColor = $c


#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
    $front = -join (14,15,42 | Get-Random -Count 2 | Foreach { $_ -as [char] })
    $back = -join (14,15,42 | Get-Random -Count 2 | Foreach { $_ -as [char] })
    $text="[{0}Christmas in {1}{2}]" -f $front,$timestring,$back

    #get each character in the text and randomly assign each a color
    $text.tocharArray() | foreach {

    $i = get-random -Minimum 1 -Maximum 20
    switch ($i) {
      {$i -le 20 -and $i -gt 15} { $color = "Red"}
      {$i -le 16 -and $i -gt 10} { $color = "Green" }
      {$i -le 10 -and $i -gt 5}{ $color = "DarkGreen"}
      default {$color = "White"}
    }

    #write each colorized character
    write-host $_ -nonewline -foregroundcolor $color
    } #foreach

} #if Christmas time

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

} #end prompt function

[玩转系统] 周五乐趣:另一个圣诞节提示

除了获得一些乐趣以及了解一些 PowerShell 脚本概念之外,这可能没有任何实际价值。希望您的假期购物一切顺利。周末愉快。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯