hi @HKPR
Thank you for reaching out to Microsoft Q&A. Inunderstand you are looking for some guidance on Azure Functions.
In Azure Functions the usual way to know whether your code is authenticated to Azure is to check the current Az context. If your function app has a managed identity enabled, call Connect-AzAccount -Identity first, then use Get-AzContext to confirm that a context was created.
Connect-AzAccount by itself is the interactive sign-in flow, so it is not the normal pattern inside a Function app. For Functions, Microsoft recommends managed identity.
Evidence
Disable-AzContextAutosave -Scope Process
try {
Connect-AzAccount -Identity -ErrorAction Stop | Out-Null
$ctx = Get-AzContext
if ($null -ne $ctx -and $null -ne $ctx.Account -and $null -ne $ctx.Subscription) {
Write-Output "Authenticated as $($ctx.Account) in subscription $($ctx.Subscription.Name)"
}
else {
Write-Output "Not authenticated"
}
}
catch {
Write-Output "Not authenticated: $($_.Exception.Message)"
}
If you are using a user-assigned managed identity, pass the client ID as well:
Connect-AzAccount -Identity -AccountId <client-id>
Some things to consider:
- If the function app does not have a managed identity enabled,
Connect-AzAccount -Identity will fail.
- If you need to use a specific subscription, you may still need
Set-AzContext after connecting.
Please 'Upvote' (Thumbs-up) and 'Accept' as answer if the response was helpful. This helps others in the community find relevant help as well.
Best regards,
Andrew Taylor