Một thông điệp Giáng sinh lễ hội
Một năm sắp trôi qua và việc duy trì năng suất ngày càng khó khăn hơn. Vì vậy, tôi đang cố gắng kết hợp việc giải trí trong khi vẫn cố gắng giúp bạn tìm hiểu PowerShell.
Tôi đã viết một tập lệnh PowerShell đơn giản chọn ngẫu nhiên một câu trích dẫn theo chủ đề Giáng sinh và hiển thị theo kiểu đầy màu sắc. Khi bạn chạy tập lệnh hoạt động trong cả bảng điều khiển và PowerShell ISE, bạn sẽ nhận được kết quả như thế này:
Tôi đã chia sẻ tập lệnh trên Github.
ChristmasWishes.ps1:
<#
Display a randomly selected holiday quote in a festive color scheme
you can find more Christmas quotes at
http://www.brainyquote.com/quotes/topics/topic_christmas.html
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
#>
#The quote should include the author separated by " - ".
$texts = @(
"I will honor Christmas in my heart, and try to keep it all the year. - Charles Dickens",
"Merry Christmas to all and to all a good night! - Santa Claus",
"Christmas isn't a season. It's a feeling. - Edna Ferber",
"It is a fine seasoning for joy to think of those we love. - Moliere",
"Christmas, children, is not a date. It is a state of mind. - Mary Ellen Chase",
"Maybe Christmas, the Grinch thought, doesn't come from a store. - Dr. Seuss",
"Christmas waves a magic wand over this world, and behold, everything is softer and more beautiful. - Norman Vincent Peale"
)
#get random text
$text = Get-Random $texts
#split the text to an array on ' - '
$split = $text -split " - "
$quote = $split[0].Trim()
$author = $split[1].Trim()
#turn the quote into an array of characters
$arr = $quote.ToCharArray()
$arr | foreach -Begin {
cls
#define an array of colors
$colors = "Red","Green","White","Magenta"
#insert a few blank lines
write-host "`n"
#insert top border
write-host ("*"*$($quote.length+6))
#insert side border
write-host '* ' -NoNewline
} -process {
#write each character in a different holiday color
Write-Host $_ -ForegroundColor (Get-Random $colors) -NoNewline
} -end {
write-host ' *'
#insert side border
Write-Host "* " -NoNewline
#write the author
Write-Host "- $author *".padleft($quote.length+4)
#insert bottom border
write-Host ("*"*$($quote.length+6))
Write-Host "`n"
}
Chúc bạn, gia đình và bạn bè có một mùa Giáng sinh và nghỉ lễ vui vẻ. Hãy nghỉ ngơi vì chúng ta sẽ gặp khó khăn trở lại vào năm 2016.