Create a virtual machine and adding a VHD file using powershell

Or: How you can use Powershell to create virtual machines! 

With a few keystroke you can create a virtual machine and attach an existing VHD(X) file to it! I usually look for the fastest and easiest way to get things done. Always manually rebuilding my lab was tiresome and demotivating so I started to look at different possible solutions. The first possible solution for me was experimenting with the Microsoft Deployment Toolkit, which was brilliant! But it still took to long to install a VM and still too much effort (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 code in the script below.

The script uses powershell to ask you what kind of OS you want to deploy, creates a directory, the virtual machine and attaches the VHDX which is sitting pretty having received a sysprep after installation (and updates :)). To use the code below you will have to have your execution policy set to unrestricted (set-executionpolicy unrestricted) and preferably change the variables to point at your VHD(X) files.

More scripts can be found at the script library!

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}

}

Also published on:

https://geekswithblogs.net/marcde

https://blogs.technet.com/b/ieitpro/