Rename the guest OS to match the virtual machine name on Hyper-V
When you are managing a large number of virtual machines, it can get tricky to keep track of everything. One thing that I see many people doing to help with this is to make sure that the guest operating system has the same network name as the virtual machine name. This makes life a lot easier when moving between different tools.
However, I tend to rename and copy virtual machines a lot, which makes it difficult to keep the virtual machine and guest operating system name synchronized. To handle this I wrote the following script:
# Get the virtual machine name from the parent partition
$vmName = (Get-ItemProperty –path “HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters”).VirtualMachineName
# Replace any non-alphanumeric characters with an underscore
$vmName = [Regex]::Replace($vmName,"\W","_")
# Trim names that are longer than 15 characters
$vmName = $vmName.Substring(0,[System.Math]::Min(15, $vmName.Length))
# Check the trimmed and cleaned VM name against the guest OS name
# If it is different, change the guest OS name and reboot
if ($env:computername -ne $vmName) {(gwmi win32_computersystem).Rename($vmName); shutdown -r -t 0}
What this script does is:
- Read the virtual machine name from the registry inside the virtual machine
- Clean up the virtual machine name and check it against the guest OS name
- If they match, move along
- If they do not match, rename the guest operating system and reboot
I have this script inside my virtual machines, configured to run automatically when the guest operating system boots.
Cheers,
Ben
Comments
Anonymous
May 13, 2013
Ben, this seems backward to me. I could see this breaking applications like SharePoint. Wouldn't it be better to have the script rename the Hyper-V VM name according to the OS name?Anonymous
May 13, 2013
Totally agree with Wes , if this mechanism - script - is used for Exchange server VMs it will be a disaster !!Anonymous
May 13, 2013
This has never been a problem for me, but you make an interesting point. I will have to see if I can think of a way to make this script work in reverse :-) Cheers, BenAnonymous
May 13, 2013
Damian Flynn has written an excellent script that retrieves guest OS details: www.damianflynn.com/.../query-virtual-machines-details-using-powershellAnonymous
May 13, 2013
Totally agree with the previous comments. You would never do this in a production environment as renaming the guest OS can have very unpredictable and/or disastrous effect with many apps like exchange/SQL etc. As a virtualization admin, you would rather track when/if the app owner did a rename in the guest and match the hypervisor name description to match.Anonymous
May 13, 2013
Hi Ben, Great post. I do something similar, I like my VM host names to match the VM name in Hyper-V but I do this from my host. I've been meaning to publish the script that does this for a while and I have here: simon-may.com/powershell-to-rename-a-vm-from-the-host essentially I use PowerShell to query the VM on the hypervisor, ask the VM for it's IP address, run an invoke command to rename the VM. We could do it in reverse real easy too.Anonymous
May 15, 2013
I can see this being useful for cloned throwaway machines so that you avoid having duplicate names on the network. Something that runs on startup and then flags that it's done a rename on this machine once would at least be safe, and if it later finds the names mismatch then you could raise a warning in the event log. Thanks for the script :) I do agree with the others and the first thing that came to mind was SQL, although it's not as cranky with server renames these days compared to back in the SQL 2000 days.Anonymous
June 23, 2013
The comment has been removedAnonymous
October 08, 2013
How to get the details of Linux(Redhat) guest VM, like hostname and ip addressAnonymous
July 21, 2014
This is exactly what I was looking for for my private cloud setup with windows azure pack. I am using SetupComplete.cmd to run the script which works as expected but doing so the VM deployment gets stuck at customizing virtual machine stage. Though the actual VM finishes deploying including the name change task. Could you help with how you have set this script to run.Anonymous
May 01, 2017
Asking questions are genuinely good thing if you are not understanding anything completely, except this paragraph gives good understanding even.Anonymous
May 23, 2017
I know this post is older. On the VMWare side there is a way to change the name of the guest os to match the vm name. Get the vm name, the name has a property for guest os name. We do this prior to adding the machine to the domain. Does Hyper-V have anything like this?$VCName = $VM.Name $WinName = $VM.Guest.Hostname If ($WinName -ne $VCName) { $progressbar1.PerformStep() Write-Host "$VCName is currently $WinName... renaming" $renamecomputer = "wmic path win32_computersystem where ""Name='%computername%'"" CALL rename name='$VCName'" Invoke-VMScript -VM $VM -GuestUser "Administrator" -GuestPassword "somegreatpwhere" -ScriptType Bat -ScriptText $renamecomputer restart-vmguest -VM $VM -Confirm:$false $progressbar1.PerformStep() }