Deploy VMs from .CSV and PowerCLI 5.5r1 CreateVM v3.1 with OS Customization and IP Addressing!

Post date: Oct 7, 2013 7:34:22 PM

Updates and customization/sysprep for linux and windows. Takes a little longer but automation is great! Saves on OS configuration time. Also now VMs with >64gb of vRAM can be deployed with enhancements of pCLI 5.5.

Currently working on optimization... to include IPaddressing through the use of Set-OSCustomizationNicMapping and less get-vm statements.

Notes:

Use 32-bit PowerCLI 5.5 Release 1 - Issue with setting IP addresses if 64bit used for RHEL

Verify Enough Storage and Ports on designated Portgroups. Dont forget to add configured vRAM to storage requirements.

*OCT15: Resolved syntax error and included DHCP OS Specifications

#####################################################################

#Create a virtual machine for each line of a specified csv file

#

# CSV format:

# VMName Location Cluster ResourcePool Datastore MemoryGB NumCpu OSdisk Disk2 Disk3 Description Customer Template NetworkName NetworkName2 IPaddress Netmask Gateway

#

#Version 3 Updates:

#Utilizing OS Customization Specifications

#Applying IP information

#Updated for PowerCLI 5.5 Release 1

#Opitmized

#

#Template can have static IP address for management and ease of administration

#

#***TEMPLATE MUST HAVE VMWARE TOOLS INSTALLED AND CURRENT***

#

# parameters:

# -filename to input list of vms to create

# Example:

#.\create-vms.ps1 -filename create-vms-ClientA.csv

#

# Example:

#.\create-vms.ps1 .\create-vms-ClientA.csv

#

# Version v3.1 AUG 2013 KnightUSN *Updated OCT2013

#####################################################################

param(

[parameter(Mandatory = $true)]

[string[]]$filename

)

#Getting List of VMs from .csv

$VMs = Import-CSV $filename -UseCulture

$DNS1 = "<Primary DNS IP>"

$DNS2 = "<Secondary DNS IP>"

#Loop to Provision VMs

ForEach($VM in $VMs){

#Selecting a Random hots to place the VM. There is a maximum of 8 concurrent deployments to a single host. VAAI allows for 2 concurrent svmotions when running multiples of this script from the same template.

$ClusterHost = Get-Cluster $VM.Cluster | Get-VMHost | Where{$_.ConnectionState -eq "Connected"} | Get-Random

If($VM.IPaddress -gt 1){

#Define OS Customization with IP Addressing

If($VM.Template -eq "<Windows Template Name>"){

$OSspec = "<Windows OS Spec>"

Get-OSCustomizationSpec $OSspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.IPaddress -SubnetMask $VM.Netmask -DefaultGateway $VM.Gateway -Dns $DNS1,$DNS2

}

Else{

$OSspec = "<RHEL OS Spec>"

Get-OSCustomizationSpec $OSspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.IPaddress -SubnetMask $VM.Netmask -DefaultGateway $VM.Gateway

}

}

#Define OS Customization with DHCP

Else{

If($VM.Template -eq "<Windows Template Name>"){

$OSspec = "<DHCP-Windows OS Spec>"

}

Else{

$OSspec = "<DHCP-RHEL OS Spec>"

}

}

#Creating the New VM. SDRS automatic recomendations take hold.

New-vm -VMhost $ClusterHost -Name $VM.VMName -ResourcePool (Get-cluster $VM.Cluster | Get-ResourcePool $VM.ResourcePool) -Location $VM.Location -Datastore $VM.Datastore -Template $VM.Template -Description $VM.Description -DiskStorageFormat "Thin" -OScustomizationSpec $OSspec

#Get-VM once for Optimization

$gVM = Get-VM $VM.VMName

#Setting the Customer Field

$gVM | Set-Annotation -CustomAttribute Customer -Value $VM.Customer

#Setting the Configured Memory

$gVM | Set-VM -MemoryGB $VM.MemoryGB -NumCpu $VM.NumCpu -Confirm:$false

#Setting the PortGroup of Network Adapter 1

$gVM | Get-NetworkAdapter | Set-NetworkAdapter -PortGroup $VM.NetworkName -confirm:$false

#Configuring OSdisk if more than template required

If ([int]$VM.OSdisk -gt 1){

$VMdisk1 = $gVM | Get-HardDisk | ?{$_.Name -eq "Hard disk 1"}

If ([int]$VM.OSdisk -gt $VMdisk1.CapacityGB){

$gVM | Get-HardDisk | ?{$_.Name -eq "Hard disk 1"} | Set-HardDisk -CapacityGB [int]$VM.OSdisk -Persistence persistent -confirm:$false

}

}

#Adding HDD2 if exist and converting GB to KB

If ([int]$VM.Disk2 -gt 1){

$Disk2 = [int]$VM.Disk2

$gVM | New-HardDisk -CapacityGB $Disk2 -Persistence persistent

}

#Adding HDD3 if exist and converting GB to KB

If ([int]$VM.Disk3 -gt 1){

$Disk3 = [int]$VM.Disk3

$gVM | New-HardDisk -CapacityGB $Disk3 -Persistence persistent

}

#Adding second NIC and PortGroup if exist

If ($VM.NetworkName2 -gt 1){

$gVM | New-NetworkAdapter -Type vmxnet3 -Portgroup $VM.NetworkName2 -StartConnected -confirm:$false

}

#PowerOn VM

$gVM | Start-VM

#Next VM

}