Lab Deployment PowerCLI Script for TCC VMware ICM Class
Finished up another successful vSphere 6 Install Configure Manage class at Tidewater Community College last week. Time to re-provision the lab environments to get things ready for the next class.
I wrote a PowerCLI script to help with the lab provisioning process. The script does the follow:
- Distributes vApps across available hosts in my TCC lab cluster.
- Creates an isolated vSwitch on the host and the required PortGroups for each lab environment.
- Clones the Master Lab vApp to the lab vApp.
- Attaches the Lab VM’s Network Adapters to Correct Port Groups.
I have a vApp which has been configured for the lab environment with all the necessary bits and pieces needed for the ICM labs. The Master Lab vApp was inspired by AutoLab and has been modified to support the lab exercises for the ICM class. I also have a version configured for the Optimize and Scale class, and I am working on a version for the View ICM class.
Here is a pretty basic diagram of what each lab environment looks like.
Each lab is connected to an isolated vSwitch (no physical uplinks) with two PortGroups. One PortGroup has promiscuous mode enabled (set to Accept) this PortGroup is used for the VM Networks on the nested ESXi servers, the other PortGroup has promiscuous mode disabled (Set to Reject) which is used for all other VMs. Students access the labs by RDPing to the Classroom connected router VM which has a port-based NAT to forward the RDP connection through to the vDC.
The vDC is a Windows Server 2008 R2 VM (soon to be updated to Windows Server 2012) which acts as the lab domain controller and has all the software (clients, putty, class isos, etc) on it. The vCenter (VCSA) and ESXi servers provide the nested virtual environment for the lab. The NAS is a FreeNAS Appliance which provides iSCSI and NFS Storage for the lab environment.
The script is below but you can find the most up to date copy here: https://github.com/herseyc/PowerCLI-Scripts/blob/master/TCC-Lab-Deploy.ps1
####################################################################### # PowerCLI script to Deploy TCC VMware IT Academy Labs for ITN254/ITN255/ITN231 from Master vApp # Usage TCC-Lab-Deploy.ps1 # # Distributes vApps across available hosts # Creates Isolated vSwitch # Creates Promiscuous Mode PortGroup # Creates Standard PortGroup # Clones Master vApp # Attaches VM Network Adapters to Correct Port Groups # # Requires active connection to vCenter Server (using Connect-VIServer) # # History: # 12/11/2015 - Hersey http://www.vhersey.com/ - Created # ####################################################################### ############Lab Variables########################## #Cluster Name $ClusterName = "LABCLUSTER" #Master vApp $MastervApp = "Test" #Datastore $Datastore = "NFS_Datastore1" #SetvApp Prefix $vAppPrefix = "LAB" #ClassRoom PortGroup $ClassRoomPG = "ClassRoom" #Master vApp Promiscuous Mode PortGroup $vAppPMNetwork = "MasterPM" #Master vApp Standard PortGroup $vAppStdNetwork = "Master" #Number of Labs to Deploy $numlabs = "1" ################################################## #Set Counters $pick = 0 #Get Datastore for vApp Deployment $vAppDatastore = Get-Datastore -Name $Datastore #Get Hosts available for vApp Deployment $Hosts = Get-VMHost -Location $ClusterName $numhost = $Hosts.Count Write-Host "Deploying Lab Across $NumHosts Hosts" Foreach ( $_ in 1..$numlabs ) { $lab++ If ( $pick -eq $numhost -Or $pick -gt $numhost ) { Write-Host "Reseting PickHost" $pick = 0 } $DeployHost = $Hosts[$pick] Write-Host "Pick: $pick" Write-Host "Deploy on Host: $DeployHost" Write-Host "Lab: $lab" $LabName = "$vAppPrefix-$lab" #Create Isolated Lab vSwitch $PMPGName = "$LabName-PM" Write-Host "Creating $LabName vSwitch" $vSwitch = New-VirtualSwitch -VMHost $DeployHost -Name $LabName #Create Promiscuous Mode PortGroup Write-Host "Creating Promiscuous Mode PortGroup $PMPGName on vSwitch $LabName" $vAppPMPG = New-VirtualPortGroup -Name $PMPGName -VirtualSwitch $vSwitch #Set Promiscuous Mode on PortGroup Write-Host "Setting Promiscuous Mode PortGroup $PMPGName on vSwitch $LabName" Get-VirtualPortGroup -VirtualSwitch $vSwitch -Name $PMPGName | Get-SecurityPolicy | Set-SecurityPolicy -AllowPromiscuous $true #Create Non-Promiscuous Mode Port Group Write-Host "Creating PortGroup $LabName on vSwitch $LabName" $vAppPG = New-VirtualPortGroup -Name $LabName -VirtualSwitch $vSwitch Write-Host "Deploying $LabName" New-vApp -VApp $MastervApp -Name $LabName -Location $Hosts[$pick] -VMHost $Hosts[$pick] -Datastore $vAppDatastore #Connect VM Network Adapters to Correct PortGroups $vAppVMs = Get-VApp $LabName | Get-VM ForEach ($vAppVM in $vAppVMs ) { Get-VM -Location $LabName $vAppVM | Get-NetworkAdapter | Where {$_.NetworkName -eq $vAppPMNetwork} | Set-NetworkAdapter -Portgroup $vAppPMPG -Confirm:$false Get-VM -Location $LabName $vAppVM | Get-NetworkAdapter | Where {$_.NetworkName -eq $vAppStdNetwork} | Set-NetworkAdapter -Portgroup $vAppPG -Confirm:$false } $pick++ }
So just a quick walk-through of what is happening. There are a few things that need to be configured, the $ClusterName where the vApp will be deployed, the $MastervApp to clone, the $Datastore where the cloned vApps will be stored, the $vAppPrefix this will be used to name the cloned vApps, some information about the networks ($vAppPMNetwork and $vAppStdNetwork), and finally the $numlabs of labs to deploy.
The script then gets the hosts in the $ClusterName and figures out how many there are.
It then loops through the hosts, creates the isolated vSwitch and required PortGroups, and then clones the $MastervApp to a new vApp on the selected host. Finally it connects the cloned vApp’s network adapters to the newly created PortGroups.
There are quite a few things I could do better here and I am going to work through them. For example the script can only be used to deploy all the labs. I cannot deploy labs 1-5 and then come back later and deploy labs 6-10. I plan to make some changes to support this. I also may change things around to pass the variables as command line parameters.
Comments and thoughts are always appreciated.
Have a great day!
Looking good Hersey, keep up the good work. TCC couldn’t get those classes done without you.