| Profiel van Dung K HoangDung's spaceWeblogNetwerk | Help |
|
|
24 april Hyper-V WMI Examples - Part V
Managing virtual machines (con' td)
Let's see how you can do it with scripts 1. Create a new virtual machineUpdates for RC1 Withe the Hyper- RC1 release, it is very simple to create a new virtual machine.. You simply call the method DefineVirtualSystem. Here is an example: ----------------------Create new empty VM ------------------------------------------- $VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService ----------------------End-of-script --Create new empty VM -------------------------------------------
2. Change DisplayNameOnce the new virtual machine is created, its display name is set to a default value of "New Virtual Machine". If you want to change the display name to a different value, you can do the following: ----------------------Change Display Name of VM -------------------------------------------$VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService ----------------------End-of-script --Change Display Name of VM ------------------------------------------- 2. Configuring virtual memoryNow you want to add more virtual memory to the new VM by increasing its RAM from 512MB to 1GB. You need to look at the class Msvm_MemorySettingData whose one of the properties contains the actual value of virtual memory allocated to a VM. ----------------------Change Virtual Memory of VM -------------------------------------------$VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService ## First you need to locate the active settings of a virtual machine ## Then find all the resources allocated to this VM. One particular resource is the Virtual Memory resource ## Find an instance of Msvm_MemorySettingData that matches with $VMem.PartComponent ## Now modify the quantity of RAM ## Commit Changes ------------------End-of-script----Change Virtual Memory of VM -------------------------------------------
3. Rename a snapshotThe script is similar to the one that changes the display name of the VM. Indeed, we need to identify an instance of the Msvm_VirtualSsytemSettingData class that represents a snapshot (SettingType =5 is the trick!) Here is an example: ----------------------Rename a snapshot ------------------------------------------- $VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService ## Find snapshot associated to this machine - Assume you have only one ## Commit changes ----------------------End-of-script -------- Rename a snapshot -------------------------------------------
Enjoy! /Dung 21 april Hyper-V WMI Examples - Part IV
Managing virtual machines
Also at the end of the day, I'd like to take snapshots of all running VMs So I decide to use Hyper-V WMI API to write a script for changing states of VMs. In the following examples, our host systems are named HVHOSTXX where xx starts from 02 to 15. -------------------Script to pause all VMs ------------------------------------------------ ## $State = @{‘Enabled’ = 2 ; ‘Disabled’= 3; ‘Paused’= 32768 ; ‘Suspended’ = 32769 ; ‘Starting’ = 32770 ; ‘Snapshotting’ = 32771 ; ` 2..15 | % {
} ---------------------------------- End of Script --------------------------------------------------- -------------------Script to take snapshots of all VMs ------------------------------------------------ 2..15 | % {
} ## 600 VMs being snapshotted in one go! ---------------------------------- End of Script ---------------------------------------------------
Quite easy, hey? /Dung Hyper-V WMI Examples - Part III
Exporting and Importing virtual machines
I was so swamped in preparing an internal training on WS08 for our folks and was not writing any post since a while. All this deployment can't be done without scripts to automate the operation. I'm pleasantly surprised with the Hyper-V WMI API that allow us to do most of the deployment work and I will share those scripting experiences with you.
Exporting virtual machineUnlike VS 2005, when you want to move a virtual machine from one host to another, you cannot simply copy the configuration file (.VMC) and the VHD file and "make it "appear on the destination host with Hyper-V. To perform the same operation, you need to export the VM from the source host system, copy all the export files to the destination host and then import it into Hyper-V. When exporting a VM, you can either export only configuration files (that contains all settings for this VM) or include all save state files (.vsv files), snapshot files (.AVHD files) and VM binary (.VHD file). After you specify a root folder to store export files, the export operation will create a folder structure for you under the root folder as follow: ( assuming that the root folder is called C:\Export) C:\Export |---------- <VM_Name>
The Virtual Machines sub-folder contains an export file (.EXP) that Hyper-V creates for export and eventually VSV files and AVHD files if you specify the option to export save state files for export. The EXP file will be read by the import operation to re-create settings for virtual machine at destination. The Virtual Hard Disks sub-folder is the location where VHD file will reside. To script the export operation with Hyper-V WMI, you simply call the method of the Msvm_VirtualSystemManagementService Class and specify a reference to the VM to be exported, a flag that indicates whether you want to export system state files or not and an export folder. The call returns either 0 or 4096. All other values indicate an error in the parameters passed to the call. Here is an example: ## Get reference to a VM to be exported ## Connect to the Virtual machine Management Service ## call the Export method Importing virtual machinesThe next step is to copy the folder structure under C:\Export to the destination host. It is not recommended to import a VM directly from the export folder and the import operation does not support network drive or network share folder so you can't simply point the import operation to a network share like \\Server\C$\Export. Here is an example of scripting the import operation: ## Connect to the Virtual machine Management Service
Bulk Export and ImportIf you have a set of virtual machines to be exported from one machine and then to be imported on a different host, you can automate the operation as follow: On the Source Server $VM_Service = get-wmiobject -computer $SourceServer –namespace root\virtualization –class Msvm_VirtualSystemManagementService foreach ($VM in $ListofVMs)
} robocopy /z /s C:\Export \\$DestServer\C$\VMs On the Destination Server $VM_Service = get-wmiobject -computer $DestServer –namespace root\virtualization –class Msvm_VirtualSystemManagementService $ListOfFolders = dir "C:\VMs" | where { $_.PSIsContainer} | select FullName } Until next time.... /Dung
|
|
|