//
you're reading...
Hyper-V, PowerShell

PowerShell scripts to create one or more Hyper-V Virtual Servers

In this post you’ll find a couple of PowerShell scripts that I wrote to create Hyper-V servers with system resources specified when you run the scripts.

The scripts, CreateSingleServer.ps1 is the one to use to create a single server and CreateMultipleServers.ps1 is used to create multiple servers and don’t require any external PowerShell modules apart from the native Hyper-V ones. Simply run the scripts on the host to create the VM’s, entering the parameters when prompted.

I created and tested these on a Hyper-V 2012 host but haven’t had the chance to test it on one of our 2008 R2 hosts as of writing – suffice to say they should work as long as you modify the references to the VHD files from VHDX to VHD in the script.

You can download the zip file containing both scripts by clicking here

If you don’t want to download them, below is the full listing of both scripts, copy them into PowerShell ISE and the formatting should take care of itself

CreateSingleServer.ps1:

# Variables
$vm = Read-Host “Virtual Machine name: “

$vcpu = Read-Host “Number of virtual CPU’s: “

[int64]$sram = Read-Host “Dynamic memory is enabled. Enter the starting Virtual Machine Memory (Press [Enter] to choose 1024MB): “
$sram = $sram * 1024 * 1024
if ($sram -eq “”){$sram=1073741824} ; if ($sram -eq $NULL){$sram=1073741824}

[int64]$mram = Read-Host “Enter the Maximum Virtual Machine Memory (Press [Enter] to choose 2048MB): “
$mram = $mram * 1024 * 1024
if ($mram -eq “”){$mram=2147483648} ; if ($mram -eq $NULL){$mram=2147483648}

[int64]$vhdsize = Read-Host “Enter the size of the VHD (Press [Enter] to choose 80GB): “
$vhdsize = $vhdsize * 1024 * 1024 * 1024
if ($vhdsize -eq “”){$vhdsize=85899345920} ; if ($vhdsize -eq $NULL){$vhdsize=85899345920}

$vmloc = Read-Host “Save location for the Virtual Machine file (Press [Enter] to choose E:\Virtual Machines): “
if ($vmloc -eq “”){$vmloc=“E:\Virtual Machines\”} ; if ($vmloc -eq $NULL){$vmloc=“E:\Virtual Machines”}

$vhdloc = Read-Host “Save location for the VHD file (Press [Enter] to choose V:\Virtual Hard Disks): “
if ($vhdloc -eq “”){$vhdloc=“V:\Virtual Hard Disks”} ; if ($vhdloc -eq $NULL){$vhdloc=“V:\Virtual Hard Disks”}

# Create Virtual Machine
MD $vmloc -ErrorAction SilentlyContinue
MD $vhdloc\$vm
New-VM $vm -path $vmloc
New-VHD -path $vhdloc\$vm\$vm”.vhdx” -SizeBytes $vhdsize
Add-VMHardDiskDrive -VMName $vm -ControllerNumber 0 -path $vhdloc\$vm\$vm”.vhdx”
Get-VM $vm | Set-VMMemory -DynamicMemoryEnabled $true -StartupBytes $sram -MinimumBytes $sram -MaximumBytes $mram
Write-Host $vm ” Virtual Machine has been created”

CreateMultipleServers.ps1

# This script allows you to create multiple servers

# Get the number of servers
$numsrvs = Read-Host “Number of servers to create: “

#Build array of servers
for ($i=0; $i -lt $numsrvs; $i++)
{
$srv = Read-Host “Virtual Machine name: “
$srvarray +=, $srv

$vcpu = Read-Host “Number of virtual CPU’s: “
$srvarray += ,$vcpu

[int64]$sram = Read-Host “Dynamic memory is enabled. Enter the starting Virtual Machine Memory (Press [Enter] to choose 1024MB): “
$sram = $sram * 1024 * 1024
if ($sram -eq “”){$sram=1073741824} ; if ($sram -eq $NULL){$sram=1073741824}
$srvarray += ,$sram

[int64]$mram = Read-Host “Enter the Maximum Virtual Machine Memory (Press [Enter] to choose 2048MB): “
$mram = $mram * 1024 * 1024
if ($mram -eq “”){$mram=2147483648} ; if ($mram -eq $NULL){$mram=2147483648}
$srvarray += ,$mram

[int64]$vhdsize = Read-Host “Enter the size of the VHD (Press [Enter] to choose 80GB): “
$vhdsize = $vhdsize * 1024 * 1024 * 1024
if ($vhdsize -eq “”){$vhdsize=85899345920} ; if ($vhdsize -eq $NULL){$vhdsize=85899345920}
$srvarray += ,$vhdsize

$vmloc = Read-Host “Save location for the Virtual Machine file (Press [Enter] to choose E:\Virtual Machines): “
if ($vmloc -eq “”){$vmloc=“E:\Virtual Machines\”} ; if ($vmloc -eq $NULL){$vmloc=“E:\Virtual Machines\”}
$srvarray += ,$vmloc

$vhdloc = Read-Host “Save location for the VHD file (Press [Enter] to choose V:\Virtual Hard Disks): “
if ($vhdloc -eq “”){$vhdloc=“V:\Virtual Hard Disks\”} ; if ($vhdloc -eq $NULL){$vhdloc=“V:\Virtual Hard Disks\”}
$srvarray += ,$vhdloc
}

# Create Virtual Machines. This loop is abit messy right now, but works. Will fix it up when I get chance.
[int]
$start = 0
for ($i=0; $i -lt $srvarray.Length; $i = $i+7) {
$srv = $srvarray[$i]
$vcpu = $srvarray[$i+ 1]
[int64]$sram = $srvarray[$i+ 2]
[int64]$mram = $srvarray[$i+ 3]
[int64]$vhdsize = $srvarray[$i+ 4]
$vmloc = $srvarray[$i+ 5]
$vhdloc = $srvarray[$i+ 6]

MD $vmloc -ErrorAction SilentlyContinue
MD $vhdloc\$srv
New-VM $srv -path $vmloc
New-VHD -path $vhdloc\$srv\$srv”.vhdx” -SizeBytes $vhdsize
Add-VMHardDiskDrive -VMName $srv -ControllerNumber 0 -path $vhdloc\$srv\$srv”.vhdx”
Get-VM $srv | Set-VMProcessor -Count $vcpu
Get-VM $srv | Set-VMMemory -DynamicMemoryEnabled $true -StartupBytes $sram -MinimumBytes $sram -MaximumBytes $mram
Write-Host $srv ” Virtual Machine has been created”
}

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: