PoshCode Archive  Artifact [caa5d979b6]

Artifact caa5d979b6557bfc8d9aea8f3beec4175bc716da5e2c5f308cc5a1d9aa277736:

  • File New-HyperVVM.ps1 — part of check-in [c181816c18] at 2018-06-10 13:59:47 on branch trunk — Uses WMI to create a new virtual machine (user: unknown size: 1391)

# 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)
}