In A Guide to Fumbling Through PowerCLI, we focused on how to get information on a vSphere environment through the PowerCLI command line interface. Now, since we’ve gotten an idea how to move around our environment with PowerCLI to find out information, let’s focus on configuring our environment with PowerCLI.
When we look at all of the switch commands we can use within PowerCLI by typing
Get-VICommand *switch, we will see some commands prefixed with
Before we can set a virtual switch, we first need to create it by using the New-VirtualSwitch command. This command will create a new Standard vSwitch for us. It is simple to find out the syntax for this command by using the get-help feature. The output of
Get-Help New-VirtualSwitch tells us exactly the information we need to create our new Standard vSwitch.
Luckily, we have all the information we need to
use New-VirtualSwitch to create a new vSwitch on our host, host1.lab.local. To create vSwitch3 using vmnics 3 and 4 and enabling jumbo frames we can type
New-VirtualSwitch -VMHost host1.lab.local -Name vSwitch3 -NumPorts 64 -nic vmnic4,vmnic5 -mtu 9000.
Once again, PowerCLI is nice enough to tell us we really don’t need to bother with the number of ports on vSphere 5.5 and later. The output tells us our switch has been created, and we can confirm it in vCenter.
Now, we need some port groups for our switch. We can use
Get-VICommand *portgroup to find out what we have available to us.
In this case, we’re going to want to use New-VirtualPortGroup since we are using a standard vSwitch. We’re going to create a new port group called Servers, on VLAN 33. In order to do this, think about the progression of the command in PowerCLI. We are setting a port group which is on a switch, which is on a host. We can use the command
Get-VMHost host1.lab.local | Get-VirtualSwitch -name vSwitch3 | New-VirtualPortGroup -Name Application33 -VLanID 33.
We can see it has been created by the PowerCLI output. Now, what if we wanted to create a VMkernel port for Fault Tolerance on our switch? In this case, we are going to need to use the
New-VMHostNetworkAdapter command, which has quite a number of attributes available (which we can see by issuing
After reading what we can do with the command, we can type
New-VMHostNetworkAdapter -VMhost host1.lab.local -PortGroup FaultTolerance -VirtualSwitch vSwitch3 -IP 192.168.66.66 -SubnetMask 255.255.255.0 -FaultToleranceLoggingEnabled $True to add our VMkernel port.
PowerCLI will tell us it has created vmk5 to our specifications after the command has finished running. If we check in vCenter, we will see our switch has the Application33 port group and FaultTolerance port group with vmk5 on it.
This is just one illustration of using PowerCLI to for a common task, configuring a standard virtual switch. There are many ways to issue and chain commands within PowerCLI, so you may end up achieving the same results using slightly different commands. Stay tuned for more examples on how to fumble your way through PowerCLI.
This article is part of the Fumbling Through PowerCLI Series.
A Guide to Fumbling Through PowerCLI
Continuing to Get Set With PowerCLI and Standard vSwitches
Get Set With PowerCLI and Distributed Virtual Switches, Part 1