{"id":76071,"date":"2015-12-30T00:01:00","date_gmt":"2015-12-30T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/12\/30\/sounds-of-the-holidayspart-1\/"},"modified":"2019-02-18T09:20:29","modified_gmt":"2019-02-18T16:20:29","slug":"sounds-of-the-holidayspart-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/sounds-of-the-holidayspart-1\/","title":{"rendered":"Sounds of the Holidays\u2013Part 1"},"content":{"rendered":"<p><b>Summary<\/b>: MVP, Thomas Rayner, inspires holiday spirit by using Windows PowerShell to trigger some multimedia.<\/p>\n<p align=\"left\">Hello! I&rsquo;m Thomas Rayner, a proud Cloud &amp; Datacenter Management Microsoft MVP, filling in for the Scripting Guy for a couple days. You can find me on Twitter (@MrThomasRayner), or posting biweekly on my blog, <a href=\"http:\/\/www.workingsysadmin.com\/\" target=\"_blank\">WorkingSysadmin: Figuring stuff out at work<\/a>. I&rsquo;m enjoying some time off from work this holiday season by playing around with some of the multimedia resources that come with Windows.<\/p>\n<p align=\"left\">Do you have a coworker who you wish was a little more festive? I do. His name is Matthew, and I&rsquo;m going to inspire him with some nice holiday messages. All I need is a few lines of PowerShell.&nbsp;<\/p>\n<p align=\"left\">Windows has a text-to-speech utility built-in to it. The details of how exactly it works aren&rsquo;t really important to me. What I want to do is add the assembly that lets me interact with it via PowerShell:<\/p>\n<p align=\"left\" style=\"margin-left:30px\">Add-Type -AssemblyName System.Speech&nbsp;<\/p>\n<p align=\"left\">So far, so good. I&rsquo;ve added the System.Speech assembly. That by itself doesn&rsquo;t do anything other than enable me to access that assembly. What I need to do now is create a new SpeechSynthesizer object:<\/p>\n<p align=\"left\" style=\"margin-left:30px\">$synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer&nbsp;<\/p>\n<p align=\"left\">Now I&rsquo;ve got a new SpeechSynthesizer named <b>$synth<\/b>. If you use the <b>Get-Member<\/b> cmdlet on <b>$synth<\/b>, you&rsquo;ll see that it does all kinds of neat things, such as allow you to change the voice, output to different audio streams, and output to a .wav file. Most importantly, <b>$synth<\/b> has a method called <b>Speak<\/b> which takes a string, for example.<\/p>\n<p align=\"left\" style=\"margin-left:30px\">$synth.Speak(&#8216;Hello world&#8217;)&nbsp;<\/p>\n<p align=\"left\">You might need to be a bit patient. It can take a little time for the SpeechSynthesizer to turn your string into audio output, but you should hear the Windows text-to-speech engine speak the words you specified. Pretty cool!<\/p>\n<p align=\"left\">All I need to do is execute the code on Matthew&rsquo;s computer instead of mine and replace the message with something more likely to inspire some holiday spirit. That&rsquo;s as easy as using <b>Invoke-Command<\/b>. My script now looks like this:<\/p>\n<p align=\"left\" style=\"margin-left:30px\">$Computer = &#8216;Matthew-Computer&#8217;<\/p>\n<p align=\"left\" style=\"margin-left:30px\">Invoke-Command -ScriptBlock {<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Add-Type -AssemblyName System.Speech<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $synth.Speak(&#8216;Matthew, this is Ebenezer Script. I am calling to make sure you have been a good little boy this year.&#8217;)<\/p>\n<p align=\"left\" style=\"margin-left:30px\">} -ComputerName $Computer&nbsp;<\/p>\n<p align=\"left\">That&rsquo;s it! I could, however, take this a bit further and send Matthew more messages:<\/p>\n<p align=\"left\" style=\"margin-left:30px\">$Computer = &#8216;Matthew-Computer&#8217;<\/p>\n<p align=\"left\" style=\"margin-left:30px\">$MessageCount = 0<\/p>\n<p align=\"left\">$Messages = @(&#8216;Matthew, this is Ebenezer Script. I am calling to make sure you have been a good little boy this year&#8217;, &#8216;Matthew, you are going to get coal in your stocking if you are not a good boy.&#8217;,&#8217;Ebenezer script to Matthew. Come in Matthew. Please advise your good-boy status. Only good boys get gifts from Ebenezer Script.&#8217;)<\/p>\n<p align=\"left\" style=\"margin-left:30px\">while ($MessageCount -lt 100)<\/p>\n<p align=\"left\" style=\"margin-left:30px\">{<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Invoke-Command -ScriptBlock {<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Add-Type -AssemblyName System.Speech<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $synth.Speak($(Get-Random $Messages))<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; } -ComputerName $Computer<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $MessageCount++<\/p>\n<p align=\"left\" style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Start-Sleep -Seconds $(Get-Random -Minimum 60 -Maximum 180)<\/p>\n<p align=\"left\" style=\"margin-left:30px\">}&nbsp;<\/p>\n<p align=\"left\">Now Matthew will get one of three random holiday messages every 60 to 180 seconds until 100 messages have been sent. He&rsquo;s definitely going to be in the holiday spirit after this!<\/p>\n<p align=\"left\">~Thomas<\/p>\n<p align=\"left\">Thanks, Thomas! Matthew is a lucky guy!<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: MVP, Thomas Rayner, inspires holiday spirit by using Windows PowerShell to trigger some multimedia. Hello! I&rsquo;m Thomas Rayner, a proud Cloud &amp; Datacenter Management Microsoft MVP, filling in for the Scripting Guy for a couple days. You can find me on Twitter (@MrThomasRayner), or posting biweekly on my blog, WorkingSysadmin: Figuring stuff out at [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,652,45],"class_list":["post-76071","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-thomas-rayner","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: MVP, Thomas Rayner, inspires holiday spirit by using Windows PowerShell to trigger some multimedia. Hello! I&rsquo;m Thomas Rayner, a proud Cloud &amp; Datacenter Management Microsoft MVP, filling in for the Scripting Guy for a couple days. You can find me on Twitter (@MrThomasRayner), or posting biweekly on my blog, WorkingSysadmin: Figuring stuff out at [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/76071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=76071"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/76071\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=76071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=76071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=76071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}