In my previous post I showed how I created a Windows 2012R2 image.However, the deployment process I use is the same every windows version (Excluding Nano obviously). If you save the script text as PowerShell script (i.e. .ps1) and amend the below values it should work ok for you
$VmPath = “E:\Hyper-V Guests\Virtual Machines” This should be location that you want the virtual machine created (not the hard disk)
$VHDPath = “E:\Hyper-V Guests\Virtual Hard Disks” This should be location that you want the virtual machine hard disk created.
$switch = “EXT_SW0” This is the name of the vm switch you have configured on your hyper-v hosts. If these are not the same on all your hosts, the script will be unable to add the network adapter. You could set the $switch value in the select statement, i.e. remove it from further down and set a different $switch value for each machine type (Like the generation is set)
$MaxBytes = 4294967296 This is the maximum value for dynamic memory (Ram)
$MinBytes = 536870912 This is the minimum value for dynamic memory (Ram)
$SUBytes = 1073741824 This is the start up bytes for dynamic memory
The script
$cred = Get-Credential -Credential "workgroup\hyperv"
do
{
$HVHost = read-host "Confirm Hyper-V Host Name"
$Session = New-PSSession -Name Host -ComputerName $HVHost -Credential $cred
Invoke-Command -Session $Session -ScriptBlock {
Write-Host "OS Selection."
Write-Host "1. Windows 2012 R2"
Write-Host "2. Windows 2008 R2"
Write-Host "3. Windows 8.1 Enterprise"
Write-Host "4. Tech Preview 2014 OCT"
Write-Host "5. Windows 10 2014 OCT"
Write-Host " "
$a = Read-Host "Select 1-5: "
Write-Host " "
switch ($a)
{
1 {
"** Windows 2012 R2 **";
$VHDTemplate = "D:\templates\W2012R2_Gen2d.vhdx";
[int]$Gen = 2 ;
break;
}
2 {
"** Windows 2008 R2 **";
$VHDTemplate = "D:\templates\W2k8R2.vhdx";
[int]$Gen = 1 ;
break;
}
3 {
"** Windows 8.1 Enterprise **";
$VHDTemplate = "D:\templates\Win81Ent.vhdx";
[int]$Gen = 2;
break;
}
4 {
"** Tech Preview 2014 OCT **";
$VHDTemplate = "D:\templates\TP2014OCT_Server.vhdx";
[int]$Gen = 2;
break;
}
5 {
"** Windows 10 2014 OCT **";
$VHDTemplate = "D:\templates\TP2014OCT_Windows10.vhdx";
[int]$Gen = 2;
break;
}
default {
"** The selection could not be determined **";
break;
}
}
[int]$VMcount = Read-host "Input Number of VMs "
$VMName = Read-host "Input Base Name (i.e DC0)"
$Total = 1
$VMs
$VmPath = "E:\Hyper-V Guests\Virtual Machines"
$VHDPath = "E:\Hyper-V Guests\Virtual Hard Disks"
$switch = "EXT_SW0"
$MaxBytes = 4294967296
$MinBytes = 536870912
$SUBytes = 1073741824
While ($total -le $VMCount) {
$NewVMName = $VMName + $total
New-VHD -ParentPath $VHDTemplate -Path "$($VHDPath)\$($NewVMName).vhdx" -Differencing
New-VM -VHDPath "$($VHDPath)\$($NewVMName).vhdx" -Generation $Gen -MemoryStartupBytes 1024MB -Name $NewVMName -Path $VmPath -SwitchName $switch
Set-VMMemory -VMName $NewVMName -DynamicMemoryEnabled $True -MaximumBytes $MaxBytes -MinimumBytes $MinBytes -StartupBytes $SUBytes
Enable-VMIntegrationService -Name "Guest Service Interface" -VMName $NewVMName
Start-vm $NewVMName
$Total++
}
$VMs = Get-VM | Sort-Object Name
ForEach ($VM in $VMs)
{
Write-Host "Virtual Machine:" $VM.Name " - Status:" $VM.Status
$vNICs = $VM | select-object -ExpandProperty NetworkAdapters
Write-Host "Virtual NICs:" ($vNICs | measure).Count
ForEach ($vNIC in $vNICs)
{
Write-Host " - " $vNIC.EthernetAddress" - "$vNIC.EthernetAddressType
}
Write-Host
}
}
$Continue = read-host "Do you wish to create another VM? (y or n) "
}
until ($Continue -like "n")
As you can see there is no error checking and it’s not optimised, however I will add this at some point. Saying that, its taken me 5 months to post this, so don’t hold your breath 🙂
Post 2: Hyper-V Host Configuration
Post 3 Creating the VM templates
Post 4 PS Script to deploy VMs
Post 5 Configure deployed VMs General