Created
April 13, 2018 07:58
-
-
Save nordineb/136040e81c5621b76ace81cdee0e3633 to your computer and use it in GitHub Desktop.
Show-Azurehealth.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function IsLoggedIn | |
{ | |
$content = Get-AzureRmContext -ErrorAction SilentlyContinue | |
if ($content) | |
{ | |
return !([string]::IsNullOrEmpty($content.Account)) | |
} | |
return $false | |
} | |
function Show-Azurehealth { | |
if (!(IsLoggedIn)) | |
{ | |
Login-AzureRmAccount | |
} | |
if ([string]::IsNullOrEmpty($subscriptionId)) | |
{ | |
$subscriptionId = (Get-AzureRmSubscription | Select -First 1 ).SubscriptionId | |
} | |
$adTenant = (Get-AzureRmSubscription -SubscriptionId $subscriptionId).TenantId | |
$clientId = "1950a258-227b-4e31-a9cf-717495945fc2" # Well-known client ID for Azure PowerShell | |
$redirectUri = "urn:ietf:wg:oauth:2.0:oob" # Redirect URI for Azure PowerShell | |
$resourceAppIdURI = "https://management.core.windows.net/" # Resource URI for REST API | |
$authority = "https://login.windows.net/$adTenant" # Azure AD Tenant Authority | |
# Load ADAL Assemblies | |
$adal = "Microsoft.IdentityModel.Clients.ActiveDirectory.3.19.2\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll" | |
Add-Type -Path $adal | |
# Create Authentication Context tied to Azure AD Tenant | |
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority | |
# Acquire token | |
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto") | |
# Create Authorization Header | |
$authHeader = $authResult.CreateAuthorizationHeader() | |
# Set REST API parameters | |
$apiVersion = "2015-01-01" | |
$contentType = "application/json;charset=utf-8" | |
# Set HTTP request headers to include Authorization header | |
$requestHeader = @{"Authorization" = $authHeader} | |
# Set initial URI for calling Resource Health REST API | |
$uri = "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.ResourceHealth/availabilityStatuses?api-version=$apiVersion" | |
# Call Resource Health REST API | |
$healthData = Invoke-RestMethod -Uri $Uri -Method Get -Headers $requestHeader -ContentType $contentType | |
# Display Health Data for Azure resources in selected subscription | |
$healthData.value | | |
Select-Object ` | |
@{n='subscriptionId';e={$_.id.Split("/")[2]}}, | |
location, | |
@{n='resourceGroup';e={$_.id.Split("/")[4]}}, | |
@{n='resource';e={$_.id.Split("/")[8]}}, | |
@{n='status';e={$_.properties.availabilityState}}, # ie., Available or Unavailable | |
@{n='summary';e={$_.properties.summary}}, | |
@{n='reason';e={$_.properties.reasonType}}, | |
@{n='occuredTime';e={$_.properties.occuredTime}}, | |
@{n='reportedTime';e={$_.properties.reportedTime}} | | |
Format-List | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment