Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect-AzAccount error when running Azure Functions coded in PowerShell

I am getting this error when trying to run my Azure Function (Which is in PowerShell). PowerShell script works normally when ran locally.

Error:

[Error] ERROR: No subscription found in the context.  Please ensure that the credentials you provided are authorized to access an Azure subscription, then run Connect-AzAccount to login

In requirements.ps1:

@{
    'Az.Accounts' = '1.9.0'
    'Az.Resources' = '1.1.2'
    'AzTable' = '2.0.3'
    'Az.Storage' = '1.7.0'
    'Az.KeyVault' = '1.5.1'
}

In the run.ps1, I added the Import-Module commands:

Import-Module Az.Resources
Import-Module Az.Accounts
Import-Module Az.KeyVault
Import-Module AzTable

First few lines in 'run.ps1':

$activeKey = Get-AzKeyVaultManagedStorageAccount -VaultName $keyVaultName -Name $storageAccountName

# Get Storage Account Key 
$key = (Get-AzStorageAccountKey -ResourceGroupName $rgName -Name $storageAccountName)

In profile.ps1:

if ($env:MSI_SECRET) {
    Disable-AzContextAutosave
    Connect-AzAccount -Identity
}

Thanks!

like image 717
thebernardlim Avatar asked Sep 20 '25 02:09

thebernardlim


1 Answers

Please make sure you:

  1. Enable a managed identity. Using a system-assigned identity is easier, using a user-assigned identity requires one more step.
  2. Assign appropriate roles on the target subscription to the identity.
  3. In your run.ps1 code, invoke Set-AzContext to select the target subscription.

Importing modules explicitly in run.ps1 is usually not necessary.

like image 118
Anatoli Beliaev Avatar answered Sep 22 '25 05:09

Anatoli Beliaev