Azure Powershell Check for Outdated Credentials

I am trying to have my powershell script check if user accounts credentials have expired, and if so, invoke a command character Add-AzureAccount

. This way the user can re-enter the credentials and not see the error.

Is there a way to check expiration using the Azure Powershell SDK?

+3


source to share


4 answers


I've used try / catch successfully, for example to call Get-AzureWebsite:

$websiteName = "..."

Try
{
    $website = Get-AzureWebsite -Name $websiteName -ErrorAction Stop;
}
Catch [System.ArgumentException]
{
    if ($_.Exception.Message -eq "Your Azure credentials have not been set up or have expired, please run Add-AzureAccount to set up your Azure credentials.")
    {
        Add-AzureAccount;

        Write-Host "Azure account set, re-run script to continue."
    }
    else
    {
        Write-Host $_.Exception.Message;
    }
}

      



I'm sure there is a less brittle way than checking the exception message, so consider this as the beginning of the approach.

+1


source


I am using the following script to load credentials from a file, thereby skipping the UI prompt. If the file does not exist or the privilege has expired, the user will be prompted for credentials next time.



if (Test-Path ("{0}/azure-credentials.json" -f (Get-Location)))
{
    $Null = Select-AzureRmProfile -Path ("{0}/azure-credentials.json" -f (Get-Location))
    Try
    {
        Get-AzureRMManagedSubscription
    } Catch {
        Login-AzureRmAccount
        Save-AzureRmProfile -Path ("{0}/azure-credentials.json" -f (Get-Location))
    }
} else {
    Login-AzureRmAccount
    Save-AzureRmProfile -Path ("{0}/azure-credentials.json" -f (Get-Location))
}

      

+1


source


The current latest version of the Azure PowerShell Module does not have any command to check if an Azure user account has expired or not.

The closest you can do is to use Get-AzureAccount -Name to check if you have added the account to your Azure PowerShell profile or not.

0


source


You can do following to check account expiration in powershell session

$Account = Get-AzureAccount -Name "email@account.com"
if (!$Account)
{
 Add-AzureAccount
}

      

Hope this helps!

0


source







All Articles