Thursday, March 19, 2015

#48 : Speaking Script in Powershell

A script can be written which can talk to you and speak out - "Sir! Your copy job has completed." or something like "Sir! Please press any key to continue!". These all are not a fiction with Powershell and you don't have to write much more lines of code to accomplish. In fact, this is just a one liner!

(new-object -com SAPI.SpVoice).speak("One more Powershell tips")

So, I decided to write a completely talking script which will copy all file from one location to another.



#--Script will demonstrate File-copy with speaking voice. --#
$source_dir="E:\folder1"
$target_dir="E:\folder2"
#-- List number of files --#
$count=0
ls ${source_dir} | select Name | foreach { $count++}
(new-object -com SAPI.SpVoice).speak("Sir! Total ${count} files to be copied. Please wait for sometime, Thank you!!")
Copy-Item "$source_dir\*.*" "$target_dir" -recurse
if ($? -ne $True )
{
(new-object -com SAPI.SpVoice).speak("Sorry! Your file-copy operation failed, please troubleshoot!!")
}
else
{
(new-object -com SAPI.SpVoice).speak("Sir! Your file-copy operation completed successfully, Thank you!!")
}



One you run it, you would see Merlin talking ...




Once you run it, you will have more ideas about writing any interactive script which could give messages verbally. I am now thinking to write something which will take commands from microphone!!

No comments:

Post a Comment

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...