James O'Neill's Blog

May 11, 2008

More on the Hyper-V API

Filed under: How to,Powershell,Virtualization,Windows Server,Windows Server 2008 — jamesone111 @ 2:02 pm

In which we see how to set the number of CPUs

I started with getting MSVM Computer System objects – which I showed back in February. With these objects I can ask for the state of the VM to be changed to Running, Stopped or Saved.

To do things in a proper Powershell Style I re-wrote and re-wrote my functions so I have GET-Vm (which returns one or more VM(s) by name), Choose-VM, which puts up a list and returns one or more VM(s). Plus Start-VM, Stop-VM and Suspend-VM. Over various iterations these have moved from demanding a single MSVM Computer System object, to accepting an or object or display name, then to accepting and of array either, to allowing input to be piped in. Since Stop-VM is a bit brutal, that same February Post showed using the ShutDown integration Component

Next I moved onto the Msvm_ImageManagementService, and a few weeks back I looked at how Virtual Hard disks can be created , mounted and Compacted.

From there it was on to the related idea of Snapshots which I covered here and here; Snapshots are handled through the Msvm_virtualSystemManagementService. This is actually a very important WMI class. I mentioned Taylor’s post which shows how to manipulate the Exchange of Key/Value pairs (the Host’s KVPs are managed through this object). But there are 6 other methods I want to introduce here they are Create-, Modify- & Destroy- VirtualSystem and Add, Modify and Remove Virtual System Resources.

Creating and Modifying work in the same way. Identify the machine (unless it is being created) and pass a block of XML which describes how you want the machine, or the resource attached to it to be. If you’re think ahead and saying "Can I dump that XML out to a file ?" you can: both the Virtual System Management Service WMI object and the MMC console provide interfaces to Export or Import the Machine.  There are quite a lot of things which we want to be able to manipulate.

  • Legacy Network Card
  • VM-Bus Network Card
  • VM-Bus SCSI Controller
  • IDE DVD Drive
  • Virtual DVD Disk (which is inserted into the drive)
  • IDE Hard drive
  • SCSI hard drive
  • Virtual Hard disk-image (inserted into the drive)
  • Memory size
  • CPU cores and reservation
  • The VM itself

And for each of these we can get the XML by

  • Building it up from Scratch
  • Reading it from a file
  • Getting the existing value from WMI (for modification)
  • Getting a default from WMI (for creating)

The first 2 are usually a pain, so typically the process goes:

  1. Get a ResourceAllocationSettingData (RASD) object
  2. Modify one or more of its properties.
  3. Covert it to XML formatted Text,
  4. Pass the XMl as one of an array of arguments to one of the Methods of the Msvm_virtualSystemManagementService.

For example: here’s how we set the number of CPUs – we get a variation on the generic RASD object, the Msvm_ProcessorSettingData object for the VM in question.

    Filter Set-VMCPUCount
{Param ($VM , $CPUCount)
$procsSettingData=Get-WmiObject -NameSpace  "root\virtualization" `
-query "select * from MsVM_ProcessorSettingData 
                                where instanceID like 'Microsoft:$($vm.name)%' "
$SettingXML=$procsSettingData.GetText([System.Management.TextFormat]::WmiDtd20) $arguments=@($VM.__Path, @($SettingXML) , $null) $Result=$VSMgtSvc.PSbase.InvokeMethod("ModifyVirtualSystemResources", $arguments) if ($Result  -eq 0) {"Success"} else {"Failure, return code: $Result "} }

[Update. There were a couple of bits of PowerShell 2.0 in the above. In 1.0 you can’t call the .InvokeMethod  method of a WMI object directly, you have to call it via .psbase and .PATH property doesn’t exist, you have to get to the path with __Path, not .path.path]

The process is almost identical for memory, except get the Msvm_MemorySettingData object and set 3 properties named, .Limit, .Reservation   and VirtualQuantity which are all set to the desired memory size in megabytes

In the next few posts I’ll look at using the RASD objects to add disks and Network cards, plus how we can create and configure the VM itself.

This post originally appeared on my technet blog.


Blog at WordPress.com.

%d bloggers like this: