OSCustomization for VM builds using PowerCLI

VM builds are probably the boring tasks that are repeatedly done by any vSphere Admins be it in Lab or Production, so it’s a good idea to automate them and spend time on better things. In this post i will be showing the script that i have been using since many years when i come across those VM builds and it only takes less than 5 min to create a VM – love automation!

In order to use the script, we need to actually create a OS customization first and this is a one time task and also we need to have a Windows VM Template created. Once you have it just bring up the PowerShell console and use the below script, the script prompts for VM name, Reserved IP, Windows Version and VM port group information and once you enter them and that’s it.

<#
.SYNOPSIS
This script deploys a VM from template, joins the Domain;Uses VMware built-in OS Customization. Tested for Windows Server 2008 R2 and 2012 R2.
.DESCRIPTION
Script prompts for name of the VM you want to deploy, IP, VLAN and Windows Version
.NOTES
Use PowerCLI v6 and above preferably one pulled from PSGallery, PowerShell v4 and above
#>

Param(
[string]$VMName = (Read-Host "Enter the Name of the VM "), 
[string]$VMIP = (Read-Host "Enter the IP reserved for the VM "),
[string]$PortGroup = (Read-Host "Enter the VLAN for the VM, example 10 "),
[string]$Winver = (Read-Host "Enter the Windows version, example 2012 ")
)

#Import PowerCLI Module
Import-Module VMware.PowerCLI

# Connect to vSphere02-SC9.virginam.com
Connect-VIServer vcenter.vmmaster.local

# Choose the right nicmapping for the vlan
if ($PortGroup -eq 10)
{
$vmvlan = 10
$pgnicmapping = "vlan10" 
$vmGateway = "<IP Here>"
}
elseif ($PortGroup -eq 20)
{
$vmvlan = 20
$pgnicmapping = "vlan20"
$vmGateway = "<IP Here>"
}

# Choose the right template
if ($Winver -eq 2008)
{
$vmTemplate =Get-Template -Name Template_Win_2008R2
}
else
{
$vmTemplate = Get-Template -Name Template_Win_2012R2
}

#Select Prod Cluster
$vmCluster = Get-Cluster -Name <Cluster Name>

#Select a Random Datastore connected to Prod Cluster
$vmDatastore = $vmCluster | Get-Datastore | Where {($_.ExtensionData.Summary.MultipleHostAccess -eq "True") -and ($_.FreeSpaceMB -gt "102400") -and ($_.State -ne "Maintenance")} | Get-Random

#Clone the Customization Spec
$vmSpecification = Get-OSCustomizationSpec -Name Windows_Builds | New-OSCustomizationSpec -Name WindowsTemp -Type NonPersistent

#Update the portgroup and gateway based on the VLAN entered
$tempnicmapping = Get-OSCustomizationSpec $vmSpecification | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -ipmode UseStaticIP -IpAddress $VMIP -SubnetMask 255.255.255.0 -DefaultGateway $vmGateway -Dns <DNS IPs here>

#Build the VM
New-VM -Name $VMName -Template $vmTemplate -OSCustomizationSpec $vmSpecification -ResourcePool $vmCluster -Datastore $vmDatastore -DiskStorageFormat Thin

Write-Host " New VM Details `n Name:`t $VMName `n OS:`t Windows Server $Winver R2 `n IPAddress:`t $VMIP `n Datastore:`t $vmDatastore" -ForegroundColor Gray

Start-Sleep -Seconds 30

#Set the right portgroup for the VM
Get-VM $VMName | Get-NetworkAdapter -Name "Network adapter 1" | Set-NetworkAdapter -NetworkName $pgnicmapping -Confirm:$false -StartConnected:$True

#Power On the VM for the customization to start
Write-Host " Powering on the VM $VMName" -ForegroundColor Green
$vm = Start-VM $VMName -Confirm:$false

Write-Host "`n Waiting for the customization to complete! Monitor the console to see the progress.......... `n" -ForegroundColor Green

while (Get-VM $VMName | where {$_.PowerState -eq "PoweredOn"})
{
$vmEvents = Get-VIEvent -Entity $VMName
$succeeded = $vmEvents | Where { $_.GetType().Name -eq "CustomizationSucceeded" }
$failed = $vmEvents | Where { $_.GetType().Name -eq "CustomizationFailed" }

if ($failed)
{
Write-Host " Customization failed!" -ForegroundColor Red
return $False
}

if($succeeded)
{
Write-Host " Customization Complete!" -ForegroundColor Green
return $True
}

Start-Sleep -Seconds 10 
}

If you are interested in procedure for creating Customization Specification, here it is. Adding batch files that can install and configure config management tools like Puppet, Chef in the Run Once section will actually ensure all the needed configurations are present for the application.

This slideshow requires JavaScript.

Hope this was informative. Thanks!

Advertisements

Virtual Machine Component Protection – VMCP

vSphere 6.0 has introduced a new feature called VMCP. Below is the vmware blog i found and i just thought, the concept cannot be explained any simpler than this.

So this is just a reference and i do not want to add anything to it. Enjoy reading the article below.

VM Component Protection

#apd, #ha, #pdl, #vmcp, #vmcp-recovery-timeline, #vmcp-recovery-workflow

Lockdown Mode – vSphere 6

Lockdown Mode in ESXi host is around the corner since version 4. There were no ‘major’ changes to it untill version 6. VMware has come up with a more effective way of hardening the ESXi hosts in the vSphere 6 as there were ways to bypass lockdown mode in earlier versions. vSphere with version 6 introduced Strict Lockdown Mode and Exception Users to accomplish this. So without any delay, let’s get started with our discussion. Continue reading

#dcui-access, #exception-users, #lockdown-mode, #strict-lockdown-mode, #vsphere-6

Availability Features – What’s New

Hey there! In this post we will find about the Availability features that were introduced with vSphere 6. Availability has always been an important characteristic of virtual systems and vSphere has provided many enhancements to this with the new version. Continue reading

#fastcheckpointing, #fastlz, #vadp

Networking and Storage – What’s New

So, moving onto our next topic in the What’s New series, we have our Networking and Storage features. This post will introduce you to the new features that are included with the vSphere version 6. So let’s get started. Continue reading

#nioc-3, #protocol-endpoints, #spbm, #storage-containers, #tcpip, #vasa-provider, #vvol

vCenter Server Features – What’s New

Hey there! In our last post we have seen the basic platform features that were introduced in vSphere 6, now let’s see the vCenter Server enhancements and changes in version 6 like Architectural changes, Certificate Management, vMotion enhancements, vSphere Web Client. Continue reading

#content-library, #cross-vcenter, #cross-vswitch, #embedded, #psc, #vcsa, #vecs, #vmca