Cuma Eğlencesi: Size Bir Noel Hediyesi
Yıllar geçtikçe PowerShell topluluğundaki birçok kişi Noel ve tatille ilgili öğeleri paylaştı. Bunları topladım ve bazı durumlarda biraz ayar yaptım. Bu yıl sizin için hepsini bir modülde toplamaya karar verdim. Bu, PowerShell 2.0 ve sonraki sürümlerde çalışacaktır.
#requires -version 2.0
<#
Christmas.psm1
A PowerShell module with a collection of holiday themed functions.
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
#>
Function Start-Jingle {
Param ([switch]$FullSong)
#The first number is the reciprocal of the duration and the rest is the
#note and octave (which we use for getting the frequency).
#For example, 4A4 is a quarter A note
if ($fullSong) {
$notes = write `
4A4 4A4 2A4 4A4 4A4 2A4 4A4 4C4 4F3 8G3 1A4 `
4Bb4 4Bb4 4Bb4 8Bb4 4Bb4 4A4 4A4 8A4 8A4 4A4 4G3 4G3 4A4 2G3 2C4 `
4A4 4A4 2A4 4A4 4A4 2A4 4A4 4C4 4F3 4G3 1A4 4Bb4 4Bb4 4Bb4 4Bb4 `
4Bb4 4A4 4A4 8A4 8A4 4C4 4C4 4Bb4 4G3 1F3 4C3 4A4 4G3 4F3 2C3 8C3 8C3 `
4C3 4A4 4G3 4F3 1D3 4D3 4Bb4 4A4 4G3 '1E3' 4C4 4C4 4Bb4 4G3 `
1A4 4C3 4A4 4G3 4F3 1C3 4C3 4A4 4G3 4F3 1D3 `
4D3 4Bb3 4A4 4G3 4C4 4C4 4C4 8C4 8C4 4D4 4C4 4Bb4 4G3 4F3 2C4 4A4 4A4 2A4 `
4A4 4A4 2A4 4A4 4C4 4C3 8G3 1A4 4Bb4 4Bb4 4Bb4 8Bb4 4Bb4 4A4 4A4 8A4 8A4 `
4A4 4G3 4G3 4A4 2G3 2C4 4A4 4A4 2A4 4A4 4A4 2A4 4A4 4C4 4F3 8G3 `
1A4 4Bb4 4Bb4 4Bb4 4Bb4 4Bb4 4A4 4A4 8A4 8A4 4C4 4C4 4Bb4 4G3 1F3
} else {
#first verse only
$notes = write 4A4 4A4 2A4 4A4 4A4 2A4 4A4 4C4 4F3 8G3 1A4 `
4Bb4 4Bb4 4Bb4 8Bb4 4Bb4 4A4 4A4 8A4 8A4 4A4 4G3 4G3 4A4 2G3 2C4 `
4A4 4A4 2A4 4A4 4A4 2A4 4A4 4C4 4F3 4G3 1A4 4Bb4 4Bb4 4Bb4 4Bb4 `
4Bb4 4A4 4A4 8A4 8A4 4C4 4C4 4Bb4 4G3 1F3
}
function Play([int] $freq, [int] $duration)
{
[console]::Beep($freq, $duration);
}
#
# Note is given by fn=f0 * (a)^n
# a is the twelth root of 2
# n is the number of half steps from f0, positive or negative.
# f0 used here is A4 at 440 Hz
#
$f0 = 440;
$a = [math]::pow(2,(1/12)); # Twelth root of 2
function GetNoteFreq([string]$note)
{
# n is the number of half steps from the fixed note.
$note -match '([A-G#]{1,2})(\d+)' | out-null
$octave = ([int] $matches[2]) - 4;
$n = $octave * 12 + ( GetHalfStepsFromA $matches[1] );
$freq = $f0 * [math]::Pow($a, $n);
return $freq;
}
function GetHalfStepsFromA([string] $note)
{
switch($note)
{
'A' { 0 }
'A#' { 1 }
'Bb' { 1 }
'B' { 2 }
'C' { 3 }
'C#' { 4 }
'Db' { 4 }
'D' { 5 }
'D#' { 6 }
'Eb' { 6 }
'E' { 7 }
'F' { 8 }
'F#' { 9 }
'Gb' { 9 }
'G' { 10 }
'G#' { 11 }
'Ab' { 11 }
}
}
$StandardDuration = 1000;
foreach($note in $notes)
{
$note -match '(\d)(.+)' | out-null
$duration = $StandardDuration / ([int] $matches[1]);
$playNote = $matches[2];
$freq = GetNoteFreq $playNote;
#write-host $playNote $freq $duration;
Play $freq $duration
start-sleep -milli 50
}
} #end Start-Jingle
Function New-Tree {
Param([string]$Caption="Happy Holidays from PowerShell")
cls
write-host "`n"
$Peek = " ^ "
$tree = "/|\"
$i = 20
$pos = $host.ui.rawui.CursorPosition
write-host -fore 'Red' ($peek.PadLeft($i -1).PadRight(36) * 2)
write-host -fore 'green' ($tree.PadLeft($i -1).PadRight(36) * 2)
1..16 |% {
$tree = $tree -replace "/(.*)\\",'//$1\\'
write-host -fore 'green' ($tree.PadLeft($i).PadRight(36) * 2)
$i++
}
write-host -fore 'green' ("|||".PadLeft(19).PadRight(36) *2 )
write-host -fore 'green' ("|||".PadLeft(19).PadRight(36) *2)
$rect = New-Object System.Management.Automation.Host.Rectangle
$rect.top = $pos.y
$rect.Right = 70
$rect.Bottom = $pos.y + 19
$buffer = $host.ui.rawui.getbuffercontents($rect)
$R = New-Object system.random
$ball = New-Object System.Management.Automation.Host.BufferCell
$ball.Character = '@'
$ball.backgroundColor = $host.ui.rawui.BackgroundColor
1..120 |% {
sleep -m 120
$rx = $r.Next(19)
$ry = $r.Next(70)
$ball.ForegroundColor = $r.next(16)
if ($buffer[$rx,$ry].Character -eq '/') {$buffer[$rx,$ry] = $ball}
if ($buffer[$rx,$ry].Character -eq '\') {$buffer[$rx,$ry] = $ball}
$host.ui.rawui.SetBufferContents($pos,$buffer)
}
Write-Host "`n$([char]14) **$Caption** $([char]14) `n" -ForegroundColor Red
} #end New-Tree
Function Get-Greeting {
cls
$XmasMsg =
@"
.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:.
. * .
. /.\ Merry Christmas .
. /..'\ from all of us .
. /'.'\ in PowerShell .
. /.''.'\ .
. /.'.'.\ .
. /'.''.'.\ .
. ^^^[_]^^^ .
. .
. .
. .
.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:.
"@
Write-Host -ForegroundColor Green $XmasMsg
Start-Sleep 5
$Msg1 =
@"
_...
o_.-"` `\
.--. _ `'-._.-'""-; _
.' \`_\_ {_.-a"a-} _ / \
_/ .-' '. {c-._o_.){\|` |
(@`-._ / \{ ^ } \\ _/
`~\ '-._ /'. } \} .-.
|>:< '-.__/ '._,} \_/ / ())
| >:< `'---. ____'-.|(`"`
\ >:< \\_\\_\ | ;
\ \\-{}-\/ \
\ '._\\' /)
'. /(
`-._ _____ _ _____ __.'\ \
/ \ / \ / \ \ \
_.'/^\'._.'/^\'._.'/^\'.__) \
,==' `---` '---' '---' )
`"""""""""""""""""""""""""""""""`
"@
$msg2 =
@"
___
/` `'.
/ _..---;
| /__..._/ .--.-.
|.' e e | ___\_|/____
(_)'--.o.--| | | |
.-( `-' = `-|____| |____|
/ ( |____ ____|
| ( |_ | | __|
| '-.--';/'/__ | | ( `|
| '. \ )"";--`\ /
\ ; |--' `;.-'
|`-.__ ..-'--'`;..--'`
"@
$msg3 =
@"
* ,
_/^\_
< >
* /.-.\ *
* `/&\` *
,@.*;@,
/_o.I %_\ *
* (`'--:o(_@;
/`;--.,__ `') *
;@`o % O,*`'`&\
* (`'--)_@ ;o %'()\ *
/`;--._`''--._O'@;
/&*,()~o`;-.,_ `""`)
* /`,@ ;+& () o*`;-';\
(`""--.,_0 +% @' &()\
/-.,_ ``''--....-'`) *
* /@%;o`:;'--,.__ __.'\
;*,&(); @ % &^;~`"`o;@(); *
/(); o^~; & ().o@*&`;&%O\
`"="==""==,,,.,="=="==="`
__.----.(\-''#####---...___...-----._
'` \)_`"""""`
.--' ')
o( )_-\
`"""` `
"@
$msg4 =
@"
.--------.
* . |________| . *
| __|/\
* .-'======\_\o/.
/___________<>__\
|||||| / (o) (o) \
|||||| | _ O _ | .
. |||||| | (_) (_) |
|||||| \ '---' / *
\====/ [~~~~~~~~~]
\\// _/~||~`|~~~~~\_
_||-'`/ || | \`'-._ *
* .-` )| ; || |) ; '.
/ `--.| || | | `\
| \ |||||) |-, \ .
\ .; _ ; |_, |
`'''||` ,\ (_) /, `.__/
||.` '. .' `. *
* || ` ' ' ` \
|| ;
. * || | .
|| | *
|| |
.__.-""-.__.-"""|| ;.-"""-.__.-""-.__.
|| /
||'. .'
|| '-._ _ _ _ _.-'
`""`
"@
$msg5 =
@"
_
.-(_)
/ _/
.-' \
/ '.
,-~--~-~-~-~-,
{__.._...__..._} ,888,
,888, /\##" 6 6 "##/\ ,88' `88,
,88' '88,__ |(\` (__) `/)| __,88' `88
,88' .8(_ \_____\_ '----' _/_____/ _)8. 8'
88 (___)\ \ '-.__ __.-' / /(___)
88 (___)88 | '--' | 88(___)
8' (__)88,___/ \___,88(__)
__`88,_/__________________\_,88`__
/ `88, |88| ,88' \
/ `88, |88| ,88' \
/____________`88,_\88/_,88`____________\
/88888888888888888;8888;88888888888888888\
/^^^^^^^^^^^^^^^^^^`/88\\^^^^^^^^^^^^^^^^^^\
/ |88| \============, \
/_ __ __ __ _ __ |88|_|^ MERRY | _ ___\
|;:. |88| | CHRISTMAS! | |
|;;:. |88| '============' |
|;;:. |88| |
|::. |88| |
|;;:' |88| |
|:;, |88| |
'---------------------""""---------------------'
"@
For($i = 1; $i -le 5; $i++) {
If([bool]!($i%2)) {
Write-Host -ForegroundColor Green (Get-Content Variable:msg$i)
} else {
Write-Host -ForegroundColor Red (Get-Content Variable:msg$i)
}
Start-Sleep -Seconds 3
} #for
} #end Get-Greeting
Function Get-Holiday {
#get start-jingle script block
$sb = (dir function:start-jingle).scriptblock
start-job -ScriptBlock $sb -Name JingleJob | out-null
start-sleep -Seconds 2
New-Tree
Wait-Job -Name JingleJob | Remove-Job
} #end Get-Holiday
Function Prompt {
$today = Get-Date
#test if today is Christmas
if ($today.Month -eq 12 -AND $today.day -eq 25) {
$text = "Merry Christmas!"
}
else {
$currYr = $Today.Year
$xmas = [datetime]"12/25/$currYr"
#test if Christmas has passed for this year.
if ($today -gt $xmas) {
$xmas = $xmas.AddYears(1)
}
$tspan = ($xmas-$today).ToString()
#strip off ms
$time=$tspan.substring(0,$tspan.LastIndexOf("."))
$text="[**~Christmas in $($time)~**]"
}
#write each character in a different color
$text.tocharArray() | foreach {
if ((Get-Random -min 1 -max 10) -gt 5) {
$color="RED"
}
else {
$color="GREEN"
}
write-host $_ -nonewline -foregroundcolor $color
}
Write (" PS " + (Get-Location) + "> ")
} #end function
Export-ModuleMember -function Start-Jingle,New-Tree,Get-Greeting,Get-Holiday,PromptFonksiyonlara göz attığınızda muhtemelen bu fonksiyonlardan bazılarının ne işe yaradığını anlayabilirsiniz ama sürprizi bozmak istemiyorum. Ama işte bir örnek.
Modülün bir kopyasını alın ve içindeki komutlara bakın.
get-command -module ChristmasPrompt'u çalıştırmanıza gerek yok çünkü modülü yüklediğinizde otomatik olarak çalışacaktır. Modülü kaldırırsanız eski isteminizi geri alamayacağınızı unutmayın. PowerShell'i yeniden başlatmanız yeterlidir.
Mutlu ve sağlıklı bir tatil geçirmenizi ve muhteşem bir Yeni Yıl geçirmenizi dilerim.
