Following up on my 003 post on usage aggregates…. Apologies for the article’s delayed timing.
Breadcrumbs
After revealing these aggregates, I thought to myself, what ‘usage’ is being aggregated. AND. Are there any PowerShell cmdlets with ‘Usage’ in them? Well. Let’s find out!!
Get-Command *Usage* | Where {$_.Source -Like 'Az.*'}
** Note ** That ‘.’ in the module name is VERY important. Otherwise you may expose Azure or AzureRM cmdlets which will (if not already) be deprecated!
Get-AzBatchPoolUsageMetrics Get-AzBatchPoolUsageMetric Get-AzCdnEndpointResourceUsage Get-AzCdnProfileResourceUsage Get-AzCdnSubscriptionResourceUsage Get-AzCognitiveServicesAccountUsage Get-AzConsumptionUsageDetail Get-AzMlCommitmentPlanUsageHistory Get-AzNetworkUsage Get-AzOperationalInsightsWorkspaceUsage Get-AzSignalRUsage Get-AzSqlInstancePoolUsage Get-AzStorageUsage Get-AzVirtualNetworkUsageList Get-AzVMUsage Get-UsageAggregates
What about this ‘Get-AzVmUsage? Well. Yeah, we can start here (it’s an AZ Compute module):
** Note ** For the above screenshot, the ‘?’ was completely unintentional. However. The question mark revealed some secret stuff…. We see that this cmdlet uses RegEx for the Location parameter!
match expected pattern ‘^[-\w\._]+$’
Now we have some information on the location, but let’s check this with Get-Help:
Now this makes sense. The location is literally an Azure datacenter location. How do we find those?
Get-AzLocation (a bit too logical here …)
This cmdlet reveals a series of locations:
Notice that a list of Providers is shown as well … Interesting details there as well (like a trail of breadcrumbs). Let’s see what locations that are in use for this test tenant:
For example, if we pick the ‘eastus’ option, we can pull the usage like so:
Get-AzVMUsage -Location EastUs
Which reveals this:
Notice that there are a lot of empty values. How can we pull just the used values?
$Values = @() $Values = Get-AzVMUsage -Location EastUs $TotalValues = $Values.Count $Counter =0 Do { $TestValue = $Values[$Counter].CurrentValue If ($TestValue -gt 0){ $Values[$Counter] } $Counter++ } While ($Counter -lt $TotalValues)
This script will review each item in the list and display only ones with non-zero values in ‘Current Value’:
Now what if we needed to report on each region so that we have a more comprehensive report on these values:
$Locations = (Get-AzVM).Location $AZLocations = $Locations | sort -Unique $NonZeroValues = $Null $NonZeroValues = @() Foreach ($AzLocation in $AzLocations) { Write-Host "$AzLocation region usage:" -ForegroundColor Green $Values = $Null $Values = @() $NonZeroValues = $Null $NonZeroValues = @() $Values = Get-AzVMUsage -Location $AzLocation $TotalValues = $Values.Count $Counter = 1 Do { # $Counter # $Values[$Counter] $TestValue = $Values[$Counter].CurrentValue # $TestValue If ($TestValue -gt 0){ # Write-Host $AzLocation,$Values[$Counter] # $NonZeroValues += $Values[$Counter] $OutputObj = New-Object -Type PSObject $OutputObj | Add-Member -MemberType NoteProperty -Name Name -Value $Values[$Counter].Name.Value $OutputObj | Add-Member -MemberType NoteProperty -Name CurrentValue -Value $Values[$Counter].CurrentValue $OutputObj | Add-Member -MemberType NoteProperty -Name Limit -Value $Values[$Counter].Limit $OutputObj | Add-Member -MemberType NoteProperty -Name Unit -Value $Values[$Counter].Unit # $OutputObj | FT -auto $NonZeroValues += $OutputObj } $Counter++ } While ($Counter -lt $TotalValues) $NonZeroValues | ft }
Honestly, the end code was a bit more complicated than I wanted, but could not get the regional labels to display in the correct order for the data that would follow. Here are the final results:
We can see that each region’s usage is different than the others.
More Cmdlets, Similar Code
What other usage reporting cmdlets are there?
Get-AzNetworkUsage Taking the previous script, we can tweak a couple lines to get this code: [sourcecode language="powershell"] $Locations = (Get-AzVM).Location $AZLocations = $Locations | sort -Unique $NonZeroValues = $Null $NonZeroValues = @() Foreach ($AzLocation in $AzLocations) { Write-Host "$AzLocation region usage:" -ForegroundColor Green $Values = $Null $Values = @() $NonZeroValues = $Null $NonZeroValues = @() $Values = Get-AzNetworkUsage -Location $AzLocation $TotalValues = $Values.Count $Counter = 1 Do { # $Counter # $Values[$Counter] $TestValue = $Values[$Counter].CurrentValue # $TestValue If ($TestValue -gt 0){ # Write-Host $AzLocation,$Values[$Counter] # $NonZeroValues += $Values[$Counter] $OutputObj = New-Object -Type PSObject $OutputObj | Add-Member -MemberType NoteProperty -Name Name -Value $Values[$Counter].Name.Value $OutputObj | Add-Member -MemberType NoteProperty -Name CurrentValue -Value $Values[$Counter].CurrentValue $OutputObj | Add-Member -MemberType NoteProperty -Name Limit -Value $Values[$Counter].Limit $OutputObj | Add-Member -MemberType NoteProperty -Name Unit -Value $Values[$Counter].Unit # $OutputObj | FT -auto $NonZeroValues += $OutputObj } $Counter++ } While ($Counter -lt $TotalValues) If ($Null -ne $NonZeroValues) { Write-Host "All Network Usage values are Zero" -ForegroundColor Yellow } Else { $NonZeroValues | ft } Write-Host '' }
Which, in a quiet tenant, would provide these results:
In a tenant with some virtual machines we would see this:
Quick view of the possible items being measured:
In this case many are at zero, but this will vary greatly on the environment being checked.
AZ Storage Locations
The usage PowerShell cmdlet for this resource in Azure provides very little data.
Get-AzStorageUsage -Location EastUs |fl
With a small amount of ode, we can list each regions:
$Locations = (Get-AzVM).Location
$AZLocations = $Locations | sort -Unique
Foreach ($AzLocation in $AzLocations){
Write-Host “Azure Location: ” -NoNewLine
Write-Host “$AzLocation” -ForegroundColor Green
Get-AzStorageUsage -Location $AZLocation | Ft
}
What about Get-AzVirtualNetworkUsageList? What can we do with this cmdlet? First, we can try to run it without switches:
Get-AzVirtualNetworkUsageList
Looks like the Resource Group is required, so we can grab that as well:
Get-AzResourceGroup | ft
Try to add this to the cmdlet and …
…another error. We need a virtual network name? Let’s grab that and see if we can also grab the ResourceGroupName as well:
Get-AzVirtualNetwork | ft name,resourcegroupname
Get-AzVirtualNetworkUsageList -ResourceGroupName ResourceGroup03 -Name VNet02
Now how do we programmatically examine all values?
$AZVirtualNetworks = Get-AzVirtualNetwork Write-Host 'Name,CurrentValue,Limit,Unit' Foreach ($AZVirtualNetwork in $AZVirtualNetworks) { $ResourceGroup = $AZVirtualNetwork.ResourceGroupName $VirtualNetwork = $AZVirtualNetwork.Name $AVNUL = Get-AzVirtualNetworkUsageList -ResourceGroupName $ResourceGroup -Name $VirtualNetwork $Name = $AVNUL.Name.Value $Value = $AVNUL.CurrentValue $Limit = $AVNUL.Limit $Unit = $AVNUL.Unit Write-Host "$Name,$Value,$Limit,$Unit" }
The rest of the ‘Usage’ cmdlets get into more specific aspects of Azure, which we will cover at a later date, otherwise this blog post will be WAY too long. Here is a list of cmdlets to cover at a later time and some ancillary information:
CDN
https://docs.microsoft.com/en-us/rest/api/cdn/endpoints
https://docs.microsoft.com/en-us/rest/api/cdn/endpoints/listresourceusage
Get-AzCdnEndpointResourceUsage
Get-AzCdnProfileResourceUsage
Get-AzCdnSubscriptionResourceUsage
https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-apis-create-account-cli?tabs=windows
Get-AzCognitiveServicesAccountUsage
Commitment Plans
https://docs.microsoft.com/en-us/rest/api/machinelearning/commitmentplans/createorupdate
Get-AzMlCommitmentPlanUsageHistory
Get-AzOperationalInsightsWorkspaceUsage
https://docs.microsoft.com/bs-latn-ba/powershell/module/az.operationalinsights/Get-AzOperationalInsightsWorkspaceUsage
Get-AzSignalRUsage
https://docs.microsoft.com/en-us/azure/azure-signalr/signalr-overview
Get-AzSqlInstancePoolUsage
https://docs.microsoft.com/bs-latn-ba/azure/sql-database/sql-database-instance-pools-how-to