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

[玩转系统] 你的圣诞节 PowerShell 提示

作者:精品下载站 日期:2024-12-14 07:53:31 浏览:16 分类:玩电脑

你的圣诞节 PowerShell 提示


继续我的 PowerShell 提示乐趣,因为我们正处于圣诞节期间。我带回了圣诞节倒计时提示。我已经更新,因此它应该可以在传统控制台和 PowerShell ISE 中工作。

[玩转系统] 你的圣诞节 PowerShell 提示

该提示显示一条随机着色的倒计时消息以及一些随机装饰。您可以在我的 Github 存储库上找到更新的代码作为要点。

PSChristmasPrompt.ps1:


 

This should work in the console with a True Type font and
the Powershell ISE

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) {
  #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
    $myChars = "♫","♪","♦","*","֍"
    $front = -join ($myChars | Get-Random -count 2)  
    #-join (14,15,42 | Get-Random -Count 2 | Foreach { $_ -as [char] })
    $back = -join ($myChars | Get-Random -count 2)
    #-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-Object {

    $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

    #the function needs to write something to the pipeline
    
    " PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

} #end function

} #If December

我建议将代码放入您的 PowerShell 配置文件脚本中。仅当当前日期是 12 月且日期小于 25 日时,才会加载该函数。另一种方法是将 IF 逻辑移至提示函数内。如果是圣诞节,请使用节日主题提示。否则,请使用默认提示。

提示功能也应该跨平台工作。

[玩转系统] 你的圣诞节 PowerShell 提示

[玩转系统] 你的圣诞节 PowerShell 提示

如果您没有获得特殊字符,您可能需要更改字体。这应该适用于大多数 True Type 字体。修改提示并从 charmap.exe 粘贴您想要的任何字符。

将此视为提前的圣诞礼物。享受。

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

取消回复欢迎 发表评论:

关灯