Provision VM on Hyper-V Cluster

The following code provides a scripted way to provision a Virtual Machine on a Hyper-V Cluster using SCVMM Powershell commandlets.
Step1
Create a Template VM with required Hard Disk and place it to the SCVMM Library
Step2
Run the script
Logic statements >

$Template = Get-Template -VMMServer $VMMServer | where {$_.Name -eq VDI_Template}

$Template holds the VDI Template Object that was created in step 1

$BestHost = $(Get-VMHostRating -Template $Template -VMName temp -VMHostGroup $VMClusterHostGroup -DiskSpaceGB 15 | sort rating -Descending | select -First 1 -Property VMHost).VMHost

$BestHost will hold the suitable hyper-v host from the cluster, it does by getting all the hosts from the cluster, sorts them by the rating and selects the first one.

$Path = $(Get-VMHostVolume -VMHost $BestHost | where {$_.IsClustered -and $_.IsAvailableForPlacement -and $_.Capacity -gt 15GB } | select Name -First 1).Name

The VM Path is selected by querying volume for the selected Hyper-V host and filters out the local disks with $_.IsClustered and Quorum disk with $_.Capacity properties.

Complete Script

###########################################################################
#
#
NAME: VM Provion on Hyper-V Cluster
#
#
AUTHOR: SivaMulpuru
#
#
COMMENT:
#
#
VERSION HISTORY:
#
1.0 6/24/2011 – Initial release
#
#
##########################################################################

# Add VMM snapin
Add-PSSnapin -Name Microsoft.SystemCenter.VirtualMachineManager
$VMName = ChildVM
$VMMServer = SCVMM01.MULPURU.LOCAL
Get-VMMServer -ComputerName
$VMMServer
$MAC1 = New-PhysicalAddress -Commit
PVS MAC $MAC1
$MAC2 = New-PhysicalAddress -Commit
VM MAC $MAC2
$Template = Get-Template -VMMServer $VMMServer | where {$_.Name -eq VDI_Template}
$BestHost = $(Get-VMHostRating -Template $Template -VMName temp -VMHostGroup $VMClusterHostGroup -DiskSpaceGB 15 | sort rating -Descending | select -First 1 -Property VMHost).VMHost
Placing $VMName on $BestHost
$Path = $(Get-VMHostVolume -VMHost $BestHost | where {$_.IsClustered -and $_.IsAvailableForPlacement -and $_.Capacity -gt 15GB } | select Name -First 1).Name
$VMName path is $Path
$JobGroupID = [Guid]::NewGuid().ToString()
JobGroupID is $JobGroupID
$CPUType = Get-CPUType -VMMServer $VMMServer | where {$_.Name -eq 2.20 GHz Opteron (dual core)}
New-VirtualNetworkAdapter -VMMServer
$VMMServer -JobGroup $JobGroupID -PhysicalAddress $MAC1 -PhysicalAddressType Static -VirtualNetwork STREAMING -VLanEnabled $false -VMNetworkOptimizationEnabled $false -MACAddressesSpoofingEnabled $false
New-VirtualNetworkAdapter -VMMServer
$VMMServer -JobGroup $JobGroupID -PhysicalAddress $MAC2 -PhysicalAddressType Static -VirtualNetwork INTERNET -VLanEnabled $false -VMNetworkOptimizationEnabled $true -MACAddressesSpoofingEnabled $false -Synthetic
New-HardwareProfile -VMMServer
$VMMServer -Owner MULPURU\Siva -CPUType $CPUType -Name HWProfile$JobGroupID -Description Profile used to create a VDI VM Template -CPUCount 4 -MemoryMB 512 -ExpectedCPUUtilization 20 -DiskIO 0 -CPUMax 100 -CPUReserve 0 -NetworkUtilization 0 -RelativeWeight 100 -HighlyAvailable $true -NumLock $false -BootOrder PxeBoot, CD, IdeHardDrive, Floppy -LimitCPUFunctionality $false -LimitCPUForMigration $false -DynamicMemoryEnabled $true -DynamicMemoryMaximumMB 4096 -DynamicMemoryBufferPercentage 20 -MemoryWeight 5000 -VirtualVideoAdapterEnabled $false -JobGroup $JobGroupID
HWProfile$JobGroupID Hardware Profile created.
$HardwareProfile = Get-HardwareProfile -VMMServer $VMMServer | where {$_.Name -eq HWProfile$JobGroupID}
New-VM -Template
$Template -Name $VMName -Description “” -VMHost $BestHost -RunAsynchronously -RunAsSystem -StartAction NeverAutoTurnOnVM -StopAction SaveVM -SkipInstallVirtualizationGuestServices -UseHardwareAssistedVirtualization $true -JobGroup $JobGroupID -Path $Path -HardwareProfile $HardwareProfile
if(!$?)
{
$VMName creation failed.
$HardwareProfile | Remove-HardwareProfile
continue
}
$VMName creation started.
$HardwareProfile | Remove-HardwareProfile
HWProfile$JobGroupID removed from library.

Leave a comment

Your email address will not be published.