How to retrieve storage account key using powershell function app?

后端 未结 1 1998
暖寄归人
暖寄归人 2021-01-24 02:15

I\'m using powershell function app to retrieve storage account key but i\'m not able to access resources .Please help me .

$resourceGroup = \"DemoResourceGroup\"         


        
相关标签:
1条回答
  • 2021-01-24 02:50

    According to the script you provide, you use Az module. So if you want to choose which Azure subscription you use, you need to use the command Select-AzSubscription. Besides, you also can add -Subscription "<subscription Id>" in Connect-AzAccoun to ensure when you login, you choose the right subscription.

    For example

    1. Create the service principal
    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
    
    1. assign the role to the service principal. For example, assign Contributor role to the sp at the subscription level
    New-AzRoleAssignment -ApplicationId <service principal application ID> -RoleDefinitionName "Contributor" `
    -Scope "/subscriptions/<subscription id>"
    
    1. Script
    $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
    
    0 讨论(0)
提交回复
热议问题