Renaming a Hyper-V Virtual Machine

Renaming a Hyper-V Virtual Machine

  • Comments 5

Moving on we come to a sample script for changing a setting on a virtual machine - in this case I am changing the virtual machines name.

VBScript:

Option Explicit
 
Dim WMIService
Dim VM
Dim VMManagementService
Dim VMSystemSettingData
Dim VMName
Dim NewVMName
Dim Result
 
'Setup variables for the VM we are looking for, and the new name
VMName = "Windows Server 2003"
NewVMName = "Windows Server 2003 - new"
 
'Get an instance of the WMI Service in the virtualization namespace.
Set WMIService = GetObject("winmgmts:\\.\root\virtualization")
 
'Get a VMManagementService object
Set VMManagementService = WMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)
 
'Get the VM object that we want to modify
Set VM = (WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" & VMName & "'")).ItemIndex(0)
 
'Get the VirtualSystemSettingsData of the VM we want to modify
Set VMSystemSettingData = (VM.Associators_("MSVM_SettingsDefineState", "MSVM_VirtualSystemSettingData")).ItemIndex(0) 
 
'Change the ElementName property
VMSystemSettingData.ElementName = NewVMName
 
'Update the VM with ModifyVirtualSystem
Result = VMManagementService.ModifyVirtualSystem(VM.Path_.Path, VMSystemSettingData.GetText_(1))

PowerShell:

#Setup variables for the VM we are looking for, and the new name
$VMName = "Windows Server 2003"
$NewVMName = "Windows Server 2003 - new"
 
#Get a VMManagementService object
$VMManagementService = gwmi -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization" -computername "."
 
#Get the VM object that we want to modify
$query = "SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" + $VMName + "'"
$VM = gwmi -query $query -namespace "root\virtualization" -computername "."
 
#Get the VirtualSystemSettingsData of the VM we want to modify
$query = "Associators of {$VM} WHERE AssocClass=MSVM_SettingsDefineState"
$VMSystemSettingData = gwmi -query $query -namespace "root\virtualization" -computername "."
 
#Change the ElementName property
$VMSystemSettingData.ElementName = $NewVMName
 
#Update the VM with ModifyVirtualSystem
$Result = $VMManagementService.ModifyVirtualSystem($VM.__PATH,$VMSystemSettingData.psbase.GetText(1))

To change a setting on a virtual machine you do not just get the virtual machine object and change the property there.  What you need to do is to:

  1. Get the MSVM_VirtualSystemSettingData associated with the virtual machine (there is only one associated with each virtual machine).
  2. Modify the setting you want to change on the MSVM_VirtualSystemSettingData object.
  3. Get an instance MSVM_VirtualSystemManagementService.
  4. Call the ModifyVirtualSystem method on the MSVM_VirtualSystemManagementService and pass it the path of the virtual machine object and text serialization of the modified MSVM_VirtualSystemSettingData object.

As you can see this is a bit counter-intuitive but once you get your head around it, it is fairly easy to do.

Cheers,
Ben

Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
  • This is not untuïtive at all. With Virtual Server this is so much easier.

    How about a C# sample? You guys manage Hyper-V from C#, VB or C++ I hope...

  • Joe -

    Not intuitive, yes.  But also compliant with the DMTF management standard for virtual machines.

    Cheers,

    Ben

  • That is a good thing from a standards perspective.

    This will take quite some time to learn.

    Anyway, can you provide a VB sample as well so I can start playing with it from managed code?

  • Is there any way to change the host name of the guest OS?

  • hi pc guy

    i tried this code but it doesnt work how can i change the hostname of the virtual machine using powershell?

    thanks

Page 1 of 1 (5 items)