Skip to content

Instantly share code, notes, and snippets.

@nicolonsky
Created March 19, 2020 20:55
Show Gist options
  • Save nicolonsky/798cfc9c0b5543647347cf09de6ef3c8 to your computer and use it in GitHub Desktop.
Save nicolonsky/798cfc9c0b5543647347cf09de6ef3c8 to your computer and use it in GitHub Desktop.
# Check Azure AD Access token
if ($null -eq [Microsoft.Open.Azure.AD.CommonLibrary.AzureSession]::AccessTokens){
Connect-AzureAD
} else {
$token = [Microsoft.Open.Azure.AD.CommonLibrary.AzureSession]::AccessTokens
Write-Verbose "Connected to tenant: $($token.AccessToken.TenantId) with user: $($token.AccessToken.UserId)"
}
@bartvermeersch
Copy link

It didn't always worked for me, I had to add a check for an empty token:
$token = [Microsoft.Open.Azure.AD.CommonLibrary.AzureSession]::AccessTokens
if (($null -eq $token) -or ($token.count -eq 0)){
Connect-AzureAD
} else {
Write-Verbose "Connected to AzureAD tenant: $($token.AccessToken.TenantId) with user: $($token.AccessToken.UserId)"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment