0

I have created cognitive services account . I want to get cognitive services account key and store it in a variable using powershell script.

I have used below script :

$resourceGroup = "Demo"

$AccountName = "DemoCs"

$Key = Get-AzCognitiveServicesAccountKey -ResourceGroupName $resourceGroup -Name $AccountName

Write-Host "account key 1 = " $Key

after executing the script result is :

2020-05-20T08:30:31Z [Information] INFORMATION: account key 1 = Microsoft.Azure.Commands.Management.CognitiveServices.Models.PSCognitiveServicesAccountKeys

Above script is able to list keys in the cloud shell but not in powershell function app .

vaibhav
  • 103
  • 2
  • 11

1 Answers1

0

You cannot simply call Import-Module Az.CognitiveServices like on your local machine where you have previously installed Az.CognitiveServices from that site. But you have to copy all files from that locally installed package into specific folder inside of your Function App in Azure.

1.Install Az.CognitiveServices in local and go to its folder to get all the content in it.

2.Go to your function KUDU. Click CMD>site>wwwroot>yourFunctionName then create a directory called modules.

3.Simply drag-and-drop all files from your local powershell module location to your Azure Function App folder create above(modules).

4.Include Az.CognitiveServices PowerShell module in run.ps1 file like that example below:

Import-Module "D:\home\site\wwwroot\HttpTrigger1\modules\Az.CognitiveServices.psd1"

5.Then run the script as below with $Key.Key1 to specify the Key1.

$Key = Get-AzCognitiveServicesAccountKey -ResourceGroupName $resourceGroup -Name $AccountName

Write-Host "account key 1 = " $Key.Key1

For more details, you could refer to this tutorial and this one.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30