Skip to content

Instantly share code, notes, and snippets.

@JimMoyle
Created March 10, 2021 14:16
Show Gist options
  • Save JimMoyle/badde6b000d70ff2aa795ace32ccc02e to your computer and use it in GitHub Desktop.
Save JimMoyle/badde6b000d70ff2aa795ace32ccc02e to your computer and use it in GitHub Desktop.
Get Azure VM price from API with PowerShell
[CmdletBinding()]
Param (
[Parameter(
ValuefromPipelineByPropertyName = $true,
ValuefromPipeline = $true,
Mandatory = $true
)]
[System.String]$vmSKU,
[Parameter(
ValuefromPipelineByPropertyName = $true
)]
[System.String]$region = 'uksouth'
)
BEGIN {
Set-StrictMode -Version Latest
} # Begin
PROCESS {
$serviceName = 'Virtual Machines'
$priceType = 'Consumption'
$filter = "serviceName eq '{0}' and armSkuName eq '{1}' and armRegionName eq '{2}' and priceType eq '{3}'" -f $serviceName, $vmSKU, $region.ToLower(), $priceType
$price = Invoke-RestMethod "https://prices.azure.com/api/retail/prices?`$filter=$filter"
$normalSkus = $price.Items | Where-Object {$_.meterName -notlike "*Low Priority" -and $_.meterName -notlike "*Spot"}
if ($normalSkus.count -ne 2) {
Write-Error 'did not filter down to 2 skus'
break
}
$priceMinMax = $normalSkus.retailPrice | Measure-Object -Maximum -Minimum
$output = [PSCustomObject]@{
BillingCurrency = $price.BillingCurrency
WindowsRetailPrice = $priceMinMax.Maximum
WVDRetailPrice = $priceMinMax.Minimum
}
Write-Output $output
} #Process
END {} #End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment