Last active
August 28, 2018 13:27
-
-
Save JaekelEDV/18afe95e6be9ed228812c9a720ef468a to your computer and use it in GitHub Desktop.
Powershell Script Create Lab VM based on Master VHDx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Function New-LabVM quickly creates VM on Hyper-V for Lab Environments | |
.DESCRIPTION | |
This Script creates a Windows Server 2016, Windows Server 2012 R2 or Windows 10 Generation 2 VM | |
with differencing disk based on existing Master-VHDx you have to create before executing this one. | |
Be sure to adjust paths in the "Parameter Section Region" according to your environment. | |
It connects to an existing external vSwitch to activate the License. | |
The VM starts automatically. | |
.PARAMETER OSType | |
Choice between Server2012R2, Server 2016, Server 2016_core or Windows10. All depending on existing Master-VHDx. | |
.PARAMETER VMName | |
Sets the Name of the VM to create. | |
.EXAMPLE | |
Execute New-LabVM.ps1 directly from Shell with dot sourcing | |
. .\New-LabVM.ps1 | |
New-LabVM -VMName Value -OSType Value | |
You might consider putting the function in your PS-Profile. | |
.NOTES | |
Author: Oliver Jäkel | oj@jaekel-edv.de | @JaekelEDV | |
#> | |
#region Parameter Section | |
Function New-LabVM { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true)][ValidateSet('Server2012R2','Server2016','Server2016_core','Windows10')][string]$OSType, | |
[Parameter(Mandatory=$true)][string] $VMName) | |
[string] $VMPath = "d:\$VMName" | |
[string] $VHDXPath = "d:\$VMName\$VMName.vhdx" | |
[long] $VHDXSize = 136365211648 | |
[string] $MasterVHDXServer2012R2 = 'd:\MASTER\master_2012R2.vhdx' | |
[string] $MasterVHDXClient = 'd:\MASTER\master_win10.vhdx' | |
[string] $MasterVHDXServer2016 = 'd:\MASTER\master_2016.vhdx' | |
[string] $MasterVHDXServer2016Core = 'd:\MASTER\master_2016_core.vhdx' | |
#endregion | |
#region Import Hyper-V Module | |
$LoadedModules = (Get-Module).Name | |
if ($LoadedModules -notcontains 'Hyper-V') {Import-Module Hyper-V} | |
else {write-host 'Hyper-V Module already loaded' -ForegroundColor Yellow} | |
#endregion | |
#region Create VM | |
New-VM -Name $VMName -MemoryStartupBytes 1024MB -Generation 2 -NoVHD -Path $VMPath | |
#endregion | |
#region Set VM | |
Set-VM -Name $VMName -DynamicMemory -ProcessorCount 4 -MemoryMaximumBytes 4GB | |
# following is not supported for gen2 | |
# Set-VMBios -VMName $VMName -EnableNumLock | |
#endregion | |
#region Create differencing Disks according to OSType-Selection | |
switch($OSType){ | |
'Server2012R2' {New-VHD -Differencing -ParentPath $MasterVHDXServer2012R2 -Path $VHDXPath -SizeBytes $VHDXSize} | |
'Server2016' {New-VHD -Differencing -ParentPath $MasterVHDXServer2016 -Path $VHDXPath -SizeBytes $VHDXSize} | |
'Windows10' {New-VHD -Differencing -ParentPath $MasterVHDXClient -Path $VHDXPath -SizeBytes $VHDXSize} | |
'Server2016_core'{New-VHD -Differencing -ParentPath $MasterVHDXServer2016Core -Path $VHDXPath -SizeBytes $VHDXSize} | |
} | |
#endregion | |
#region Attach Disk to VM | |
Add-VMHardDiskDrive -VMName $VMName -ControllerType SCSI -ControllerNumber 0 -Path $VHDXPath | |
#endregion | |
#region Set Bootorder | |
Set-VMFirmware -VMname $VMName -FirstBootDevice (Get-VMHardDiskDrive -VMName $vmName)[0] | |
#endregion | |
#region Determine external vSwitch and connect vNIC to it | |
$VMSwitch = (Get-VMSwitch -SwitchType External).Name | |
Get-VMNetworkAdapter -VMName $VMName | Connect-VMNetworkAdapter -SwitchName $VMSwitch | |
#endregion | |
#region Disable automatic Checkpoint | |
Set-VM -Name $VMName -AutomaticCheckpointsEnabled $false | |
#endregion | |
#region Start-VM | |
Start-VM -Name $VMName | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment