I think this is my post about Distributed Switch in my “Back to Basics” series. At the back of mind I know I’ve yet to cover “Port Mirroring” as feature, and I will someday double back to that. Right now, I think I have an issue with my physical switch which is preventing me from doing the work on that properly. Usually, at the end of series of post I wrap up with an overview on how carry out similar admin tasks from PowerCLI – and that’s what this post is all about.
Creating an Distributed Switch
The PowerCLI cmdlet New-VDSwitch can create a Distributed Switch in this case the command creates the switch but also enables LLDP in both directions, together with an MTU of 9000 with support for two Distributed Uplinks. Distributed Switches created without the -version parameter will automatically use the most recent iteration of the Distributed Switch.
New-VDSwitch -Name “DSwitch0-GoldCluster01” -Location “New York” -LinkDiscoveryProtocol “LLDP” -LinkDiscoveryProtocolOperation “Both” -MTU 9000 -NumUplinkPorts 2
Adding Distributed Portgroups to Distributed Switch
The PowerCLI cmdlet New-VDPortgroup can be used together a range and foreach loop to create a series of portgroups dVLAN101 to dVLAN105, each with the VLAN Tag set for 101 to 105 on each portgroup created
101..105 | Foreach {
$Num = $_
New-VDPortgroup -VDSwitch (Get-VDSwitch -Name “DSwitch0-GoldCluster01”) -Name dVLAN$num -VLanId $num
}
Adding VMware ESXi Hosts to the Distributed Switch
The PowerCLI cmdlet Add-VDSwitchVMHost can be used to add VMware ESXi hosts to the Distributed Switch. Using a combination of range and foreach loop we can add hosts esx01nyc.corp.com, esx02nyc.corp.com and esx03nyc.corp.com to the Distributed Switch DSwitch0-GoldCluster01.
1..3 | Foreach {
$Num = “{0:00}” -f $_
Add-VDSwitchVMHost -VDSwitch (Get-VDSwitch -Name “DSwitch0-GoldCluster01”) -VMHost esx”$Num”nyc.corp.com
}
Adding Physical vmnics to Distributed Switch Uplinks
The PowerCLI command Add-VDSwitchPhysicalNetworkAdapter can be used to assign physical vmnic to the UpLinks container. Again a combination of ranges and foreach loops finds each ESXi host from esx01nyc to esx03nyc, retrieving the ID of the vmnic. Each vmnic added consumes the 1st available Uplink container – in this case vmnic2 is added to Uplink1 and vmnic3 is added Uplink2.
1..3 | Foreach {
$Num = “{0:00}” -f
$_ $vmhostNetworkAdapter = Get-VMHost esx”$Num”nyc.corp.com | Get-VMHostNetworkAdapter -Physical -Name vmnic2
Get-VDSwitch “DSwitch0-GoldCluster01” | Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $vmhostNetworkAdapter -Confirm:$False
}
1..3 | Foreach { $Num = “{0:00}” -f
$_ $vmhostNetworkAdapter = Get-VMHost esx”$Num”nyc.corp.com | Get-VMHostNetworkAdapter -Physical -Name vmnic3
Get-VDSwitch “DSwitch0-GoldCluster01” | Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $vmhostNetworkAdapter -Confirm:$False
}