Last week I was working on an environment running VMware vSphere Enterprise licenses. The job at hand was to expand the compute environment with some new blades. Due to the lack of host profiles, I wanted to be able to provision a new host with all the necessary datastores from CSV and all the correct vSwitch settings.
The scripts connects to vCenter, and asks which vSphere host you want to provision using their hostname. I used 2 functions; The first is used to configure the vSwitch settings, the second to configure the NFS advanced settings along with creating the datastores from a CSV file.
Function Configure_network
Within the network function, the default ‘VM Network’ portgroup is deleted.
As used for this specific client, the IP addresses are automatically set. The last octet being the same in every subnet.
 Function Configure_datastores
We’re only using NFS in this environment. Therefor we set the correct NFS advanced options along with the creation of the new datastores. The information for the datastores is retrieved from a CSV file.
The CSV should be formatted like this:
The actual script…
#Set the vCenter server $vCenterServer = "cf-vc01.cloudfix.lab" #Get the IP Address of the ESX host function Get-HostToIP($hostname) { $result = [system.Net.Dns]::GetHostByName($hostname) $result.AddressList | ForEach-Object {$_.IPAddressToString } } Function configure_network() { #Set the parameters for vSwitch0. $vs0vSwitchname = "vSwitch0" $vs0vSwitchNIC1 = "vmnic6" $vs0vSwitchNIC2 = "vmnic7" $vs0vPortGroupName = "VM Network" $vs0vPortGroupVlanID = "9" $vs0vPortGroupName2 = "VM_LAN" $vs0vPortGroupVlanID2 = "9" $vs0vPortGroupName3 = "DMZ" $vs0vPortGroupVlanID3 = "6" $vs0vPortGroupName4 = "App1" $vs0vPortGroupVlanID4 = "11" $vs0vPortGroupName5 = "App2" $vs0vPortGroupVlanID5 = "12" $vs0vPortGroupName6 = "VoIP" $vs0vPortGroupVlanID6 = "105" $vs0vPortGroupName7 = "Frontend" $vs0vPortGroupVlanID7 = "100" $vs0vPortGroupName7 = "Replication" $vs0vPortGroupVlanID7 = "17" #Set the parameters for vSwitch1. $vs1vSwitchname = "vSwitch1" $vs1vSwitchNIC1 = "vmnic0" $vs1vSwitchNIC2 = "vmnic1" $vs1vPortGroupName = "vMotion" $vs1vPortGroupIP = $IP_firstpart + "2" + $IP_lastpart $vs1vPortGroupMask = "255.255.255.0" $vs1vPortGroupVlanID = "2" #Set the parameters for vSwitch2. $vs2vSwitchname = "vSwitch2" $vs2vSwitchNIC1 = "vmnic2" $vs2vSwitchNIC2 = "vmnic3" $vs2vPortGroupName = "NFS" $vs2vPortGroupIP = $IP_firstpart + "3" + $IP_lastpart $vs2vPortGroupMask = "255.255.255.0" #Reconfigure vSwitch0. Write-Host "Reconfigure vSwitch0" -foregroundcolor green Get-VMHost $VMHost | Get-VirtualSwitch -Name $vs0vSwitchname | Set-VirtualSwitch -Nic $vs0vSwitchNIC1,$vs0vSwitchNIC2 Get-VMHost $VMHost | Get-VirtualPortGroup -Name $vs0vPortGroupName | Remove-VirtualPortGroup -Confirm:$true Get-VMHost $VMHost | Get-VirtualSwitch -Name $vs0vSwitchname | New-VirtualPortGroup -Name $vs0vPortGroupName2 -VLanId $vs0vPortGroupVlanID2 Get-VMHost $VMHost | Get-VirtualSwitch -Name $vs0vSwitchname | New-VirtualPortGroup -Name $vs0vPortGroupName3 -VLanId $vs0vPortGroupVlanID3 Get-VMHost $VMHost | Get-VirtualSwitch -Name $vs0vSwitchname | New-VirtualPortGroup -Name $vs0vPortGroupName4 -VLanId $vs0vPortGroupVlanID4 Get-VMHost $VMHost | Get-VirtualSwitch -Name $vs0vSwitchname | New-VirtualPortGroup -Name $vs0vPortGroupName5 -VLanId $vs0vPortGroupVlanID5 Get-VMHost $VMHost | Get-VirtualSwitch -Name $vs0vSwitchname | New-VirtualPortGroup -Name $vs0vPortGroupName6 -VLanId $vs0vPortGroupVlanID6 Get-VMHost $VMHost | Get-VirtualSwitch -Name $vs0vSwitchname | New-VirtualPortGroup -Name $vs0vPortGroupName7 -VLanId $vs0vPortGroupVlanID7 Get-VMhost $VMhost | Get-VirtualSwitch -Name $vs0vSwitchname | Get-NicTeamingPolicy | Set-NicTeamingPolicy -LoadBalancingPolicy ExplicitFailover -NetworkFailoverDetectionPolicy LinkStatus -NotifySwitches $true -FailbackEnabled $false #Create and configure vSwitch1. Write-Host "Create and configure vSwitch1" -foregroundcolor green Get-VMHost $VMHost | New-VirtualSwitch -Name $vs1vSwitchname -Nic $vs1vSwitchNIC1,$vs1vSwitchNIC2 Get-VMHost $VMHost | New-VMHostNetworkAdapter -VirtualSwitch $vs1vSwitchname -PortGroup $vs1vPortGroupName -IP $vs1vPortGroupIP -SubnetMask $vs1vPortGroupMask -VMotionEnabled:$true Get-VMHost $VMHost | Get-VirtualSwitch -Name $vs1vSwitchname | Get-VirtualPortGroup -Name $vs1vPortgroupName | Set-VirtualPortGroup -VLanId $vs1vPortGroupVlanID Get-VMhost $VMhost | Get-VirtualSwitch -Name $vs1vSwitchname | Get-NicTeamingPolicy | Set-NicTeamingPolicy -LoadBalancingPolicy ExplicitFailover -NetworkFailoverDetectionPolicy LinkStatus -NotifySwitches $true -FailbackEnabled $false #Create and configure vSwitch2 Write-Host "Create and configure vSwitch2" -foregroundcolor green Get-VMHost $VMHost | New-VirtualSwitch -Name $vs2vSwitchname -Nic $vs2vSwitchNIC1,$vs2vSwitchNIC2 Get-VMHost $VMHost | New-VMHostNetworkAdapter -VirtualSwitch $vs2vSwitchname -PortGroup $vs2vPortGroupName -IP $vs2vPortGroupIP -SubnetMask $vs2vPortGroupMask Get-VMhost $VMhost | Get-VirtualSwitch -Name $vs2vSwitchname | Get-NicTeamingPolicy | Set-NicTeamingPolicy -LoadBalancingPolicy ExplicitFailover -NetworkFailoverDetectionPolicy LinkStatus -NotifySwitches $true -FailbackEnabled $false } function Configure_datastores() { Write-Host "Config NFS advanced options" -foregroundcolor green # Configure NFS best practise values and maxvolume setting Set-AdvancedSetting -Name NFS.MaxVolumes -Value 256 -VMHost $VMHost Set-AdvancedSetting -Name NFS.TcpipHeapSize -Value 32 -VMHost $VMHost Set-AdvancedSetting -Name NFS.TcpipHeapMax -Value 128 -VMHost $VMHost Set-AdvancedSetting -Name NFS.HeartbeatMaxFailures -Value 10 -VMHost $VMHost Set-AdvancedSetting -Name NFS.HeartbeatFrequency -Value 12 -VMHost $VMHost Set-AdvancedSetting -Name NFS.HeartbeatTimeout -Value 5 -VMHost $VMHost #Create new NFS datastores Write-Host "Create new NFS Datastores" -foregroundcolor green $list = Import-Csv "nfs.csv" foreach($entry in $list) { Get-VMhost $VMhost | New-Datastore -Nfs -Name $entry.DatastoreName -Path $entry.NfsExport -NfsHost $entry.NfsHost } } #Set the ESX host to be configured. $VMHost = Read-Host -Prompt "Enter the name of the ESX host without domain" $VMHost = $VMHost + ".cloudfix.lab" #Get the IP Address of the host $IP_adr = Get-HostToIP($VMHost) #Get first 2 octets of the IP Address $IP_firstpart = $IP_adr.Substring(0,5) #Get last octet of the IP Address $IP_lastpart = $IP_adr.Substring(6,3) #Connect to vCenter. Get-Credential | connect-viserver -Server $vCenterServer Configure_network Configure_datastores
Click here to download the PowerCLI script.
I’m pretty sure this script can be enhanced on several points, but it does the job right now. 🙂
Any additions are easily implemented. I hope this PowerCLI script can help you in similar environment conditions!
Happy PowerCLI scripting!