# encoding: ascii
# api: powershell
# title: New-HyperVVM
# description: Uses WMI to create a new virtual machine
# version: 0.1
# type: function
# license: CC0
# function: New-HyperVVM
# x-poshcode-id: 571
# x-archived: 2009-11-03T15:10:01
#
# inspired by “The Virtual PC Guy” http://blogs.msdn.com/virtual_pc_guy/archive/2008/05/28/scripting-vm-creation-with-hyper-v.aspx
# Added the ability to specify a location to store the Virtual Machine
# Also added remote capability to create a VM on a remote HyperV Host
# Andy Schneider, http://www.get-powershell.com
#
function New-HyperVVM {
	param	(
			[string]$Hypervhost = "localhost",
			[string]$Vm = "VM Courtesy of PowerShell",
			[string]$location = "C:\MyVirtualMachines\$vm"
			)
	$wmiClassString = "\\" + $Hypervhost + "\root\virtualization:Msvm_VirtualSystemGlobalSettingData"
	$wmiclass = [WMIClass]$wmiClassString
	$newVSGlobalSettingData = $wmiClass.CreateInstance()
	$newVSGlobalSettingData.psbase.Properties.Item("ExternalDataRoot").value = $location
	$newVSGlobalSettingData.psbase.Properties.Item("ElementName").value = $Vm
	$VSManagementService = gwmi MSVM_VirtualSystemManagementService -namespace "root\virtualization" -ComputerName $Hypervhost
	$GlobalSettings  = $newVSGlobalSettingData.psbase.GetText(1)
	$VSManagementService.DefineVirtualSystem($GlobalSettings, $ResourceSettings)
}
