Skip to Content

Continuing to Get Set With PowerCLI and Standard vSwitches

In A Guide to Fumbling Through PowerCLI, we focused on how to get information on a vSphere environment through the VMware vSphere PowerCLI command line interface.

Now, since we now have an idea on how to move around our environment with PowerCLI to find out information, us focus on configuring our environment with PowerCLI.  We are going to focus on creating a vSwitch in PowerCLI, specifically a standard vSwitch.

If you do not have vSphere Enterprise plus, the only switch type available to you in VMware vSphere is the Standard vSwitch.  In order for features like vMotion to work, you must have all the hosts in your vSphere cluster configured in a uniform manner, specifically when it comes to their networking.  

This is why PowerCLI is such an important tool when creating a vSwitch.  You can write a PowerCLI script to configure the standard vSwitches on the hosts in your vSphere cluster.  This will ensure all hosts have the same networking configuration.

PowerCLI vSwitch Standard Commands

When we look at all of the vSphere switch commands we can use within PowerCLI by typing Get-VICommand *switch, we will see some commands prefixed with New and Set.

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 the New-VirtualSwitch command 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.

vmware vsphere powercli new virtualswitch standard

Once again, PowerCLI is nice enough to tell us we really do not 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.

vmware vsphere powercli standard switch created

Creating Port Groups With PowerCLI

While creating a switch with the New-VirtualSwitch command is a great start, we are going to need more than that connect virtual machines to the vSwitch.  Now, we need to create some virtual port groups for our switch. We can use Get-VICommand *portgroup to find out what we have available to us.

vmware vsphere powercli get vicommand help

In this case, we are going to want to use the New-VirtualPortGroup PowerCLI command 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.

vmware vsphere powercli create new portgroup

We can see the port group has been created by the PowerCLI output.

Once you have created the port group, you can of course get port group information on it using the Get-VirtualPortGroup command for standard switches.

Creating VMkernel Ports With PowerCLI

VMkernel ports are used in VMware vSphere for a number of reasons which relate to communication of core vSphere functions like vMotion and network storage or NFS.  Again, it is very important to ensure VMkernel ports are configured correctly in your VMware vSphere environment if you are using standard vSwitches.

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 Get-Help New-VMHostNetworkAdapter).

vmware vsphere powercli get help

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.

vmware vsphere powercli create new 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.

vmware vsphere powercli vmkernel port created

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.  

PowerCLI is an easy and cost effective way to make sure your vSphere environment is uniformly configured.

Summary of vSphere Switch Standard PowerCLI Commands

To summarize, we have just worked with the following PowerCLI commands for standard vSwitches.

  • Creating a new standard vSwitch:
    New-VirtualSwitch -VMHost host1.lab.local -Name vSwitch3 -NumPorts 64 -nic vmnic4,vmnic5 -mtu 9000
  • Creating a new port group on a standard virtual switch:
    Get-VMHost host1.lab.local | Get-VirtualSwitch -name vSwitch3 | New-VirtualPortGroup -Name Application33 -VLanID 33.
  • Creating a new VMkernel port for VMware vSphere Fault Tolerance on a standard virtual switch:
    New-VMHostNetworkAdapter -VMhost host1.lab.local -PortGroup FaultTolerance -VirtualSwitch vSwitch3 -IP 192.168.66.66 -SubnetMask 255.255.255.0 -FaultToleranceLoggingEnabled $True

These commands can be adapted to meet your requirements to configure your vSphere networking environment.  They are a good starting point for a script to configure your vSphere standard switches with PowerCLI.

This article is part of the Fumbling Through PowerCLI Series.
Part 1 – A Guide to Fumbling Through PowerCLI
Part 2 – Continuing to Get Set With PowerCLI and Standard vSwitches

Part 3 – Get Set With PowerCLI and Distributed Virtual Switches, Part 1

One of the reasons I am focusing on vSphere networking to learn PowerCLI is that your vSphere host network configuration is so important to the smooth operation of your vSphere environment.  Stay tuned for more examples on how to fumble your way through PowerCLI.