Posts Custom PowerShell API for Azure Naming Policy
Post
Cancel

Custom PowerShell API for Azure Naming Policy

Azure PowerShell API for Azure Naming Policy - AzureIs.Fun

To continue our PowerShell API series, we have another example of a highly useful API that you can integrate into your environment.

Choosing names for Azure resources can be a challenging task. With so many options and requirements, it can be difficult to know where to start. Fortunately, there are resources available to simplify this process.

One such resource is the Cloud Adoption Framework page with abbreviations for Azure Resources Abbreviation examples for Azure resources.

Of course, manually checking that page will not save us any time. Therefore, we will create a script to extract the information and save it inside an object that we can use.

Use PowerShell to collect Resource Name Abbreviations data from HTML page

Here is that script:

# Specify the URL of the website and the ID of the table we want to scrape
$url = "https://learn.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/resource-abbreviations"
# Use Invoke-WebRequest with -UseBasicParsing to download the webpage and extract the HTML code
$html = Invoke-WebRequest -Uri $url -UseBasicParsing | Select-Object -ExpandProperty Content
#If everything else fails user regex!
#Use regular expressions to extract the table data from the HTML code
$tableRegex = '<table>.*?<tbody>(.*?)</tbody>.*?</table>'
$rowRegex = '<tr>.*?<td>(.*?)</td>.*?<td>(.*?)</td>.*?<td>(.*?)</td>.*?</tr>'
$tableMatches = [regex]::Matches($html, $tableRegex, [System.Text.RegularExpressions.RegexOptions]::Singleline)
# Initialize an empty array to hold the table data
$tableData = @()
# Iterate over each match to extract the rows of data
foreach ($tableMatch in $tableMatches) {
$tableRows = [regex]::Matches($tableMatch.Groups[1].Value, $rowRegex, [System.Text.RegularExpressions.RegexOptions]::Singleline)
# Iterate over each row to extract the columns of data
foreach ($tableRow in $tableRows) {
$rowData = @{
"Resource" = $tableRow.Groups[1].Value
"Namespace" = ($tableRow.Groups[2].Value).Replace("<code>","").Replace("</code>","")
"Abbreviation" = ($tableRow.Groups[3].Value).Replace("<code>","").Replace("</code>","")
}
# Add the row data to the table data array
$tableData += New-Object PSObject -Property $rowData
}
}
# Display the table data
$tableData

Lately, I have experienced compatibility issues with existing HTML parsing modules and cmdlets, so I have decided to use regex instead. Although this is not the cleanest method, you can check other options such as the PowerHTML module or use ParsedHTML property of Invoke-WebRequest on Windows PowerShell v5.

Now we can use this data to display the abbreviations for our resources.

Get Name Abbreviation for Azure Resource Type

Here is the first function that we can use to quickly return the abbreviation for a resource type or namespace:

function Get-ResourceTypeAbbreviation {
param(
[Parameter(Mandatory=$true)]
[string]$ResourceType
)
if ($tableData.Resource -contains $ResourceType) {
$Answer = $tableData | Where-Object Resource -like $ResourceType | Select-Object -ExpandProperty Abbreviation
Write-Host "Correct abbreviation for $ResourceType is: $Answer"
} elseif ($tableData.Namespace -contains $ResourceType) {
$Answer = $tableData | Where-Object Namespace -like $ResourceType | Select-Object -ExpandProperty Abbreviation
Write-Host "Correct abbreviation for $ResourceType is: $Answer"
} else {
throw "Unknown resource type: $ResourceType"
}
}
#Examples:
#Get-ResourceTypeAbbreviation -ResourceType "Virtual Machine"
#Get-ResourceTypeAbbreviation -ResourceType "Microsoft.Compute/virtualMachines"

Get Name Abbreviation for existing Azure Resource

Here is another function that we can use to check the resource type and abbreviation for existing resources:

function Get-ExistingResourceTypeAndAbbreviation {
param(
[Parameter(Mandatory=$true)]
[string]$ResourceName
)
# Check if the resource exists
$resource = Get-AzResource -Name $ResourceName -ErrorAction SilentlyContinue
if (!$resource) {
Write-Error "Resource '$ResourceName' not found."
return
}
# Get the resource provider namespace
#$resourceId = $resource.ResourceId
$resourceType = $resource.ResourceType
# Get the abbreviation for the resource type
$abbreviation = $tableData | Where-Object Namespace -like $ResourceType | Select-Object -ExpandProperty Abbreviation
# Return the resource type and abbreviation as a PSCustomObject
return [PSCustomObject]@{
ResourceType = $resourceType
Abbreviation = $abbreviation
}
}
#Example:
#Get-ExistingResourceTypeAndAbbreviation -ResourceName "VM-AzureIsFun"

Get Naming Convention for Resource Type

Here is another version of this function that will give you the entire resource name based on a predefined naming policy:

#To simplify this example, we can define hashtable with naming standard:
$ResourceNamesHash = @{}
$ResourceNamesHash.Add("Microsoft.Compute/virtualMachines", "VM-`$Subscription-`$Description")
$ResourceNamesHash.Add("Microsoft.KeyVault/vault", "kv-`$Environment-`$Description")
$ResourceNamesHash.Add("Microsoft.Network/vpnGateways", "vpng-`$VNetName-`$Description")
# Define a function to get the abbreviation for a given resource type
function Get-ResourceNamingConvention {
param(
[Parameter(Mandatory=$true)]
[string]$ResourceType
)
if ($ResourceNamesHash.ContainsKey($ResourceType)) {
Write-Host "Naming Policy for $ResourceType is:" $ResourceNamesHash[$ResourceType]
} else {
throw "Unknown resource type: $ResourceType"
}
}
#Example:
#Get-ResourceNamingConvention -ResourceType "Microsoft.Network/vpnGateways"

You can easily extend this script to accept parameters such as subscription name, environment, location, abbreviations, etc., and to use an existing file as input so that you don’t need to keep your policy defined within the script.

Create your own PowerShell API

You can use these as individual scripts or combine them into one Azure Function API that is always available, saving you time when creating resources.

To speed up the process, you can also schedule the first script to run separately once a day and save the output in a file that the other functions can easily read.

Check out my previous article to learn how to Create Your Own PowerShell APIs for Azure Governance with Azure Function App and how that can be useful for your daily Azure Administrator tasks.

Another way of having Azure Resource Naming Policy quickly available to you is to use Azure Dashboards for Azure Governance.


I hope this article was helpful. Keep clouding around!

Vukasin Terzic

This post is licensed under CC BY 4.0