PowerShell How-To

Managing Azure Cognitive Service Accounts with PowerShell

Here's how to start taking advantage of Microsoft's suite of artificial intelligence APIs.

Azure Cognitive Services is a suite of APIs provided by Microsoft that allows mere mortals to tap the power of artificial intelligence (AI) in many different ways.

Broken out in six different areas (vision, speech, knowledge, search, language and anomaly detection), Azure Cognitive Services allows developers to build applications that walk and talk like a real human being is interacting with users, and to leverage AI across many different other areas.

Using Azure Cognitive Services requires an account in Azure. This account is used to authenticate to the various APIs, track resource usage and more. We could use the Azure portal to manage these accounts...but let's do it the fun way with PowerShell.

Prerequisites
First, you'll need the Az.CognitiveServices PowerShell module. This module comes with the Az PowerShell module available via the PowerShell Gallery by running Install-Module Az.

Once you've got the module installed and authenticated with Connect-AzAccount, you're ready to begin working with Azure Cognitive Services accounts.

Getting Started
To get started, let's first see if any Azure Cognitive Services accounts exist in our Azure subscription using the Get-AzCognitiveServices command. When you run this command, nothing will be returned because no accounts exist yet. Let's create one.

We can create a new Cognitive Services account using New-AzCognitiveServicesAccount. However, this command will need two pieces of information you probably aren't familiar with yet: account types and SKUs.

The account type is the type of API the Azure Cognitive Services account will work with. To discover the account type options, use the Get-AzCognitiveServicesAccountType command, as shown below. Notice that you have a lot of options to choose from. To demonstrate, I'd like to begin using the speech services API, so I'll use the SpeechServices type.

PS C:\> Get-AzCognitiveServicesAccountType
AnomalyDetector
Bing.Autosuggest.v7
Bing.CustomSearch
Bing.EntitySearch
Bing.Search.v7
Bing.SpellCheck.v7
CognitiveServices
ComputerVision
ContentModerator
CustomVision.Prediction
CustomVision.Training
Dummy
Face
Internal.AllInOne
LUIS
QnAMaker
SpeakerRecognition
SpeechServices
TextAnalytics
TextTranslation

Next, I'll need to figure out the SKUs to use. The SKU is the tier plan that defines the price, call limit and rate for the account. You've got a lot of options; to see them all, you'd use the Get-AzCognitiveServicesAccountSkus command. To find the SKUs only related to Speech Services, which we're using as an example, type Get-AzCognitiveServicesAccountSkus -Type SpeechServices -Location EastUS. I'm using EastUS because that's the region closest to me. You can exclude the Location parameter to see all regions.

PS> Get-AzCognitiveServicesAccountSkus -Type SpeechServices -Location EastUS

ResourceType : accounts
Name         : F0
Tier         : Free
Kind         : SpeechServices
Locations    : {EASTUS}
Restrictions : {}

ResourceType : accounts
Name         : S0
Tier         : Standard
Kind         : SpeechServices
Locations    : {EASTUS}
Restrictions : {}

Because this is just a demonstration, I'll use the free SKU or F0.

Now that I have the type and SKU, I can now create my Azure Cognitive Services account.

C:\> New-AzCognitiveServicesAccount -ResourceGroupName CognitiveServices -Name TestAccount -Type SpeechServices -SkuName F0 -Locat
ion EastUS

Notice
Microsoft will use data you send to Bing Search Services or the Translator Speech API to improve Microsoft products and services.
Where you send personal data to these Cognitive Services, you are responsible for obtaining sufficient consent from the data
subjects. The General Privacy and Security Terms in the Online Services Terms do not apply to these Cognitive Services. Please refer to the Microsoft Cognitive Services section in the Online Services Terms
(https://www.microsoft.com/en-us/Licensing/product-licensing/products.aspx) for details. Microsoft offers policy controls that may
be used to disable new Cognitive Services deployments
(https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-apis-create-account).
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): a


ResourceGroupName : CognitiveServices
AccountName       : TestAccount
Id                : /subscriptions/xxxxxxxxxxx/resourceGroups/CognitiveServices/providers/Microsoft.Cogniti
                    veServices/accounts/TestAccount
Endpoint          : https://eastus.api.cognitive.microsoft.com/sts/v1.0/issuetoken
Location          : EastUS
Sku               : Microsoft.Azure.Management.CognitiveServices.Models.Sku
AccountType       : SpeechServices
ResourceType      : Microsoft.CognitiveServices/accounts
Etag              : "050000e3-0000-0000-0000-5ca7c4010000"
ProvisioningState : Succeeded
Tags              :

Your account is now created! You can now manipulate it using other commands inside of the Az.CognitiveServices module such as Set-AzCognitiveServicesAccount, as well as Get-AzCognitiveServicesAccountUsage to see how much you're using the service -- just to name a few.

You can get a full list of all available commands by running Get-Command -Module Az.CognitiveServices.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular