A little PS script for getting VM and VMSS member status

Occasional Contributor

I wrote this simple PS script to see the status of our VMs and VMSS members, as I wished to monitor infrastructure with an eye to costs.

The VMSS bit took me a while, probably because I'm not very bright, but I thought the construction might help others who need to do tasks on each member of a scale set.

YMMV

 

# Login-AzureRMAccount
# Uncomment above to log in
# add a Object to collect output
$outObj = New-Object PSObject
# What is your TenantID
$tenantId = "your tenant ID in here"
# First get a list of subscriptions in this tenant
$subList = Get-AzureRmSubscription -TenantId $tenantId
# Then write it to the screen in yellow
Write-Host -ForegroundColor Yellow $subList.name
foreach ($subName in $subList.name)
    {
# Write each sub as we work on it below the yellow name in Green
    Write-Host -NoNewline -ForegroundColor Green $subName ""
# Below is an odd construction
# I find that if you use Select-AzureRmSubscription with a string subName
# it does not always work, mostly but not always
# the method below always has worked for me YMMV
    $subObj = Get-AzureRmSubscription -SubscriptionName $subName
    $subTmp = Select-AzureRmSubscription -SubscriptionId $subObj
# Get list of Resource Groups
    $rgList = Get-AzureRmResourceGroup
    foreach ($rgName in $rgList.ResourceGroupName)
# In each RG first get the VMSSs then the VMs
        {
# The below logic will be useful for any thing you need to do on each member of a scale set
        $vmssList = Get-AzureRmVmss -ResourceGroupName $rgName
        foreach ($vmssName in $vmssList.Name)
            {
            $vmssObj = Get-AzureRmVMssVM -InstanceView -Name $vmssName -ResourceGroupName $rgName
            foreach ($instCount in $vmssObj.InstanceID)
# if you want to use this logic for another task on each member of a scale set
# then replace what is between {} with your tasks
                {$vmssTmp = Get-AzureRmVmssVM -InstanceView -InstanceId $instCount -ResourceGroupName $rgName -VMScaleSetName $vmssName
                $outTmp = $vmssName + "_" + $instCount + "." + $rgName + "." + $subName
                $outObj | Add-Member $outTmp  $vmssTmp.Statuses[1].DisplayStatus
                } 
            }
        $vmList = Get-AzureRmVM -ResourceGroupName $rgName
        foreach ($vmName in $vmList.Name)
            {
            $vmObj = Get-AzureRmVM -Name $vmName -ResourceGroupName $rgName -Status
            $outTmp = $vmObj.Name + "." + $rgName + "." + $subName
            $outObj | Add-Member $outTmp  $vmObj.Statuses[1].DisplayStatus
            }
        }
    }
    Write-Host
# Write-Host for the New line
$outObj
# Output the list

0 Replies