VMware PowerCLI

Automate vSwitch and VMkernel configuration VMware PowerCLI

As we have already detailed in this post about how we can configure VMware clusters with PowerCLI and the benefits of doing so, the sky is the limit in what we can do with VMware PowerCLI and automating configuration processes on our ESXi hosts including our network configuration.  One of the most tedious and labor intensive configuration processes especially with the Web client is configuring vSwitches and VMkernel ports especially if using Standard vSwitches.  In this quick Friday post, I wanted to paste over some PowerCLI code to configure a couple of vSwitches – one for iSCSI and one for vMotion.  Today’s topic – Automate vSwitch and VMkernel configuration VMware PowerCLI.

Automate vSwitch and VMkernel configuration VMware PowerCLI

The first thing we need to do is setup a CSV file that will hold all the values that we will be passing into our PowerCLI script.  Below is the example CSV file that you can use as a template.  You will of course need to change the iSCSI and vMotion IP addresses and subnets defined along with which vmnics you want to assign to which vSwitch.

HostIP,vSwitch1,vSwitch2,iscsia,iscsib,iscsiIP1,iscsiIP2,iscsisubnet,vmotiona,vmotionb,vmotionIP1,vmotionIP2,vmosubnet,iscsinic1,iscsinic2,vmonic1,vmonic2
10.1.1.50,vSwitch1,vSwitch2,iSCSI1,iSCSI2,10.0.9.30,10.0.9.31,255.255.255.0,vMotion1,vMotion2,172.16.0.30,172.16.0.31,255.255.255.0,vmnic2,vmnic3,vmnic4,vmnic5
10.1.1.51,vSwitch1,vSwitch2,iSCSI1,iSCSI2,10.0.9.40,10.0.9.41,255.255.255.0,vMotion1,vMotion2,172.16.0.40,172.16.0.41,255.255.255.0,vmnic2,vmnic3,vmnic4,vmnic5

Now for the PowerCLI code that will utilize the values we have defined in our CSV file.  You will need to populate the path to your CSV file that you want to use.  Also, there are no checks in the script below to verify connection to a vCenter server, so you will want to make sure to use the connect-viserver commandlet and connect to vCenter beforehand.  The values are defined based on the CSV import.  Also, there are checks in place in the script to check and see if the vSwitches as well as the VMkernel ports already exist.  If they do, the script will not attempt to create any of the vSwitches or the VMkernel ports.

The Code

########################################
#                                      #
#   Lab Cluster VSS Switch Config      #
#     ESXi VSS Switch creation         #
#                                      # 
########################################

$Servers = Import-CSV "C:\<path to your CSV file>\hostnet.csv"

