简体   繁体   中英

retrieve the Hostname from an Azure VM

For monitor reasons I need to retrieve the hostname of each virtual machine.

I used the nice resource "Resource graph" in Azure Dashboard with following query.

project VMnaam = name,
location,
resourceGroup,
subscriptionId,
vmsize = properties.hardwareProfile.vmSize,
VMtype = properties.storageProfile.imageReference.offer,
diskSizeInGB = properties.storageProfile.osDisk.diskSizeGB,
OStype = properties.storageProfile.osDisk.osType,
adminUsername = properties.osProfile.adminUsername,
hostname = properties.osProfile.computerName,
type
 | where type == "microsoft.compute/virtualmachines"

which gives me a nice overview of every virtual machine. But something got my attention in the results. the hostname (properties.osProfile.computerName) is the same name as the VMname. Inside the VM I have checked this and controlled that this is indeed correct so I wanted to check what happens if I change the hostname in my VM.

In my linux VM I changed the hostname rebooted the machine and the new hostname is available in the terminal.

Through Azure Resourc Graph however The change is not visible; If I manually retrieve the computername with powershell with following powershell code:

Login-AzAccount -Subscription "mysub"
$vm = get-azvm -ResourceGroupName "myrg"  -Name "myvm"

$vm.OSProfile.ComputerName

I also see the first value of my hostname that was initially setup by Azure but not the new hostname that I changed.

Is it not possible to retrieve a changed hostname from Azure? Does it take a while? Is there any other way to retrieve the hostname from Azure?

no, it is not possible, azure is not aware of the actual hostname in the vm.

Also the fact that vmname and hostname match is just a coincidence (or rather a default from Azure). If you provision a vm through API you have the ability to set hostname when provisioning and it doesn't have to match vm name (although, it usually makes sense if it does).

edit: according to woter324 recent update seem to have fixed this and now Azure is aware of the actual vm hostname (confirmed for windows)

(az vm list | ConvertFrom-Json).osProfile.computerName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM