Thursday, March 6, 2014

Fun With PowerShell (Make it Speak)

In my downtime, I wanted to have some fun with PowerShell and make it speak using Add-Type -AssemblyName System.Speech. I have always written an age calculation script in every scripting language I have learned and I realized I had never done one in PowerShell. So I wrote one and decided to have it speak to you. Here it is:

---------------------------------------------------------------------------------
######################################## 
# Funscript.ps1                        # 
# http://imjustanengineer.blogspot.com #
# Written by DJ 3/2014                 #
######################################## 

Add-Type -AssemblyName System.Speech
$synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer

Write-Host This script will tell you how old you will be in $year and will speak to you. Make sure your speakers are on.;
$synthesizer.Speak("This script will tell how old you will be in ($year) and will speak to you. Make sure your speakers are on. ") | Out-Null
$name = Read-Host 'What is your name?';
$synthesizer.Speak("Hello, ($name) ,!") | Out-Null
$yb = Read-Host 'What year were you born?';
$synthesizer.Speak("Thank you, ($name) ,") | Out-Null
$date = Get-Date;
$year = + $date.Year;
$today = Get-Date -Format D;
$span = $year - $yb;
Write-Host Hello, $name. Today is $today. You will be $span years old this year. When is the party?;
$synthesizer.Speak("Hello, ($name). Today is ($today). You will be ($span) years old this year. When is the partee? ") | Out-Null
[System.Console]::Beep(262,200)
[System.Console]::Beep(262,200)
[System.Console]::Beep(294,200)
[System.Console]::Beep(262,200)
[System.Console]::Beep(349,200)

[System.Console]::Beep(330,200)
[System.Console]::Beep(262,200)
[System.Console]::Beep(262,200)
[System.Console]::Beep(294,200)
[System.Console]::Beep(262,200)
[System.Console]::Beep(392,200)
[System.Console]::Beep(349,200)
[System.Console]::Beep(262,200)
[System.Console]::Beep(262,200)
[System.Console]::Beep(482,200)
[System.Console]::Beep(440,200)
[System.Console]::Beep(349,200)
[System.Console]::Beep(330,200)
[System.Console]::Beep(294,200)
[System.Console]::Beep(482,200)
[System.Console]::Beep(482,200)
[System.Console]::Beep(440,200)
[System.Console]::Beep(349,200)
[System.Console]::Beep(392,200)
[System.Console]::Beep(349,200)




I would like to see what you can come up with. Be sure to leave your comments and suggestions below.