Azure Function Authentication error using powershell script accessing azure services

ROHITH JOGIPARTHI 1 Reputation point
2021-10-28T11:07:12.867+00:00

I am trying to update My Azure App Service Settings through PowerShell Script and execute the Powershell Script From ADF using an Azure Function Activity. But My Azure Function is failing with below Error. Please Find below my code to update the AAP settings using powershell script.
-------------------------------------------Code.ps1---------------------------------------------------
$webapp=Get-AzWebApp -ResourceGroupName 'MyResourcegroup' -Name 'MyAzureApp'

$appSettings=$webapp.SiteConfig.AppSettings
$newAppSettings = @{}
ForEach ($item in $appSettings) { $newAppSettings[$item.Name] = $item.Value }

$newAppSettings['DatabaseIURL'] = "@Microsoft.keyVault(SecretUri=https://azkeyvault.vault.azure.net/secrets/DatabaseIURL-datasource2Url/b3ea7ec3e03d415b8525da9c99d3fdc1"

Set-AzWebApp -ResourceGroupName 'cms-catalyst-dev-bcnl-rg' -Name 'cms-cloud-dev-bcnl' -AppSettings $newAppSettings
-----------------------------------------ERROR------------------------------------------------------------------------------------
2021-10-28T10:59:50.603 [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.Exception :Type : Microsoft.Azure.Commands.Common.Exceptions.AzPSApplicationExceptionErrorKind : UserErrorLineNumber : 60ErrorFileName : ClientFactoryTargetSite :Name : CreateArmClientDeclaringType : Microsoft.Azure.Commands.Common.Authentication.Factories.ClientFactoryMemberType : MethodModule : Microsoft.Azure.PowerShell.Authentication.dllStackTrace :at Microsoft.Azure.Commands.Common.Authentication.Factories.ClientFactory.CreateArmClientTClientat Microsoft.Azure.Commands.WebApps.Utilities.WebsitesClient..ctor(IAzureContext context)at Microsoft.Azure.Commands.WebApps.Models.WebAppBaseClientCmdLet.get_WebsitesClient()at Microsoft.Azure.Commands.WebApps.Cmdlets.WebApps.GetAzureWebAppCmdlet.ExecuteCmdlet()at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.<>c__31.<ExecuteSynchronouslyOrAsJob>b__3_0(T c)at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action1 executor)at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJobTat Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()Message : 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.Data : System.Collections.ListDictionaryInternalSource : Microsoft.Azure.PowerShell.AuthenticationHResult : -2146232832CategoryInfo : CloseError: (:) [Get-AzWebApp], AzPSApplicationExceptionFullyQualifiedErrorId : Microsoft.Azure.Commands.WebApps.Cmdlets.WebApps.GetAzureWebAppCmdletInvocationInfo :MyCommand : Get-AzWebAppScriptLineNumber : 26OffsetInLine : 9HistoryId : 1ScriptName : C:\home\site\wwwroot\Az_DB_Switch\run.ps1Line : $webapp=Get-AzWebApp -ResourceGroupName "xyz" -Name "xxyyzz"PositionMessage : At C:\home\site\wwwroot\Az_DB_Switch\run.ps1:26 char:9+ $webapp=Get-AzWebApp -ResourceGroupName "xyz" - …+

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,321 questions
Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
209 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 26,146 Reputation points Microsoft Employee
    2021-10-29T03:07:28.427+00:00

    Seems the context running your PowerShell doesn't have the correct login context to access the resources.

    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

    Have tried using an automation account? This blog post walks through how to set that up. Also note that since you're accessing a key vault resource, the identity of the automation account will need to have the appropriate access policies.