I’ve recently taken on a project to review a client’s Azure environment and address all the risks associated with the lack of redundancy in its current design and the first task was to inventory their virtual machines so we can map them accordingly to the function it provides. One of my colleagues who have been working with this client forewarned me that there would be few name mismatches between the name given to the virtual machine in Azure and the name of the computer in the operating system. What may be named as SQL potentially could have been repurposed to a completely different function.
To clarify, the name I’m referring to is the configuration on the top left corner of the Azure virtual machine as compared to the value under the Computer name field, which is what, say, a computer is named within the Windows operating system:
There isn’t an easy way to retrieve this information so I did a quick search on the internet to see if there was a script available and was not able to find one so I wrote one using the Get-AzVM cmdlet to quickly retrieve, compare and output in the PowerShell console. More refinement can be made to the script but this quick and dirty job provided me with what I need so please feel to expand it as necessary.
Begin by copying the following and saving it as a .ps1 file:
foreach ( $azVM in Get-AzVM ) {
$networkProfile = $azVm.NetworkProfile.NetworkInterfaces.id.Split(“/”)|Select -Last 1
$vmName = $azVm.OsProfile.ComputerName
$rgName = $azVm.ResourceGroupName
#$tags = $azVM.Tags
$hostname = (Get-AzVM -ResourceGroupName $rgName -Name $azVm.OsProfile.ComputerName -status).computername
$IPConfig = (Get-AzNetworkInterface -Name $networkProfile).IpConfigurations.PrivateIpAddress
[pscustomobject]@{
Name = $azVm.OsProfile.ComputerName
ComputerName = $hostname
“IP Addresses” = $IPConfig
#Tags = $tags
“Az Name Match Host Name” = $azVm.OsProfile.ComputerName.equals($hostname)
}
}
Here is a screenshot of what it looks like in the Windows PowerShell ISE editor:
With the script saved in a directory, proceed to connect to Azure:
Connect-AzAccount
The environment in this example redirects the login to a Citrix ADC that performs AAA authentication with DUO MFA:
Your subscription name, tenant ID will be displayed upon successful authentication:
Navigate to the directory with the script and execute it with .<scriptname>.ps1:
As mentioned previously, more refinement such as handling blank Computer Names differently or exporting it to a CSV instead. Also note that I originally included outputting the Tags but have commented it out in the script.