PowerCLI script for vCenter configuration


As part of my efforts to automate further within the private cloud environment at work, setting up a home lab and studying for the VCAP-DCA exam, I have been working on a PowerCLI script to configure vCenter once it is deployed. (Note: we are also working on deploying vCenter itself using Puppet, but that’s another post).

It has been really good to work through the functionality exposed through PowerCLI (Version 5.8 Release 1) and those only exposed through the API. I would also like to note that @vBrianGraf from VMware Tech Marketing has been an awesome resource for several different pieces that are not currently exposed directly through PowerCLI.

The goals for the script were as follows:

  1. Setup Datacenter and Folder management.
  2. Create cluster(s) with High Availability and DRS configured.
    1. HA would set the HA Admission Control Policy to reserver 25% of CPU and Memory.
  3. Create vDS and default portgroup.
  4. Setup Autodeploy.
    1. For the Intel NUC, this included adding a custom VIB for the e1001 network driver.

The script can be downloaded here and seen after the break.

# setup_vcenter.ps1
#
# Created by Chris Mutchler on 1/13/2015
#
# Setup and configure a vCenter for VPC from scratch.
# -Configure AutoDeploy
# -Configure vDS
# -Configure Clusters
# -Configure Storage & Storage Clusters

# Standard-defined Variables
# DO NOT MODIFY
$clusters = @("SMALL", "MEDIUM", "LARGE", "XLARGE", "2XLARGE", "3XLARGE")

# User-defined Variables
[string]$VCENTER = ""
[string]$DC = ""
[string]$USERNAME = "administrator@vsphere.local"
[string]$PASSWORD = ""
[string]$FQDN = ".localdomain"
[string]$ESXiDepot = "E:\VMware-Software\esxi5.5_update02-2068190_offline.zip"
[string]$ESXiVersion = "ESXi-5.5U2"
[string]$VENDOR = "Home-Lab"
[string]$ModelPattern = "ipv4=172.20.1.1-172.20.1.254"

# Connect to the new vCenter
$vcs = $VCENTER + "." + $DC + $FQDN
Connect-VIServer $vcs -User $USERNAME -Password $PASSWORD

# Create the Datacenter Object and return flat object
$folder = Get-Folder -NoRecursion
New-Datacenter -Location $folder -Name $DC | fl

# Setup Auto-Deploy
Add-EsxSoftwareDepot $ESXiDepot
# This line is troublesome still - returning Name: filename-standard
$ProfileName = Get-EsxImageProfile | where { $_.Name -match "-standard" } | select Name
New-EsxImageProfile -CloneProfile $ProfileName -Name $ESXiVersion -Vendor $VENDOR
$DeployRuleName = "CPT-" + $EsxiVersion
New-DeployRule -Name $DeployRuleName -Item $ESXiVersion -Pattern $ModelPattern
Add-DeployRule -Name $DeployRuleName

# Still have copy hardwired and tramp files to TFTPD server

# Setup the default clusters
foreach ($cluster in $clusters) {
    $ClusterName = $DC + "-FC-" + $cluster + "-1"
    New-Cluster -Name $ClusterName -Location $DC -DRSEnabled -DRSAutomationLevel 'FullyAutomated' -HAEnabled -HAAdmissionControlEnabled -HAFailoverLevel 2 -VMSwapfilePolicy "WithVM" -HARestartPolicy "Medium" -HAIsolationResponse "DoNothing" -EVCMode 'intel-sandybridge'
    
    # Modify HAAdmissionControl Policy to slot based with 25% resources for failover
    $mycluster = Get-Cluster $ClusterName | Get-View
    $spec = New-Object VMware.Vim.ClusterConfigSpecEx
    $spec = vmSwapPlacement = "vmDirectory"
    $spec.dasConfig = New-Object VMware.Vim.ClusterDasConfigInfo
    $spec.dasConfig.vmMonitoring = "vmMonitoringDisabled"
    $spec.dasConfig.admissionControlPolicy = New-Object VMware.Vim.ClusterFailoverResourcesAdmissionControlPolicy
    $spec.dasConfig.admissionControlPolicy.cpuFailoverResourcesPercent = 25
    $spec.dasConfig.admissionControlPolicy.memoryFailoverResourcesPercent = 25
    $spec.dasConfig.defaultVmSettings = New-Object VMware.Vim.ClusterDasVmSettings
    $spec.dasConfig.defaultVmSettings.restartPriority = "medium"
    $spec.dasConfig.defaultVmSettings.isolationResponse = "none"
    $spec.dasConfig.defaultVmSettings.vmToolsMonitoringSettings = New-Object VMware.Vim.ClusterVmToolsMonitoringSettings
    $spec.dasConfig.defaultVmSettings.vmToolsMonitoringSettings.enabled = $true
    $spec.dasConfig.defaultVmSettings.vmToolsMonitoringSettings.vmMonitoring = "vmMonitoringOnly"
    $spec.dasConfig.defaultVmSettings.vmToolsMonitoringSettings.failureInterval = 30
    $spec.dasConfig.defaultVmSettings.vmToolsMonitoringSettings.minUpTime = 120
    $spec.dasConfig.defaultVmSettings.vmToolsMonitoringSettings.maxFailures = 3
    $spec.dasConfig.defaultVmSettings.vmToolsMonitoringSettings.maxFailureWindow = 3600
    $spec.drsConfig = New-Object VMware.Vim.ClusterDrsConfigInfo
    $spec.drsConfig.enabled = $true
    $spec.drsConfig.defaultVmBehavior = "fullyAutomated"
    $spec.drsConfig.vmotionRate = 3
    $spec.dpmConfig = New-Object VMware.Vim.ClusterDpmConfigInfo
    $spec.dpmConfig.enabled = $false
    $spec.dpmConfig.hostPowerActionRate = 3
    
    $reconfigcluster = $mycluster.moref
    
    $reconfigcluster = Get-View -Id $mycluster.moref
    $reconfigcluster.ReconfigureComputeResource_Task($spec, $true)
} #/END cluster creation

# Setup the distributed switch
$vDSName = "vSW-A-1." + $DC
New-VDSwitch -Name $vDSName -Location $DC
New-VDPortgroup -VDSwitch $vDSName -Name "default-Portgroup"

# Setup the storage cluster
$StorageClusterName = "HUSVM-" + $DC + "-1"
New-DatastoreCluster -Name $StorageClusterName -Location $DC | Out-Null
Set-DatastoreCluster -DatastoreCluster $StorageClusterName -SdrsAutomationLevel 'FullyAutomated' -IOLoadBalanceEnabled -IOLatencyThresholdMillisecond 15 -SpaceUtilizationThresholdPercent 80 -Confirm:$false | Out-Null