ForEach ($Server in $Servers) {

	$VMhost = $Server.HostIP
	$VMSwitch1 = $Server.vSwitch1
    $VMSwitch2 = $Server.vSwitch2
    $iscsinic1 = $Server.iscsinic1
    $iscsinic2 = $Server.iscsinic2
    $vmotionnic1 = $Server.vmonic1
    $vmotionnic2 = $Server.vmonic2
    $iscsi1 = $Server.iscsia
    $iscsi2 = $Server.iscsib
    $vmotion1 = $Server.vmotiona
    $vmotion2 = $Server.vmotionb
    $iscsiIP1 = $Server.iscsiIP1
    $iscsiIP2 = $Server.iscsiIP2
    $iscsisubnet = $Server.iscsisubnet
    $vmotionIP1 = $Server.vmotionip1
    $vmotionIP2 = $Server.vmotionip2
    $vmosubnet = $Server.vmosubnet
	

    get-virtualswitch -VMHost $VMhost -name vswitch0 | set-virtualswitch -nic vmnic0,vmnic1 -MTU 9000 -confirm:$false

    #Creating vSwitch1 for iSCSI

    if ((get-virtualswitch -VMHost $VMhost -name $VMSwitch1 -ErrorAction SilentlyContinue) -eq $null) {

        Write-host "Creating VSS Switch $VMSwitch1"
        new-virtualswitch -host $VMhost -name $VMSwitch1 | set-virtualswitch -nic $iscsinic1,$iscsinic2 -MTU 9000 -confirm:$false

    } else {

        Write-host "VSS Switch $VMSwitch1 already exists"

    }

    #Creating vSwitch2 for vMotion

    if ((get-virtualswitch -VMHost $VMhost -name $VMSwitch2 -ErrorAction SilentlyContinue) -eq $null) {

        Write-host "Creating $vSwitch1"
        new-virtualswitch -host $VMhost -name $VMSwitch2 | set-virtualswitch -nic $vmotionnic1,$vmotionnic2 -MTU 9000 -confirm:$false

    } else {

        Write-host "VSS Switch $vSwitch1 already exists"
    }


    #Creating iSCSI1 VMkernel Ports

    if ((Get-VirtualPortGroup -VMHost $vmhost -Name $iscsi1 -ErrorAction SilentlyContinue) -eq $null) {

        Write-host "Creating VMkernel port $iscsi1"    
        new-vmhostnetworkadapter -vmhost $VMhost -PortGroup $iscsi1 -VirtualSwitch $VMSwitch1 -IP $iscsiIP1 -SubnetMask $iscsisubnet -MTU 9000
        Get-VirtualPortGroup -VMHost $vmhost -Name $iscsi1 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $iscsinic1 -MakeNicUnused $iscsinic2 -FailbackEnabled:$false

    } else {

        Write-host "VMkernel port $iscsi1 already exists"

    }


    #Creating iSCSI2 VMkernel Ports

    if ((Get-VirtualPortGroup -VMHost $vmhost -Name $iscsi2 -ErrorAction SilentlyContinue) -eq $null) {

        Write-host "Creating VMkernel port $iscsi2"
        new-vmhostnetworkadapter -vmhost $VMhost -PortGroup $iscsi2 -VirtualSwitch $VMSwitch1 -IP $iscsiIP2 -SubnetMask $iscsisubnet -MTU 9000
        Get-VirtualPortGroup -VMHost $vmhost -Name $iscsi2 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $iscsinic2 -MakeNicUnused $iscsinic1 -FailbackEnabled:$false

    } else {

        Write-host "VMkernel port $iscsi2 already exists"

    }


    #Creating vMotion1 VMkernel Ports

    if ((Get-VirtualPortGroup -VMHost $vmhost -Name $vmotion1 -ErrorAction SilentlyContinue) -eq $null) {

        Write-host "Creating VMkernel port $vmotion1"
        new-vmhostnetworkadapter -vmhost $VMhost -PortGroup $vmotion1 -VirtualSwitch $VMSwitch2 -IP $vmotionip1 -SubnetMask $vmosubnet -MTU 9000 -VMotionEnabled:$true
        Get-VirtualPortGroup -VMHost $vmhost -Name $vmotion1 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $vmotionnic1 -MakeNicStandby $vmotionnic2

    } else {

        Write-host "VMkernel port $vmotion1 already exists"

    }


    #Creating vMotion2 VMkernel Ports

    if ((Get-VirtualPortGroup -VMHost $vmhost -Name $vmotion2 -ErrorAction SilentlyContinue) -eq $null) {
    
        Write-host "Creating VMkernel port $vmotion2"
        new-vmhostnetworkadapter -vmhost $VMhost -PortGroup $vmotion2 -VirtualSwitch $VMSwitch2 -IP $vmotionip2 -SubnetMask $vmosubnet -MTU 9000 -VMotionEnabled:$true
        Get-VirtualPortGroup -VMHost $vmhost -Name $vmotion2 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $vmotionnic2 -MakeNicStandby $vmotionnic1
  
	} else {
        
        Write-host "VMkernel port $vmotion2 already exists"

    }

}

The process will complete in under a minute on a couple of hosts and makes it extremely painless compared to the Web client to get your VSS switches configured and VMkernel ports created and configured with IP addresses, MTU values, and appropriate vmnics defined.

Thoughts

The more you use PowerCLI to complete labor intensive tasks such as creating VSS switches and port groups, the more you will grow to love it and use it.  This quick script to Automate vSwitch and VMkernel configuration VMware PowerCLI process shows just how easy it is to create a CSV file and then use the input to configure our switches and VMkernel ports.

Subscribe to VirtualizationHowto via Email 🔔

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com and has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

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