PoshCode Archive  Artifact [73d6559fcf]

Artifact 73d6559fcf611bb577084a677c016c6dfa57173cfb9e1ab21966d0322878fbe9:

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

# 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: 2365
# x-archived: 2010-11-23T15:11:25
#
# 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)
}