Monday, December 5, 2016

PowerShell has a speech synthesis class with a speak method (who knew ?) so that the host console can now 'talk' with your user.
I have been having a little fun with it.
#The engine is exposed through the class [System.Speech] by adding the type:
Add-Type -AssemblyName System.Speech
#We then build a named instance (an instance of the class).
#We store ours in the variable tts (text to speech):
#create the instance with new-object
$tts = New-Object System.Speech.Synthesis.SpeechSynthesizer
#Now we can call the speak method on the instance by dot sourcing but before that we need to have a text to read:
$readFileOutLoud = <path to text file>
#I used my config script path location.
#I can now call the method:
$tts.Speak($readFileOutLoud)

It worked flawlessly.
Could we just as readily include any custom spoken out-put through means of a variable that stores our custom message ?
$myPhrase = {"PowerShell ROCKS"}
#test our variable
#$myPhrase shows the expected output "PowerShell ROCKS"
#call the speak method:
$tts.Speak($myPhrase)
Success !
You can use any variable from your current session with the correct data type or you can create one for your use
and that, that is all, just 'to cool for school'.
#Let's build a function that will both tell my user something and write the same output to the console host:
function showAndTell
{$myPhrase,
$tts.Speak($myPhrase)
}
#call the function by typing its' name
#showAndTell
That's pretty cool.
But not cool enough.
Include a little boiler-plate so that the custom in session function is findable to PowerShell's' get-Help and Get-Command functionalities:
function showAndTell
{<#
 .SYNOPSIS
  The function is an advanced command that out-puts both a verbose message and an audio message.
 .DESCRIPTION
  The function both 'speaks' to your user and outputs to the
  console host screen your custom message.
 .PARAMETER ParameterA
  The description of the ParameterA parameter.
 .PARAMETER ParameterB
  The description of the ParameterB parameter.
 .EXAMPLE
  type: showAndTell
  This example shows how to run the command.
 .INPUTS
  System.Object
 .OUTPUTS
  The function writes an informational message to the
  console host and outputs an audio message to your user.
 .NOTES
  The function is executed in serial fashion as
  a first in first out operation.
   #functionName : showAndTell
   #codeAuthor   : Steven McKnight
   #Company      : MpMxLabs
   #ParallelLevel : OrderOneSerial (-s-)
   #Source       : Cyberex
   #written at   : Piety Hill
   #Copyright (c) : 2016
 .ROLE
  OrderOne
 .COMPONENT
  Dependencies
 .FUNCTIONALITY
  Serial (-s-)
 .LINK
  about_Functions_Advanced
 .LINK
  about_Comment_Based_Help
 .LINK
  about_Functions
#>
$myPhrase,
$tts.Speak($myPhrase)
}
There you have it. Deeply cool.

I want to thank #Ryan Mattfield for his fine post here of
previous that got me to partying on text to speech.