<#
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" }