Home > ESX, Flex-10, HP, PowerCLI, Powershell, VMware > Scripting Flex-10 ESX design with PowerCLI

Scripting Flex-10 ESX design with PowerCLI

February 18th, 2011

My Flex-10 ESX design with simplicity and scalability posts detail the configuration steps required to deploy ESX with Flex-10. There’s a lot of clicking around in the vSphere client that can be automated with PowerCLI.

The steps below will set up the ESX networking component as described in the design posts so can be incorporated into the rest of your PowerCLI build process.

The information text and colours are optional but it does make it easier for the person building the ESX host to clearly see what is happening as the script runs and notice any errors.

$ESXHost=Get-VMHost "vmhost01.local"

Write-Host "Getting Host Networking Information"  -ForegroundColor Green
Write-Host ""
$HostNetworkInfo = $ESXHost | Get-VMHostNetwork

#Get actual ESX hostname
$vmhost = $HostNetworkInfo.HostName

Write-Host "Connected to ESX host:- " $ESXHost.Manufacturer " " $ESXHost.Model ": " $vmhost "." $HostNetworkInfo.DomainName -Separator "" -ForegroundColor Green
Write-Host ""
$ESXHostView=$ESXHost | Get-View

# Work out Host Number by extracting the numbers from the host name $vmhost variable and storing as integer. Example vmhost01 would return 01
# This allows you to calculate IP address information based on the host number
[int]$HostNum = (([regex]'\d+').matches($vmhost) | select Value).Value

# Variables to store IP address information
$vSwitch0PortNumber = 128
$VMKernelVLAN = 603
$VMKernelIP = "10.1.100." + ($HostNum+100)  # Add 100 to HostNum value to work out vmkernel IP
$VMKernelMask = "255.255.255.0"
$VMKernelGateway = "10.1.100.254"
$vMotionIP = "192.168.0." + $HostNum 		# Use HostNum value for vMotion IP
$vMotionMask = "255.255.255.0"
{$PortGroups = ("servers_dev",600),
               ("servers_prod",641),
               ("vdi1",632)}

If ($ESXHost.State -notmatch "Maintenance") {
	Write-Host "Putting Host into Maintenance Mode" -ForegroundColor Green
	$ESXHost | Set-VMHost -State maintenance}

Write-Host "Removing any VM networks" -ForegroundColor Green
$ESXHost | Get-VirtualPortGroup | Where-Object {$_.Port -eq $Null} | Remove-VirtualPortGroup -Confirm:$False

Write-Host "Removing any empty Virtual Switches" -ForegroundColor Green
$ESXHost | Get-VirtualSwitch | Where-Object { $Null -eq (Get-VirtualPortGroup -VirtualSwitch $_)} | Remove-VirtualSwitch -Confirm:$False

Write-Host "Configuring vSwitch0" -ForegroundColor Green
$vSwitch0 = $ESXHost | Get-VirtualSwitch | where {$_.Name -like "vSwitch0"}

Write-Host "Setting the Port Number on vSwitch0" -ForegroundColor Green
$vSwitch0 | Set-VirtualSwitch -NumPorts $vSwitch0PortNumber -Confirm:$false

Write-Host "Adding vmnic1 to vSwitch0" -ForegroundColor Green
$vSwitch0 | Set-VirtualSwitch -Nic vmnic0,vmnic1 -Confirm:$false

Write-Host "Enabling Beacon Probing on vSwitch0" -ForegroundColor Green
$vSwitch0 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -NetworkFailoverDetectionPolicy BeaconProbing

Write-Host "Adding VMKernel Port Group to vSwitch0" -ForegroundColor Green
$VMKernel = $vSwitch0 | New-VirtualPortGroup -Name vmkernel -vlanid $VMKernelVLAN

Write-Host "Adding VMKernel IP Address" -ForegroundColor Green
$ESXHost | New-VMHostNetworkAdapter -PortGroup $VMKernel -VirtualSwitch $vSwitch0 -IP $VMKernelIP -SubnetMask $VMKernelMask

Write-Host "Adding VMKernel Gateway" -ForegroundColor Green
$HostNetworkInfo | Set-VMHostNetwork -VMKernelGateway $VMKernelGateway

Write-Host "Setting vmnic1 to an Active Adapter for VMKernel" -ForegroundColor Green
$VMKernel | Get-NicTeamingPolicy |  Set-NicTeamingPolicy -MakeNicActive "vmnic1"

Write-Host "Setting vmnic0 to a Standby Adapter for VMKernel" -ForegroundColor Green
$VMKernel | Get-NicTeamingPolicy |  Set-NicTeamingPolicy -MakeNicStandby "vmnic0"

Write-Host "Overriding failover order for VMKernel" -ForegroundColor Green
$VMKernel | Get-NicTeamingPolicy |  Set-NicTeamingPolicy -InheritFailoverOrder $false -InheritFailback $true -InheritLoadBalancingPolicy $true -InheritNetworkFailoverDetectionPolicy $true -InheritNotifySwitches $true

Write-Host "Creating vSwitch1 and vMotion Port Group" -ForegroundColor Green
$ESXHost | New-VirtualSwitch -Name "vSwitch1" -Nic vmnic2,vmnic3 | New-VirtualPortGroup -Name vmotion

Write-Host "Adding vMotion vmkernel IP Address" -ForegroundColor Green
New-VMHostNetworkAdapter -PortGroup vmotion -VirtualSwitch vSwitch1 -IP $vMotionIP -SubnetMask $vMotionMask -VMotionEnabled $true

Write-Host "Configuring vSwitch1" -ForegroundColor Green
$vSwitch1 = $ESXHost | Get-VirtualSwitch | where {$_.Name -like "vSwitch1"}

Write-Host "Enabling Beacon Probing on vSwitch1" -ForegroundColor Green
$vSwitch1 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -NetworkFailoverDetectionPolicy BeaconProbing

Write-Host "Creating Virtual Machine Port Groups on vSwitch0" -ForegroundColor Green
$PortGroups | %{$vSwitch0 | New-VirtualPortGroup -Name $_[0] -vlanid $_[1]}

Write-Host "Setting VM Port Group Failover order" -ForegroundColor Green
$VMPortGroups = $vSwitch0 | Get-VirtualPortGroup | Where-Object {$_.Port -eq $Null}
foreach ($VMPortGroup in $VMPortGroups){
	Write-Host $VMPortGroup.Name ": Set vmnic0 to an Active Adapter"  -ForegroundColor Green
	$VMPortGroup | Get-NicTeamingPolicy |  Set-NicTeamingPolicy -MakeNicActive "vmnic0"
	Write-Host $VMPortGroup.Name ": Set vmnic1 to a Standby Adapter"  -ForegroundColor Green
	$VMPortGroup | Get-NicTeamingPolicy |  Set-NicTeamingPolicy -MakeNicStandby "vmnic1"
	Write-Host $VMPortGroup.Name ": Override the vSwitch failover order"  -ForegroundColor Green
	$VMPortGroup | Get-NicTeamingPolicy |  Set-NicTeamingPolicy -InheritFailoverOrder $false -InheritFailback $true -InheritLoadBalancingPolicy $true -InheritNetworkFailoverDetectionPolicy $true -InheritNotifySwitches $true
}
Comments are closed.