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 to share
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 to share