Archive for the ‘Hyper-V’ Category

New-HyperVVm

September 7, 2008

I recently had the need to be able to create a VM on a few of our Hyper-V servers at work. I found a great article by The Virtual PC Guy on how to create a new VM. I took what he had and extended the functionality a bit. To manage Hyper-V, for now, we are pretty much stuck with WMI. However, Virtual Machine Manager will help us out a bunch. In the meantime, we can start with WMI. I figured it never hurts to know what is going on under the hood anyway.

One key piece of functionality that was not included was the ability to override the default location for new VM’s when they are created. This is critical to us as we are using a SAN and have dedicated LUN’s for our clustered VM’s. So now that you have the background, here’s the code

   1: function New-HyperVVM {
   2:     param    (
   3:             [string]$Hypervhost = "localhost",
   4:             [string]$Vm = "VM Courtesy of PowerShell",
   5:             [string]$location = "C:\MyVirtualMachines\$vm"
   6:             )
   7:     $wmiClassString = "\\" + $Hypervhost + "\root\virtualization:Msvm_VirtualSystemGlobalSettingData"
   8:     $wmiclass = [WMIClass]$wmiClassString
   9:     $newVSGlobalSettingData = $wmiClass.CreateInstance()
  10:     $newVSGlobalSettingData.psbase.Properties.Item("ExternalDataRoot").value = $location
  11:     $newVSGlobalSettingData.psbase.Properties.Item("ElementName").value = $Vm
  12:     $VSManagementService = gwmi MSVM_VirtualSystemManagementService -namespace "root\virtualization" -ComputerName $Hypervhost
  13:     $GlobalSettings  = $newVSGlobalSettingData.psbase.GetText(1)
  14:     $VSManagementService.DefineVirtualSystem($GlobalSettings, $ResourceSettings)
  15: }

By the way, this is posted up at PoshCode. I highly recommend getting the code from their, and updating it !

Advertisements

Managing Hyper-V with WMI

June 12, 2008

Dung K Hoang has an excellent series of blog posts on how to manage Hyper-V with WMI.

I have taken a couple of his scripts and built a function that takes input from the pipeline and gives me an object that has three properties, a HostServer, VirtualMachine, and a Switch.

   1: function Get-HyperVInfo {
   2:     begin {
   3:         $VmSwitchinfo = @();
   4:     }
   5:     process {
   6:         $computer = $_
   7:         $ListofVMs = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter "ElementName <> Name" -computer $computer
   8:         $ListofSwitches = gwmi -namespace root\virtualization Msvm_VirtualSwitch -computer $computer
   9:         $ListofSwitchPorts = gwmi -namespace root\virtualization Msvm_SwitchPort  -computer $computer
  10:         foreach ($Switch in $ListofSwitches)
  11: {
  12:             $SwitchGUID = $Switch.Name
  13:             $SwitchDisplayName = $Switch.ElementName
  14:             $PortsOnSwitch = $ListofSwitchPorts | where {$_.SystemName -match $SwitchGUID} 
  15:  
  16:             foreach ($Port in $PortsOnSwitch)
  17: {
  18:                 $PortPath = $Port.__PATH
  19:                 $ListofConnections = gwmi -namespace root\virtualization Msvm_ActiveConnection -computer $computer
  20:                 $a = $ListofConnections | where {$_.Antecedent -like $PortPath}
  21:                 if ($a -ne $NULL)
  22: {
  23:                     $LANEndPoint = $a.Dependent 
  24:                     foreach ($VM in $ListofVMs)
  25: {
  26:                         $VMGUID = $VM.Name
  27:                         $VMDisplayName = $VM.ElementName
  28:                         if ($LanEndPoint -like "*$VMGUID*")
  29: {
  30:  
  31:                             $vminfo = "" |Select-Object VirtualMachine ,HostServer, switch
  32:                             $vminfo.Switch = $SwitchDisplayName
  33:                             $vminfo.VirtualMachine = $VMDisplayName
  34:                             $vminfo.HostServer = $_
  35:                             $vmswitchinfo += $vminfo
  36:  
  37:                         }
  38:                     }
  39:                 }
  40:             }
  41:         } 
  42:  
  43:  
  44:     }
  45:     end {
  46:     $vmswitchinfo
  47:     }
  48: }

To use this function you can just do something like this

“HVSERVER1″,”HVSERVER2″,”HVSERVER3” | Get-HypervInfo

I have used this script to get a bunch of information on all the VM’s in my Hyper V Cluster. Note that this is just using WMI so I can query my Server Core machines that are running Hyper V.

Many Many thanks to Dung for this info. I would not have even been able to get started without his help.