Tuesday, September 25, 2012

PowerCLI: configuring vSwitches on vSphere5

Having just installed more than 30 vSphere 5 ESXi, I decided that I was not going to configure their vSwitches one by one, because I didn't like the idea that I could make a mistake in the configuration by entering all those settings manually.
Consumed by laziness, I immediately went on the Net to find the script that I was looking for and found this one: PowerCLI: Easy vSwitch & PortGroup Setup.

The aim of this script by Virtu-Al.net is to copy network settings from a source host to another host, which is more or less what I need if I first configure a host as a master from which to clone networking settings. The problem with this script is that it doesn't correspond to what I need for three reasons:
  • the first one is that it does not assign vmnics to the vSwitches,
  • the second one is that it does not activate vMotion on the Management Interface even though this setting is activated on the source host,
  • the third one is that it does not recursively clone settings in a one-to-many fashion.

Needless to say, I started jotting down my own script. It is structured in a way that for 32 hosts (using the Powershell Range construct x..y, which is very well explained here) I repeat the same task of:
  • enabling vMotion on the Management Network VMkernel port on vSwitch0
  • adding a secondary active NIC interface to vSwitch0, which will in the end rely on vmnic0 and vmnic2
  • creating a second vSwitch named vSwitch1 which relies on vmnic1 and vmnic3
  • creating three port groups with different VLAN IDs on vSwitch1
Connect-VIServer "your_vCenter" | Out-Null
1..32 | % {
$destination_host = Get-VMHost -Name "host$_.yourdomain.com"
$vswitch0 = "vSwitch0"
Write-Host "Configuring $vswitch0 Management Interface for vMotion and adding a second active NIC" -ForegroundColor blue
Write-Host "vSphere 5 installation already sets up vSwitch0 with a VM network and a Management Interface on the first NIC" -ForegroundColor gray
$destination_host |Get-VMHostNetworkAdapter |?{ $_.PortGroupName -match "Management Network"} | Set-VMHostNetworkAdapter  -VMotionEnabled:$true -Confirm:$false | Out-Null
$destination_host | Get-VirtualSwitch -Name $vswitch0 | Set-VirtualSwitch -Nic "vmnic0","vmnic2" -Confirm:$false | Out-Null
$vswitch1 = "vSwitch1"
Write-Host "Configuring $vswitch1 with two active adapters and adding three port groups with different VLANID" -ForegroundColor blue
$destination_host | New-VirtualSwitch -name $vswitch1 -Confirm:$false -nic "vmnic1","vmnic3" | Out-Null
$destination_host | Get-VirtualSwitch -Name $vswitch1 | New-VirtualPortGroup -Name "production_vlan" -Confirm:$false | Out-Null
$destination_host | Get-VirtualSwitch -Name $vswitch1 | New-VirtualPortGroup -Name "backup_vlan" -VLanId 8 -Confirm:$false | Out-Null
$destination_host | Get-VirtualSwitch -Name $vswitch1 | New-VirtualPortGroup -Name "production2_vlan" -VLanId 18 -Confirm:$false | Out-Null
Write-Host "Done configuring vSwitches on $destination_host"-ForegroundColor blue

As you can see, nothing very difficult here. I sue simple PowerCLI cmdlets which are very easy to understand. Just replace names and VLAN IDs to suit your needs and there you are. I hope this post helps someone. If you found it useful do not hesitate to comment, tweet or google+!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...