We made it to the end of the week, and I don't know about you but I have my head buried in PowerShell work. But you know what they say about all work and no fun...so I figured I'd take a break from serious PowerShell and do something a little fun. Although, I suppose you might pick up a pointer or too along the way. This installment of Friday Fun, actually the first one, is about making beautiful music with Powershell.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
I trust you're familiar with the `a escape sequence. If not, try this.
[cc lang="powershell"]
PS C:\> write `a
[/cc]
What PowerShell is really doing is calling the Beep method of the [Console] .NET class. But there's a bit more that can be done. You can specify an frequency and a duration, in milliseconds.
[cc lang="powershell"]
PS C:\> [console]::beep(440,500)
[/cc]
In fact, given the right frequencies you can play "music" in PowerShell.
[cc lang="PowerShell"]
$scale=@{
MidC=261.6
CSharp=277.2
D=293.7
DSharp=311.1
E=329.6
F=349.2
FSharp=370.0
G=392.0
GSharp=415.3
A=440.0
ASharp=466.2
B=493.9
C=523.2
}
#play a chromatic scale
write-host "First octave" -ForegroundColor Cyan
$scale.values | sort | foreach {[console]::beep($_,125)}
[/cc]
Or, you can be more avant-garde and listen to the music of chaos. I put together a script that plays a set number of notes with a random frequency and of random duration.
[cc lang="Powershell"]
#requires -version 2.0
#usage: music.ps1 64
Param([int]$Length=16)
#function to create the musical tones
Function Make-Music {
Param(
[Parameter(Position=0, ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
[int]$frequency=400,
[Parameter(Position=1, ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
[int]$duration=500)
Process {
[console]::Beep($frequency,$duration)
}
} #function
Write-Host "Generating a $length note sequence" -ForegroundColor Cyan
$notes=for ($i=1;$i -le $length;$i++) {
$f=Get-Random -Minimum 500 -Maximum 2500
$r=Get-Random -Minimum 1 -Maximum 10
New-Object -TypeName PSObject -Property @{
Frequency=$f
Duration=$r*125
}
} #for
$notes | make-music
$a=Read-Host "Do you want to save this to an XML file? [YN]"
if ($a -match "^Y$") {
$file=Read-Host "Enter a filename or press Enter to cancel."
if ($file) {
$notes | Export-Clixml -Path $file
}
}
[/cc]
The script takes an integer for the length of the "musical" sequence. I used a For construct to create a series of custom objects with a frequency and duration.
[cc lang="powershell"]
$notes=for ($i=1;$i -le $length;$i++) {
$f=Get-Random -Minimum 500 -Maximum 2500
$r=Get-Random -Minimum 1 -Maximum 10
New-Object -TypeName PSObject -Property @{
Frequency=$f
Duration=$r*125
}
} #for
[/cc]
This collection of objects is then piped to a nested function that can accept pipelined input which generates the sequence. If you like what you hear you can also export the sequence to an XML file. You'll be prompted for a file name. With the XML "score" you can play your "music" at any time, assuming you've loaded the Make-Music function into your shell.
[cc lang="PowerShell"]
PS C:\> import-clixml c:\work\opus1.xml | Make-Music
[/cc]
You can download a zip file with everything you need to get started here.
The Sound of Files
dir C:\Somewhere | where {$_.length -gt 37 -AND $_.length -lt 32767} | foreach {make-music $_.length 250}