简体   繁体   中英

How to get cognitive services account key using powershell function app?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM