question

ROHITHJOGIPARTHI-4030 avatar image
0 Votes"
ROHITHJOGIPARTHI-4030 asked MayankBargali-MSFT commented

Azure Function Authentication error using powershell script accessing azure services

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.CreateArmClient[TClient](IAzureContext context, String endpoint)at 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_3`1.<ExecuteSynchronouslyOrAsJob>b_3_0(T c)at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action`1 executor)at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet)at 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-functionsazure-app-configuration
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ryanchill avatar image
0 Votes"
ryanchill answered MayankBargali-MSFT commented

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@ROHITHJOGIPARTHI-4030 As per the error message no subscription was found in the current session of your PowerShell function.

"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"

Please make sure that you have enabled the Identity on your function app and assign the roles on your subscription or as your requirement for the identity that is created on your function app.

In your code.ps1 file you need to invoke Set-AzContext to select the target subscription for which you are performing the operation.

 Set-AzContext -SubscriptionId "yoursubscriptionID"

Make sure that you have updated your requirements.psd1 for any PowerShell packages you want to install on your function app. By default when you create the project the Az module will be commented in your requirements.psd1 file.

Feel free to get back to me if you need any assistance.

0 Votes 0 ·