Powercli – Virtual Switches

Q: How do I add a new port group to a virtual switch on each ESX host

A: Using powercli you could run the following:

[sourcecode language="powershell"]
$cluster_name = "Your_Cluster_Name"
$vSwitch = "vSwitch_you_want_to_add_vlan_to"
$PortGroupName = "Name_of_Your_Port_Group"
$PortGroupVLAN = "VLAN_of_Your_Port_Group"

foreach ($esx in get-VMhost -Location $cluster_name | sort Name) { $esx | Get-VirtualSwitch -Name $vSwitch | New-VirtualPortGroup -Name “$PortGroupName″ -VlanId $PortGroupVLAN }

[/sourcecode]

Q: How do I change the number of ports on my vSwitch using PowerCli

A: With the following commands:

[sourcecode language="powershell"]
$vSwitch = "Your_vSwitch_Name"
$NumberPorts = "Number_of_ports"

Get-VMHost | % {Get-VirtualSwitch -VMHost $_ -Name $vSwitch | % { Set-VirtualSwitch -VirtualSwitch $_ -NumPorts "$NumberPorts" }

[/sourcecode]

Q: How do I create a vSwitch from PowerCli
A: With this code:

[sourcecode language="powershell"]
# You can do multiple vmnic's on the command just use a , between
$vmnic = "Name_of_vmnic_you_want_assigned_to_switch"
$vSwitch = "Name_of_new_vSwitch"
$NumPorts = "Number_of_ports"

New-VirtualSwitch -Name $vSwitch -NumPorts $NumPorts -Nic $vmnic

[/sourcecode]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.