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
Related
I want to create 70 VM's of the same OS in Azure. For this, I have created a VM converted to a custom image of it so that I can deploy 70 instances of this machine.
The issue here for me is if I deploy 70 instances of VM via custom images I am getting the hostname of all the VM's as same. All 70 VM's hostname is WIN2K12.
Is there any way where I can change this hostname while deploying?
I'm dealing with the same question right now and my solution is run a remote PowerShell command through Azure CLI (Linux shell in the example below) in the VMs that need to have their hostnames changed. Basically is something like this:
$RG = "Your Resource Group Name"
$OLD_NAME = "The replicated hostname name"
$NEW_NAME = "The new hostname"
$VM_NAME = "The VM's name that you just created/copied"
az vm run-command invoke \
--resource-group $RG \
--name $VM_NAME \
--command-id RunPowerShellScript \
--scripts "Rename-Computer -ComputerName $OLD_NAME -NewName $NEW_NAME"
I guess you can adapt it to get parameters from command line etc.
Don't forget to restart the VMs after this.
Point of clarification.
If you create VM from a Image they will not/should not have the same Hostname. Part of process of making an Azure Custom Image is to sysprep machine which guarantees the hostname will be different when you deploy new VM using that image.
https://learn.microsoft.com/en-us/azure/virtual-machines/windows/capture-image-resource
In Azure the only time VM will get the same host name is if you create VM from a specialized disk, meaning create VM from a copy of the same disk (not really image).
The thing you have to confirm is if your applications on the VM allows sysprep.
Since you chose "NO" on SysPrep the image is not generalized. You're basically creating 70 copies of the same harddrive and starting them. Sysprep ensures that the server upon boot accepts either manual or scripted input to change hostname among other things.
Can't we Move a Microsoft Azure VM to a Different Subnet Within a vNet using the azure new portal or the azure classic portal ? if not possible through portal then how to do so ?then how to edit the properties of a VM after creation, like moving to a different subnet,, etc.,?
It is possible through the new portal. First I want to ask you if you're using a Classic VM or a Resource manager VM. If you're using the last one you can easily switch between subnets by changing the configuration settings.
go to your network interface > Ip configurations and click on the Nic name (see picture below)
A new tab will open and you can change the Subnet of the nic.
If your vm is a classic one, moving it to different vNet is very easy using azure powershell cmdlets. Here is the code-
$vmName = "xxxxx"
$srcServiceName = "xxxxx"
$newVNet = "xxxxx"
# export vm config file
$workingDir = (Get-Location).Path
$sourceVm = Get-AzureVM –ServiceName $srcServiceName –Name $vmName
$global:vmConfigurationPath = $workingDir + "\exportedVM.xml"
$sourceVm | Export-AzureVM -Path $vmConfigurationPath
# remove vm keeping the vhds and spin new vm using old configuration file but in a new vNet
Remove-azurevm –ServiceName $srcServiceName –Name $vmName
$vmConfig = Import-AzureVM -Path $vmConfigurationPath
New-AzureVM -ServiceName $srcServiceName -VMs $vmConfig -VNetName $newVNet -WaitForBoot
if not possible through portal then how to do so ?then how to edit the properties of a VM after creation, like moving to a different subnet,, etc.,?
It could be done with Powershell. In brief, it contains 3 steps:
Get the VM (NIC) configuration
Edit the VM (NIC) configuration
Update the edited configuration
Note: Moving VMs between different VNET is not supported. To move the VM to another VNET, the only solution for now is re-create the VM with the same vhd file.
Here is a good step-by-step guide:
How to change Subnet and Virtual Network for Azure Virtual Machines (ASM & ARM)
I created a VM in Windows Azure and some networking people are asking me for the deployment id. I cannot see this property anywhere on the portal. How can I get the deployment id of a Windows Azure VM? I just created the VM through the portal.
One way is to:
Go to https://resources.azure.com and log in
Search for the name of your VM and click to open details. It should return JSON information about the VM.
In the JSON data, search for deploymentId (it should be under the hardwareProfile section in the JSON)
You can see the deployment ID in the virtual machine's Dashboard tab. Refer to the screenshot-
Here's how you can do it via Powershell:
First log in to azure:
login-AzureRmAccount
Then get a reference to the virtual machine. In my case, I have a virtual machine called malcolms-dad in the resource group breaking-bad:
$vm = (Get-AzureRmResource -ResourceGroupName breaking-bad -ResourceName malcolms-dad -ResourceType MicrosoftClassicComputer/virtualMachines)
Now you have the reference, you can query for the deployment id property:
$vm.Properties.HardwareProfile.DeploymentId
Note that we had to pass in the -ResourceType parameter into the Get-AzureRmResource query. This might seem superfluous, but if you omit the parameter the command returns an object without the Properties field.
Need to shutdown specific VMs in my Azure subscription. Is there a way, I can make the ‘Get-AzureVM’ PowerShell cmdlet to read from an input file? We have too many VMs in our subscription. So specifying individual machine by name is tedious in our case.
Please advice.
I am assuming in the answer below that you are using ASM Azure portal, i.e. the older portal as you are using the ASM cmdlets.
If you want to start all VMs in your subscription then you can use:
Get-AzureVM | Start-AzureVM
If you want to start only specific VMs then you can use below script:
#Declaring the Dictionary object to store VM Names along with their Service names
$VMList = #{}
#Adding VMs to the VM List
#You can replace this section with reading from file and then populating your dictionary object
#Replace the VM Name and Service name with your environment data
$VMList.Add("VMName01", "ServiceName")
$VMList.Add("VMName02","ServiceName")
#Getting the VM object using Get-AzureVM and then starting the VMs
$VMList.Keys | % {Get-AzureVM -Name $_ -ServiceName $VMList.Item($_)} | Start-AzureVM
I recently setup Active Directory on a VM inside an Azure Virtual Network. Unfortunately, the Virtual Network DNS entry was not put in before creating and working with the Active Directory VM.
Because of Azure's framework, I believe I have to recreate the VM (after making the DNS change). Does anyone know how to take a snapshot of the virtual hard-drive and re-associate that with a newly created VM after the DNS change is made. I have read information about sysprep to capture an image but I'm afraid that will corrupt various AD settings.
Also, does anyone have any other information about this type of DNS change?
Thanks,
akgh
Here are the steps:
First export the VM configuration to an XML file
get-azurevm -servicename [service name] | export-azurevm -path [local path]
This generates a small XML file that contains information about the VM.
Next, remove the VM configuration from Azure.
remove-azurevm [VM name]
This does not actually delete the VHD file, it only removes the configuration about the VHD that you have previously backed-up/exported from step 1. So there is no danger or destruction done here. These are actually the same steps you would do if you wanted to stop paying for the service.
Now, restore the VM configuration with the new DNS settings you want.
$dnslocal=New-AzureDNS -Name "my Azure DNS Server" -IPaddress "192.168.10.4"
$dnsonprem=New-AzureDNS -Name "my OnPrem DNS Server" -IPaddress "172.16.83.21"
Import-AzureVM -Path 'E:\myVM.xml' | New-AzureVM -ServiceName 'myVM' -Location 'West US' -DnsSettings $dnslocal,$dnsonprem -VNetName "MyVNET" –AffinityGroup “CATAFF1”
Note: You may get an error when trying to import the VM configuration. If you get an error that the DNS name already exists, then you may need to remove the cloud service
remove-azureservice –servicename “myVM"