| 1 |
# |
| 2 |
# copyright (c) 2014-2016 david betz |
| 3 |
# used to setup labs via pxeboot or iso, recreate vms based on existing vhds, or create vms with differencing disks |
| 4 |
# |
| 5 |
function hyperv_create { param($name, $os = $null, $iso = $null, $diskLocation = $null, $diskSize = 128GB, $gen = 2, $memory = 2GB, $maxMemory = 2GB, $processorCount = 8, $addPrivateNetwork = $False, $auxDiskCount = 0, $auxDiskSize = 0, $networkBoot = $false) |
| 6 |
$diskRootDefault = 'V:\_VM' |
| 7 |
$templateFolder = 'V:\_VMTEMPLATE' |
| 8 |
$programDataFolder = 'V:\_VM\__PROGRAMDATA' |
| 9 |
$vhdxSystemPath = "$diskRootDefault\$name.vhdx" |
| 10 |
if ($os) { |
| 11 |
$baseDiskPath = "$templateFolder\$os.vhdx" |
| 12 |
} |
| 13 |
$switchName = 'External Virtual Switch' |
| 14 |
$privateSwitchName = 'Private Virtual Switch' |
| 15 |
|
| 16 |
if($maxMemory -gt $memory) { |
| 17 |
$memory = $maxMemory |
| 18 |
} |
| 19 |
|
| 20 |
if((Test-Path $vhdxSystemPath)) { |
| 21 |
Write-Host "Disk already exists. Aborting. `n`t$vhdxSystemPath" |
| 22 |
} |
| 23 |
else { |
| 24 |
if(!$diskLocation -and $os) { |
| 25 |
Write-Host "Creating VHD $vhdxSystemPath" |
| 26 |
New-VHD -SizeBytes $diskSize -Path $vhdxSystemPath -Differencing -ParentPath $baseDiskPath > $null |
| 27 |
} |
| 28 |
elseif($iso -or $networkBoot) { |
| 29 |
$vhdxSystemPath = ("{0}`\{1}.vhdx" -f $diskRootDefault, $name) |
| 30 |
if((Test-Path $vhdxSystemPath)) { |
| 31 |
Write-Host "!!ALREADY EXISTS: $vhdxSystemPath" |
| 32 |
return |
| 33 |
} |
| 34 |
Write-Host "Creating VHD $vhdxSystemPath" |
| 35 |
New-VHD -Path $vhdxSystemPath -Dynamic -Size 127GB > $null |
| 36 |
} |
| 37 |
else { |
| 38 |
$vhdxSystemPath = $diskLocation |
| 39 |
} |
| 40 |
|
| 41 |
Write-Host "Creating VM $name" |
| 42 |
$vm = New-VM -Name $name -Generation $gen -Memory $memory -VHDPath $vhdxSystemPath -SwitchName $switchName > $null |
| 43 |
Set-VMMemory -VMName $name -MaximumBytes $maxMemory |
| 44 |
|
| 45 |
Write-Host "Setting VM $name proc count to $processorCount" |
| 46 |
Set-VMProcessor -VMName $name -Count $processorCount > $null |
| 47 |
|
| 48 |
Write-Host "Setting VM $name checkpoint type to Standard" |
| 49 |
Set-VM -VMName $name -CheckpointType Standard -AutomaticStartAction Nothing -AutomaticStopAction ShutDown -SmartPagingFilePath $programDataFolder -SnapshotFileLocation $programDataFolder |
| 50 |
|
| 51 |
if($gen -eq 2) { |
| 52 |
Set-VMFirmware -VMName $name -EnableSecureBoot Off |
| 53 |
} |
| 54 |
|
| 55 |
if($iso) { |
| 56 |
Write-Host "Adding boot ISO" |
| 57 |
$dvd = Add-VMDvdDrive -VMName $name -Path $iso > $null |
| 58 |
Set-VMFirmware -VMName $name-FirstBootDevice $dvd |
| 59 |
if($gen -eq 1) { |
| 60 |
Set-VMBios -VMName $name -StartupOrder @("CD", "LegacyNetworkAdapter", "Floppy", "IDE") |
| 61 |
} |
| 62 |
else { |
| 63 |
Set-VMFirmware -VMName $name -FirstBootDevice $dvd |
| 64 |
} |
| 65 |
} |
| 66 |
elseif($networkBoot) { |
| 67 |
if($gen -eq 1) { |
| 68 |
Add-VMNetworkAdapter -IsLegacy $true -VMName $name |
| 69 |
Set-VMBios -VMName $name -StartupOrder @("LegacyNetworkAdapter", "Floppy", "CD", "IDE") |
| 70 |
} |
| 71 |
else { |
| 72 |
Set-VMFirmware -VMName $name -FirstBootDevice (Get-VMNetworkAdapter -VMName $name | select -first 1) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if($addPrivateNetwork) { |
| 77 |
Add-VMNetworkAdapter -VMName $name -SwitchName $privateSwitchName |
| 78 |
} |
| 79 |
|
| 80 |
if($auxDiskCount -gt 0) { |
| 81 |
function addAuxDisk { param([Int32]$id) |
| 82 |
$vhdxPath = ("{0}`\{1}-Disk{2}.vhdx" -f $diskRootDefault, $name, $id.ToString("D2")) |
| 83 |
#+ increasing sizes makes it easier to identify drives inside VM |
| 84 |
if($auxDiskSize -gt 0) { |
| 85 |
$size = $auxDiskSize * 1024*1024*1024 |
| 86 |
} |
| 87 |
else { |
| 88 |
$size = ((70 + ($id*10)) * 1024*1024*1024) |
| 89 |
} |
| 90 |
Write-Host ("Adding disk {0} of size {1}" -f $vhdxPath, $size) |
| 91 |
New-VHD -path $vhdxPath -Dynamic -Size $size > $null |
| 92 |
Add-VMHardDiskDrive -VMName $name -ControllerType SCSI -Path $vhdxPath > $null |
| 93 |
} |
| 94 |
1..$auxDiskCount | % { |
| 95 |
addAuxDisk -id $_ |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
#+ PXE example |
| 102 |
# hyperv_create 'PXE Client 01' -gen 1 -memory 2GB -auxDiskCount 0 -networkBoot $true |
| 103 |
|
| 104 |
#+ iso example |
| 105 |
# $winsrv2012iso = 'H:\ISO\Volume\SW_DVD5_Windows_Svr_Std_and_DataCtr_2012_R2_64Bit_English_Core_MLF_X19-05182.ISO' |
| 106 |
# hyperv_create 'Windows Server 2012 Core' -iso $winsrv2012iso -gen 2 -memory 2GB -maxMemory 4GB -auxDiskCount 0 |
| 107 |
|
| 108 |
#+ differencing disk (for existing image) example |
| 109 |
# hyperv_create 'CentOS 6 (Lab 01)' -os 'CentOS 6 (Gen 1)' -gen 1 -memory 2GB -maxMemory 4GB -auxDiskCount 2 -auxDiskSize 80 |
| 110 |
# hyperv_create 'AD Server 01' -os 'Windows Server 2012 R2' -gen 2 -memory 4GB -maxMemory 4GB -auxDiskCount 0 -auxDiskSize 80 |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|