azure - 如何使用 powershell 函数应用程序检索存储帐户 key ?

标签 azure powershell azure-function-app

我正在使用 powershell 函数应用来检索存储帐户 key ,但我无法访问资源。请帮助我。

$resourceGroup = "DemoResourceGroup"

$AccountName = "Demo"

$Key = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $AccountName)

Write-Host "storage account key 1 = " $Key

我收到以下错误:

2020-05-14T14:00:05Z [错误] 错误:Get-AzStorageAccountKey:“this.Client.SubscriptionId”不能为 null。 在 D:\home\site\wwwroot\TimerTrigger1\run.ps1:25 字符:8 + $key = Get-AzStorageAccountKey -ResourceGroupName "DocumentParser_FBI ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzStorageAccountKey],ValidationException + FullQualifiedErrorId:Microsoft.Azure.Commands.Management.Storage.GetAzureStorageAccountKeyCommand

脚本堆栈跟踪: 在,D:\home\site\wwwroot\TimerTrigger1\run.ps1:第 25 行

Microsoft.Rest.ValidationException:“this.Client.SubscriptionId”不能为空。 在Microsoft.Azure.Management.Storage.StorageAccountsOperations.ListKeysWithHttpMessagesAsync(字符串resourceGroupName,字符串accountName,Nullable1展开,Dictionary2customHeaders,CancellationTokencancelToken) 在 Microsoft.Azure.Management.Storage.StorageAccountsOperationsExtensions.ListKeysAsync(IStorageAccountsOperations 操作、字符串 resourceGroupName、字符串 accountName、Nullable1 展开、CancellationToken CancellationToken) 在 Microsoft.Azure.Management.Storage.StorageAccountsOperationsExtensions.ListKeys(IStorageAccountsOperations 操作、字符串 resourceGroupName、字符串 accountName、Nullable1 展开) 在 Microsoft.Azure.Commands.Management.Storage.GetAzureStorageAccountKeyCommand.ExecuteCmdlet()

最佳答案

根据您提供的脚本,您使用Az模块。因此,如果您想选择使用哪个Azure订阅,则需要使用命令Select-AzSubscription 。另外,您还可以添加-Subscription "<subscription Id>"Connect-AzAccoun以确保您在登录时选择正确的订阅。

例如

  1. 创建服务主体
Import-Module Az.Resources # Imports the PSADPasswordCredential object
$credentials = New-Object Microsoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential -Property @{ StartDate=Get-Date; EndDate=Get-Date -Year 2024; Password=<Choose a strong password>}
$sp = New-AzAdServicePrincipal -DisplayName ServicePrincipalName -PasswordCredential $credentials
  • 将角色分配给服务主体。例如,在订阅级别将贡献者角色分配给 sp
  • New-AzRoleAssignment -ApplicationId <service principal application ID> -RoleDefinitionName "Contributor" `
    -Scope "/subscriptions/<subscription id>"
    
  • 脚本
  • $appId = "your sp app id"
    $password = "your sp password"
    $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
    $mycreds = New-Object System.Management.Automation.PSCredential ($appId, $secpasswd)
    
    Connect-AzAccount -ServicePrincipal -Credential $mycreds -Tenant <you sp tenant id>
    Get-AzSubscription -SubscriptionName "CSP Azure" | Select-AzSubscription
    
    $resourceGroup = "nora4test"
    
    $AccountName = "qsstorageacc"
    
    $Key = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $AccountName)[0].Value
    
    Write-Host "storage account key 1 = " $Key
    

    关于azure - 如何使用 powershell 函数应用程序检索存储帐户 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61799697/

    相关文章:

    c# - 为什么在读取 Azure Functions 中的环境变量时建议使用 GetEnvironmentVariable 而不是 AppSettings

    c# - 如何将 asp.net Identity 连接到 SQL Azure

    azure - 无法更新 Azure 应用程序网关 - 错误 : The disabled Rule '920300' is unknown

    c# - Powershell 到 C# 语法转换

    json - 如何使用 PowerShell 在没有索引的情况下访问 JSON 文件中的数组对象

    azure - 新部署的 azure 函数返回 404 Not Found 错误

    azure - 我如何从java spring-boot AZURE函数中的资源文件夹中读取任何文件?

    azure - Databricks 运行时 ML 和 ML 流程之间的区别

    powershell - 有没有办法在 PowerShell 中使文本居中

    azure - 使用 MSI 从 ADF 调用 Azure Function