{"id":82805,"date":"2018-02-28T00:01:12","date_gmt":"2018-02-28T08:01:12","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=82805"},"modified":"2019-02-18T09:10:00","modified_gmt":"2019-02-18T16:10:00","slug":"windows-powershell-and-the-azure-text-to-speech-rest-api-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/windows-powershell-and-the-azure-text-to-speech-rest-api-part-1\/","title":{"rendered":"Windows PowerShell and the Azure Text-to-Speech Rest API (Part 1)"},"content":{"rendered":"<p><strong>Summary<\/strong>: You can use Windows PowerShell to authenticate to the Microsoft Cognitive Services Text-to-Speech component through the Rest API.<\/p>\n<p>Q: Hey, Scripting Guy!<\/p>\n<p>I heard about the cool Microsoft Cognitive Services, and had heard they have a REST API. Does that mean I can use PowerShell to consume them? Could you show me how to authenticate to it?<\/p>\n<p>\u2014SH<\/p>\n<p>A: Hello SH,<\/p>\n<p>I just love waking up and saying &#8220;YES YOU CAN!&#8221; when people ask &#8220;Can you do that with Windows PowerShell?&#8221;<\/p>\n<p>So\u2026 ahem\u2026. &#8220;Yes you can!&#8221;<\/p>\n<p>For those who didn&#8217;t know, Cognitive Services are hosted in the Azure cloud, and they allow you to many things easily. With very little work in PowerShell or any other programming language, we can moderate content visually.<\/p>\n<p>We can easily use it to search the internet for content or, like we&#8217;ll do over the next series of articles, make use of the Text-to-Speech component.<\/p>\n<p>First, <a target=\"_blank\" href=\"https:\/\/azure.microsoft.com\/try\/cognitive-services\/\" rel=\"noopener\">sign up for the trial of Cognitive Services<\/a>, which is part of the Azure Subscription.<\/p>\n<p>If you don&#8217;t already have an Azure account, select <strong>FREE ACCOUNT<\/strong> to get yourself started. We&#8217;ll wait for you to finish.<\/p>\n<p>Once that process is done, you&#8217;ll see some selections. Select <strong>Login<\/strong> to authenticate and add the trial. Then choose <strong>Speech<\/strong> to get a trial started with the Speech API.<\/p>\n<p>Next, the most important button to select is <strong>Get API Key<\/strong>, beside the <strong>Bing Speech API<\/strong>. This will generate the application keys you&#8217;ll need to talk to the API.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/cog-services-01-02202018.png\"><img decoding=\"async\" width=\"1024\" height=\"683\" class=\"alignnone size-large wp-image-82815\" alt=\"Screenshot of Get API Key\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/cog-services-01-02202018-1024x683.png\" \/><\/a><\/p>\n<p>IMPORTANT: If you already have an Azure subscription, and just want to get going under production (that is, not the trial), the steps are different. Please <a target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cognitive-services\/cognitive-services-apis-create-account\" rel=\"noopener\">follow these instructions<\/a> if you do not want to use the trial version, or you want to move to production from your trial.<\/p>\n<p>&#8220;But\u2026but Scripting Guy!&#8221; I can hear you say. &#8220;This is a PowerShell article\u2026. Where&#8217;s the PowerShell?&#8221;<\/p>\n<p>Well I was wondering when you were going to ask. I like to show both halves of the solution whenever I can.<\/p>\n<p>First, you&#8217;ll need the updated AzureRM cmdlets from the PowerShell gallery to make sure you have the <strong>AzureRM.CognitiveServices<\/strong> module. If you haven&#8217;t done this before, just run the following cmdlet to get the complete set of cmdlets for managing Azure:<\/p>\n<p style=\"padding-left: 60px\"><code>Install-Module AzureRM<\/code><\/p>\n<p>If you are running an older version and need to update, just run this:<\/p>\n<p style=\"padding-left: 60px\"><code>Update-Module AzureRM\n<\/code><\/p>\n<p>You can confirm if the new module is available by running this:<\/p>\n<p style=\"padding-left: 60px\"><code>Get-Module -ListAvailable AzureRM.CognitiveServices<\/code><\/p>\n<p>On my system, I have a total of seven new cmdlets available to me for provisioning and managing these resources.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/cog-services-02-02202018.png\"><img decoding=\"async\" width=\"1024\" height=\"178\" class=\"alignnone size-large wp-image-82825\" alt=\"Screenshot of new cmdlets\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/cog-services-02-02202018-1024x178.png\" \/><\/a><\/p>\n<p>We would first authenticate by using the <strong>Login-AzureRMAccount<\/strong> cmdlet. Once connected, we can examine the properties of that newly created resource.<\/p>\n<p>To list all Cognitive Service accounts we&#8217;ve created, we can run the following cmdlet. In this example, we have only the one created.<\/p>\n<p style=\"padding-left: 60px\"><code>Get-AzureRMCognitiveServicesAccount<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/cog-services-03-02202018.png\"><img decoding=\"async\" width=\"1024\" height=\"171\" class=\"alignnone size-large wp-image-82835\" alt=\"Screenshot of account created\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/cog-services-03-02202018-1024x171.png\" \/><\/a><\/p>\n<p>But if we&#8217;d like to re-create this, we can store and grab the properties by targeting the name of the account and the resource group it&#8217;s stored within. We will place this within the object called <strong>$Account<\/strong>.<\/p>\n<p style=\"padding-left: 60px\"><code>$Account=Get-AzureRmCognitiveServicesAccount -Name 'HSG-CognitiveService' -ResourceGroupName 'HSG-ResourceGroup'<\/code><\/p>\n<p>We&#8217;ll need some data from this object we captured, to rebuild or duplicate this process in the future.<\/p>\n<p>To begin with, let&#8217;s obtain the first two: <strong>Location<\/strong> and the <strong>AccountType<\/strong>. The location should not to be mistaken with Azure datacenter locations.<\/p>\n<p>We can obtain this from the object we created earlier, called <strong>$Account<\/strong>. It is stored within the property called <strong>Location<\/strong>, as seen in the preceding screenshot. Its value is <strong>global<\/strong>.<\/p>\n<p style=\"padding-left: 60px\"><code>$Location=$Account.Location<\/code><\/p>\n<p>An additional value we&#8217;ll need is the type of account we created. In our case, it was a connection to <strong>Bing.Speech<\/strong>. You can see this attached to the <strong>AccountType<\/strong> property, which we will obtain from the <strong>$Account<\/strong> object we created earlier.<\/p>\n<p style=\"padding-left: 60px\"><code>$AccountType=$Account.AccountType<\/code><\/p>\n<p>Another property we&#8217;ll need to obtain is the list of objects Azure uses to identify the pricing tiers. To find this, we can pipe the <strong>$Account<\/strong> object into the <strong>Get-AzureRMCognitiveServicesAccountSkus<\/strong> cmdlet. We will need to grab the <strong>values<\/strong> property specifically from this object, and expand it.<\/p>\n<p style=\"padding-left: 60px\"><code>$Skus=$Account | Get-AzureRmCognitiveServicesAccountSkus | Select-Object -expandproperty Value\n<\/code><\/p>\n<p>If you examine the newly created object, you&#8217;ll see results that don\u2019t seem all that useful:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/cog-services-04-02202018.png\"><img decoding=\"async\" width=\"1024\" height=\"114\" class=\"alignnone size-large wp-image-82845\" alt=\"Screenshot of object results\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/cog-services-04-02202018-1024x114.png\" \/><\/a><\/p>\n<p>However, we can do a little PowerShell magic, and expand the <strong>Sku<\/strong> property further by doing this:<\/p>\n<p style=\"padding-left: 60px\"><code>$Sku.Sku<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/cog-services-05-02202018.png\"><img decoding=\"async\" width=\"313\" height=\"196\" class=\"alignnone size-full wp-image-82855\" alt=\"Screenshot of expanded property\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/cog-services-05-02202018.png\" \/><\/a><\/p>\n<p>In our case, we only need worry about the SKU used from the object we most recently created. Our goal is simply to duplicate the process. We can access the SKU directly again from the <strong>$Account<\/strong> object.<\/p>\n<p style=\"padding-left: 60px\"><code>$Account.Sku<\/code><\/p>\n<p>This will give us output similar to what we saw before (when we grabbed all the SKUs). In our case, to rebuild the resource, we only need the property called <strong>Name<\/strong>.<\/p>\n<p style=\"padding-left: 60px\"><code>$Account.Sku.Name<\/code><\/p>\n<p>To capture any of these properties, and avoid re-typing, we can just pipe to <strong>Set-Clipboard<\/strong>:<\/p>\n<p style=\"padding-left: 60px\"><code>$Account.Sku.Name | Set-Clipboard<\/code><\/p>\n<p>To re-create this resource from a new Azure subscription, we would just run the following script (after logging in of course):<\/p>\n<p style=\"padding-left: 60px\"><code># AzureRM Resource Group\n<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$ResourceGroup='HSG-ResourceGroup'\n<\/code><\/p>\n<p style=\"padding-left: 60px\"><code># Azure Cognitive Services Account SKU<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$Sku='F0'<\/code><\/p>\n<p style=\"padding-left: 60px\"><code># Azure Cognitive Services Account Type\n<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$AccountType='Bing.Speech'<\/code><\/p>\n<p style=\"padding-left: 60px\"><code># Unique Name to our Azure Cognitive Services Account\n<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$AccountName='HSG-Speech Account'<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>New-AzureRmCognitiveServicesAccount -ResourceGroupName 'HSG-ResourceGroup' -Name 'HSG-AzureRMSpeech' -Type Bing.Speech -SkuName F0 -Location 'global' -force<\/code><\/p>\n<p>Be sure to visit again as we start to look into how to leverage this amazing resource by using the REST API!<\/p>\n<p>I invite you to follow the Scripting Guys on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\" rel=\"noopener\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\" rel=\"noopener\">Official Scripting Guys Forum<\/a>.<\/p>\n<p><strong>Sean Kearney, Premier Field Engineer<\/strong><\/p>\n<p><strong>Enterprise Services Delivery, Secure Infrastructure<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: You can use Windows PowerShell to authenticate to the Microsoft Cognitive Services Text-to-Speech component through the Rest API. Q: Hey, Scripting Guy! I heard about the cool Microsoft Cognitive Services, and had heard they have a REST API. Does that mean I can use PowerShell to consume them? Could you show me how to [&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":[568],"tags":[3,154,45],"class_list":["post-82805","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","tag-scripting-guy","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: You can use Windows PowerShell to authenticate to the Microsoft Cognitive Services Text-to-Speech component through the Rest API. Q: Hey, Scripting Guy! I heard about the cool Microsoft Cognitive Services, and had heard they have a REST API. Does that mean I can use PowerShell to consume them? Could you show me how to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/82805","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=82805"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/82805\/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=82805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=82805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=82805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}