I’m a lazy bastard and am constantly looking for ways to deploy my lab machines faster and with less effort. I’m interested in learning and testing as much as I can but I just don’t want to be installing machines constantly!
The first step for me was experimenting with MDT, which was brilliant! But it still took to long to install a VM and still too much effort (told you I’m lazy : ))! So the second step for me was to create a number of VHDX files and use those to deploy the virtual machines resulting in the following code.
It uses Powershell to create the virtual machine and copy the VHDX file to the folder of the VM & attach it. I consider this a poor mans SCVMM or at least a beginning to it. In the future I have plans expanding this code to be able to deploy a complete lab with a minimal touch configuration. But I’m not there yet…
To use this script you will need preinstalled and sysprepped VHDX files in place (my script has them on D:\Base VHD\). Change the patch to match your own VHD file location
Code:
function Run-As
{
# Use Check Switch to check if admin
param([Switch]$Check)
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()`
).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if ($Check) { return $IsAdmin }
if ($MyInvocation.ScriptName -ne "")
{
if (-not $IsAdmin)
{
try
{
$arg = "-file `"$($MyInvocation.ScriptName)`""
Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList $arg -ErrorAction 'stop'
}
catch
{
Write-Warning "Error - Failed to restart script with runas"
break
}
exit # Quit this session of powershell
}
}
else
{
Write-Warning "Error - Script must be saved as a .ps1 file first"
break
}
write-host "Script Running As Administrator" -foregroundcolor red
Write-host ""
}
#Defining Variables. Change these patch to your own!
$2008R2STD = "D:\Base VHD\2k8R2std.vhdx"
$2008R2ENT = "D:\Base VHD\2k8R2ent.vhdx"
$2008R2DC = "D:\Base VHD\2k8r2dc.vhdx"
$2012STD = "D:\Base VHD\2012std.vhdx"
$2012ENT = "D:\Base VHD\2012ent.vhdx"
#Launching run-as function
Run-as
#Cleaning screen
cls
# Offer choice in which OS to deploy
Do {
Write-host
Write-host "Wich operating system would you like to deploy?" -foregroundcolor Green
Write-host "1. 2008 R2 Standard" -foregroundcolor Yellow
Write-host "2. 2008 R2 Enterprise" -foregroundcolor Yellow
Write-host "3. 2008 R2 Datacenter" -foregroundcolor Yellow
Write-host "4. 2012 Standard" -foregroundcolor Yellow
Write-host "5. 2012 Enterprise" -foregroundcolor Yellow
$deploy = Read-host "Please select an operating system"
if ($deploy -eq "") {write-host "error:Please select and operating system" -foregroundcolor Red}; if ($deploy -eq $NULL) {write-host "error: Please select an operating system" -foregroundcolor Red}
write-host
} while ($deploy -eq "" -or $deploy -eq $NULL)
#What should the VM be called
$Name = Read-Host "Enter the Virtual Machine name (Press [Enter] to choose Srv01)"
if ($Name -eq ""){$Name="Srv01"} ; if ($Name -eq $NULL){$Name="Srv01"}
#how much memory needs to be assigned (static only at this point)
$Memory = Read-Host "Enter the size of the Virtual Machine Memory (Press [Enter] to choose 2048MB)"
if ($Memory -eq ""){$Memory=2048MB} ; if ($Memory -eq $NULL){$Memory=2048MB}
#Where should the VM be stored
$Location = Read-Host "Enter the location of the Virtual Machine file (Press [Enter] to choose D:\Virtual Machines)"
if ($Location -eq ""){$Location="D:\Virtual Machines"} ; if ($Location -eq $NULL){$Location="D:\Virtual Machines"}
#Listing Networks on the host
Write-host
get-vmswitch |select Name, SwitchType
write-host
#Which network should be used
$Network = Read-Host "Enter the name of the Virtual Machine Network (Press [Enter] to choose External)"
if ($Network -eq ""){$Network="External"} ; if ($Network -eq $NULL){$Network="External"}
#VM creation starts here
new-vm $Name -path $Location
Switch ($deploy)
{
1 {Copy-Item $2008R2STD $Location\$Name ; add-vmharddiskdrive -vmname $Name -path $Location\$NaAme\2K8R2std.vhdx}
2 {Copy-Item $2008R2ENT $Location\$Name ; add-vmharddiskdrive -vmname $Name -path $Location\$Name\2K8R2ent.vhdx}
3 {Copy-Item $2008R2DC $Location\$Name ; add-vmharddiskdrive -vmname $Name -path $Location\$Name\2K8R2dc.vhdx}
4 {Copy-Item $2012STD $Location\$Name ; add-vmharddiskdrive -vmname $Name -path $Location\$Name\2012STD.vhdx}
5 {Copy-Item $2012ENT $Location\$Name ; add-vmharddiskdrive -vmname $Name -path $Location\$Name\2012ENT.vhdx}
}
get-vm $Name | add-vmdvddrive -controllernumber 1
get-vm $Name | set-vmmemory -startupbytes $Memory
get-vm $Name | add-vmnetworkadapter -switchname $Network
# Start the VM.
start-vm $Name
cls
#Success message.
Switch ($deploy)
{
1 {Write-host "Windows 2008 R2 Standard Deployed!" -foregroundcolor Green}
2 {Write-host "Windows 2008 R2 Enterprise Deployed!" -foregroundcolor Green}
3 {Write-host "Windows 2008 R2 Datacenter Deployed!" -foregroundcolor Green}
4 {Write-host "Windows 2012 Standard Deployed!" -foregroundcolor Green}
5 {Write-host "Windows 2012 Enterprise Deployed!" -foregroundcolor Green}
}