Archive for the ‘PoshCode.org’ 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 !

Start Posting to http://poshcode.org – NOW !!!

August 18, 2008

Hal posted a comment a while back asking if I would consider putting some of the functions i had been blogging about up on http://www.poshcode.org. I had heard of it and even used some of the scripts up there but for whatever reason I hadn’t bothered to post anything. Hal finally convinced me to put some code up there. I have to say, its ridiculously easy.

My 0.02 is this should be the default shared online repository for Powershell scripts and functions. So I have one question for you.. Why are you still reading this and not posting your latest and greatest script up to PoshCode?

Go and do it.. now !

-Andy