I use this script to deploy one or more identical servers in Hyper-V
. The script handles both the use of a Golden Image template .VHDX file, or a clean installation using an ISO install image and will provide you with a question by question gathering of the most common installation parameters
.
Prerequisites:
- Windows Server 2012 release candidate or above
- Powershell 3.0
- Hyper-V role installed on the server
- Hyper-V Powershell module installed on the server
- A template VHDX file for each operating system you’d like to be able to deploy
- ISO-files for each operating system you’d like to be able to deploy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<# v1.0 - 25.09.2012 By Reidar J . Boldevin #> # Specify the local path for your virtul machines and hard disks $vmpath = 'D:\Hyper-V\Virtual Machines\Virtual Machines\' $vmdiskpath = 'D:\hyper-v\Virtual Hard Disks\' # Query the user on installation parameters [int]$vmcount = Read-Host "How many identical servers would you like to create?" $vmname = Read-Host "What would you like to call the servers (numbers will be added)?" [int64]$vmminram = 1MB*(Read-Host "Minimum memory in megabytes?") [int64]$vmmaxram = 1MB*(Read-Host "Maximum memory in megabytes?") [int64]$vhdsize = 1GB*(Read-Host "How big would you like the system disk to be (in gigabytes)?") # Give the user options to create new VM's based on a template VHDX-file or through a clean installation with a pre mounted ISO-file Write-Host "1. Windows Server 2008R2 2. Windows Server 2012RC" $vmversion = Read-Host "What server version would you like to install?" if ($vmversion -eq '1') { $vmmount = 'C:\warez\en_windows_server_2008_r2_standard_enterprise_datacenter_and_web_with_sp1_x64_dvd_617601.iso' $vmtemplate = 'D:\Templates\2008R2_template1.vhdx' } elseif ($vmversion -eq '2') { $vmmount = 'C:\warez\en_windows_server_2012_release_candidate_datacenter_x64_dvd_863833.iso' $vmtemplate = 'D:\Templates\2012_template1.vhdx' } cls # Functions to create new or copy server from template function Create-NewServer { $vmcounter = 1 while ($vmcounter -le $vmcount) { $vmnewpath = "$vmdiskpath$vmname$vmcounter.vhdx" New-VM -Name $vmname$vmcounter -Path "$vmpath$vmname$vmcounter" -MemoryStartupBytes $vmminram -NewVHDPath $vmnewpath -NewVHDSizeBytes $vhdsize -SwitchName 'Physical to Virtual' Set-VMMemory -VMName $vmname$vmcounter -DynamicMemoryEnabled $true -MinimumBytes $vmminram -MaximumBytes $vmmaxram Set-VMDvdDrive -VMName $vmname$vmcounter -Path $vmmount $vmcounter ++ } } function Copy-TemplateServer { $vmcounter = 1 while ($vmcounter -le $vmcount) { $vmnewpath = "$vmdiskpath$vmname$vmcounter.vhdx" Copy-Item $vmtemplate $vmnewpath Set-ItemProperty $vmnewpath -Name IsReadOnly -Value $false New-VM -Name $vmname$vmcounter -Path "$vmpath$vmname$vmcounter" -MemoryStartupBytes $vmminram -SwitchName 'Physical to Virtual' -VHDPath $vmnewpath Set-VMMemory -VMName $vmname$vmcounter -DynamicMemoryEnabled $true -MinimumBytes $vmminram -MaximumBytes $vmmaxram $vmcounter ++ } Write-Host "NB. Remember to run Sysprep with Generalize option after booting the server!" } # Query the user to create a new server, or to copy from an exisiting template Write-Host "1. Copy an exitising template" Write-Host "2. New server from scratch" [int]$deploytype = Read-Host "Would you like to copy a template or install a new server from scratch?" if ($deploytype -eq 1) { Copy-TemplateServer } elseif ($deploytype -eq 2) { Create-NewServer } else { Write-Host "Not a valid option" } |