check if i'm logged to azure or not using powershell script in Azure Functions

HKPR 41 Reputation points
2022-07-23T10:40:02.367+00:00

Could anyone help on how to write the function script in PowerShell to verify If my azure session is logged in?

Using Connect-AzAccount will not log automatically I believe.

Azure Functions
Azure Functions

An Azure service that provides an event-driven serverless compute platform.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Vinod Nallavade 331 Reputation points
    2022-07-23T15:32:47.917+00:00

    Hi @HKPR , Thanks for reaching out Microsoft Q&A.
    Here is the function snippet that will help you to check if have a valid session or not.

    function Login()  
    {  
        $context = Get-AzContext  
      
        if (!$context)   
        {  
            Connect-AzAccount  
        }   
        else   
        {  
            Write-Host " Already connected"  
        }  
    }  
    

    Was this answer helpful?

    8 people found this answer helpful.

  2. Micah Warren 0 Reputation points
    2026-06-26T18:26:31.84+00:00

    You can access to details of the logged in user/service via Get-AzAccessToken which has when the token expires.

    If you just want a T/F on if the session is active you can use the below. I would ensure using the -ErrorAction SilentlyContinue as if not logged into a session it will write an error message.

    $SignedIn = (Get-AzAccessToken -ErrorAction SilentlyContinue).ExpiresOn -ge (Get-Date)
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  3. Andrew Taylor - COREZENN 1,305 Reputation points Volunteer Moderator
    2026-06-27T00:44:53.5266667+00:00

    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

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.