VMware Cloud Community
FireDog7881
Contributor
Contributor

Configuring new build Host to be consistent

This is one of my first scripts that I created to quickly build out ESXi hosts without having Host Profiles or Distributed Virtual Switches. The main reason for this script is to configure the Networking so our Portgroups are consistent. It also configures the host to use Round Robin as default for datastores as well as configure existing LUNs to use Round Robin (ESXCLI must be installed).

This script utilizes a CSV file to pull the portgroup names. The location of the CSV file is set in the variables.

This script asks you questions so you don't need to enter parameters into the command line, this is meant to be run interactively.

This script sets Host Attributes, you need to create the host attributes in vCenter before running this script.

This script assumes you have 4 NICs available for the Host.

It uses vswitch0 with vmnics 0 & 1 for Service Console and VMotion

Creates vSwitch1 and uses vmnics 2 & 3 for VM Traffic.

Creates vSwitch2 with no physical NICs for Internal communication to quarantine VMs if necessary.

Please comment and let me know if there are better ways to do what I am doing.

Thank you.

$EnteredHost = Read-Host "Please enter the name of the host you want to configure"
$vMotionIP = Read-Host "Please enter the IP address for the VMotion portgroup"
$ByCreated = Read-Host "Please enter your Name"
$HostLocation = Read-Host "Please enter physical host location"
$vMotionSubnetMask = '255.255.255.0'
$PGcsv = Import-Csv "./portgroups.csv"
$esxvmhost = get-VMHost $EnteredHost
$DateCreated = Get-Date -format D
#Create new Virtual Switch with Nics2 and 3
Write-Host -ForegroundColor Yellow "Creating new Virtual Switch - vSwitch1 with NICs 2 and 3"
$esxvmhost | New-VirtualSwitch -name "vSwitch1" -nic vmnic2,vmnic3 -numports 128
Write-Host -ForegroundColor Yellow "Creating new Virtual Switch for isolated Internal portgroup"
$esxvmhost | New-VirtualSwitch -name "vSwitch2"
$esxvmhost | Get-VirtualSwitch -name "vSwitch2" | New-VirtualPortGroup -name "Internal"
#Create vMotion Portgroup
Write-Host -ForegroundColor Yellow "Creating VMotion Portgroup on vSwitch0 with IP address $vMotionIP"
New-VMHostNetworkAdapter -VMHost $esxvmhost -PortGroup “VMotion“ -VirtualSwitch vSwitch0 -IP $vMotionIP -SubnetMask $vMotionSubnetMask -VMotionEnabled:$true
#Create Portgroups on vSwitch 1
#This uses a CSV file called portgroups.csv located in the same place as this executable
foreach ($portgroup in $PGcsv){
    $PGname = $portgroup.portgroup
    $PGid = $portgroup.id
    Write-Host -ForegroundColor Yellow "Creating Portgroup $PGname with vlan ID of $PGid"
$esxvmhost | Get-VirtualSwitch -name "vSwitch1" | New-VirtualPortGroup -name $PGname -vlanid $PGid
}
#Remove Default Portgroup VM Network
#Need to add an "if" statement so as to not get an error when VM Network isn't found
$DefaultPG = Get-VirtualPortgroup -host $EnteredHost -Name ‘VM Network‘ -ea SilentlyContinue
Remove-VirtualPortGroup -VirtualPortGroup $DefaultPG -Confirm:$false -ea SilentlyContinue
#Set defualt PSP to Round Robin,
#ESXCLI must be installed in default location for this to work
Write-Host -ForegroundColor Yellow "Setting Round Robin on host $esxvmhost"   
& 'C:\Program Files\VMware\VMware vSphere CLI\bin\esxcli.exe' --server $EnteredHost --user root --password "REMOTE*&^control" nmp satp setdefaultpsp --satp VMW_SATP_ALUA --psp VMW_PSP_RR
#Set Round Robin on existing LUNroots
Write-Host -ForegroundColor Yellow "Setting Round Robin policy on existing LUNs"
Get-ScsiLun -VmHost $esxvmhost -LunType disk |  Set-ScsiLun -MultipathPolicy RoundRobin #-CommandsToSwitchPath 1
#Set Custom Attributes for VM
Write-Host -ForegroundColor Yellow "Setting Custome Attributes for VM"
($esxvmhost |Get-View).setCustomValue("CreatedBy", $ByCreated)
($esxvmhost |Get-View).setCustomValue("CreationDate", $DateCreated)
($esxvmhost |Get-View).setCustomValue("Location", $HostLocation)

0 Kudos
0 Replies