I've been prepping my demo scripts for Techmentor using the ubiquitous Start-Demo, and realized I could take things further. I mean, why do I have to do all the talking? Windows 7 has a terrific text to speech engine so why not take advantage of it. With a little work I could create a virtual demo that doesn't require a real person, other than to kick it off. Thus was born Start-VirtualDemo.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
My function relies on the SAPI.SPVoice COM object and works best on Windows 7. The voice isn't perfect but much better than previous versions and at least in my tests more than adequate. The function needs a text file "script". In this file, write the text you want to be read putting a # at the beginning of the line. Then type any PowerShell expressions you would like to demonstrate. For now, the command must be all in one line. Here's a sample text file.
#Hello, I am Serenity, and I will be your virtual guide today. #Let me demonstrate some basic PowerShell commands. #First, let's find all the running services on this computer. Get-Service | Where {$_.status -eq "Running"} #What about processes? Get-Process -name "PowerShell" #Here are the running PowerShell sessions #PowerShell also lets you navigate other data stores like the registry. CD hklm:\Software dir get-itemproperty "microsoft\windows nt\currentversion" #Do you think this would be useful? #WMI is a lot of fun and easy to use in PowerShell. Get-Wmiobject Win32_logicaldisk -filter "drivetype=3" #This command will return fixed logical disk information. #If you need help on how to run a command, just ask. Get-Help Get-WMIObject #Isn't that easy? #Remember, those who forget to script are doomed to repeat their work." #Discover more with a copy of Windows PowerShell 2.0 Tee ef em by Don Jones and Jeffery Hicks #Thank you for your attention and have a great day!
The function will strip out any blank lines so put in as many as you want to help visually organize your script. You might have to experiment and tweak some words or phrases using phonetic-like alternatives to get the correct pronunciation. The script itself is pretty straight forward.
Param( [Parameter(Position=0)] [string]$File="demo.txt", [int]$rate=-1 ) #verify the text file "script" if (-Not (Test-Path -path $File)) { Write-Error "Failed to find $file. Please verify and try again." } else { Try { #create the Voice object $voice=New-Object -ComObject "SAPI.SPVoice" #I find a slower rate works better but you may need to experiment $voice.rate=$rate } Catch { #Oops. Write-Warning "Unable to create the Voice object." } if ($voice) { #cleas the screen Clear-Host #store the current location so we can return to it Push-Location #get all data from the text file, filtering out blank lines $data=Get-Content $file | where {$_} Write-Host "Demo Started." -ForegroundColor Cyan #go through each line. Lines that start with # will be spoken. #All other lines will be displayed in a pseudo prompt and then executed foreach ($line in $data) { If ($line.StartsWith("#")) { $voice.Speak($line.Substring(1)) | Out-Null } else { #display the command using the current location in a prompt #add a blank line before and after the command Write-host ("`nPS {0}> {1} `n" -f (Get-Location).path,$line) #pause a few seconds to give the viewer time to read the command sleep 3 #execute the command Invoke-Expression $line | Out-Default } } #Foreach #return to the original location Pop-Location } #if $voice } #else file found Write-Host "Demo Finished." -ForegroundColor Cyan
To run the script all you need to do is specify the text file. Or use the default value from the parameter. The script will store your current location and return to it at the end so if you change locations in your demo nothing will have changed when your demo ends. The script will display the demo command in a pseudo prompt, using your current location.
#display the command using the current location in a prompt #add a blank line before and after the command Write-host ("`nPS {0}> {1} `n" -f (Get-Location).path,$line)
I have to say I thought this turned out quite nicely. Here's a video demo.
By the way, I was only kidding about not having to talk and actually do the demos. Still, there are some intriguing possibilities that come to mind with this.
You can download a zip file with the Start-VirtualDemo script any my demo text file here.
That’s great. Wonderfully elegant demonstration of it’s capability. I’ll be making use of this for training with PowerShell. Plus now it’s in text format, it’ll fit nicely into version control.
Thanks. The tricky part can be getting the text and commands in the right order so that you can see something on the screen before the voice over. And of course some text needs to be tweaked to evoke a decent pronunciation.
Another Friday Gem! Thanks!