search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Rename virtual machine name on Hyper-V to match the guest OS hostname
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 4,493 views | 25/06/2013 08:40

If you need to sync OS name with VM name on Hyper-V, you can use KVP exchange feature:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$HyperVHost = "MyHyperV"
filter Import-CimXml
{
   $CimXml = [Xml]$_
   $CimObj = New-Object -TypeName System.Object
   foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY"))
   {
       $CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE
   }
   $CimObj
}
 
$VMs = Get-VM -ComputerName $HyperVHost
foreach ($VM in $VMs)
{
   $VMInfo = Get-WmiObject -ComputerName $HyperVHost -Namespace root\virtualization -Query "Select * From Msvm_ComputerSystem Where ElementName='$VM'"
   $KVPData = Get-WmiObject -ComputerName $HyperVHost -Namespace root\virtualization -Query "Associators of {$VMInfo} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent"
   $KVPExport = $KVPData.GuestIntrinsicExchangeItems
   $Hostname = ($KVPExport | where {$_.Name -eq "FullyQualifiedDomainName"}).Data
   $VM | Rename-VM -NewName $Hostname
}

Btw, I haven’t tested this script yet. But that should work :)



Leave a Reply