Friday, December 20, 2013

Automate vSphere Networking (Create VMkernel & VM Portgroups) using PowerCLI




Welcome: To stay updated with all my Blog posts follow me on Twitter @arunpande !

This blog post provides a simple and easy to use PowerCLI script which allows vSphere administrators & consultants to perform the following tasks AUTOMATICALLY !


  • Add multiple ESXi hosts in a cluster
  • Create vSphere Standard Switch
  • Assign Physical Network cards
  • Create Virtual Machine portgroups and assign VLAN
  • Create VMkernel Port groups
  • Assign IP address, Netmask & Gateway to the VMkernel ports

I have provided comments before each script to help you understand the script better. Please leave a comment if you have any questions.

#Create an Empty array which will save all the values in $esxi variable, 
#you can input values for #ESXi host IP/FQDN using multiple. 
#In this example I have used Read-Host option.

$esxi = @()

#Enter the IP/FQDN of the ESXi servers and this script will 
#continue to prompt you to add hosts untill you stop entering them.

do {
  $input =  Read-Host "Enter the name or IP of the ESXi server"
  if ($input -ne ""){
      $esxi += $input
  }
 }
until ($input -eq "")

#The ESXi hosts that you add in the above step are saved in an array. 
#The below commandlet lets you addd these ESXi host to the cluster "DR-Stie". 
#The credentials have been pre-entered.

foreach ($name in $esxi)
{
add-vmhost -Name $name -location DR-Site -user root -password XXXXXX -force: $true
}

#The below cmdlet allows you to define the last octet for VMkernel interfaces 
#that you want to configure. 
#For example I have used 101 as the last octet for VMkernel interfaces used for 
#vMotion & NFS. Though they would belong to different Subnets & VLANs.

    $vMotionip = 101
    $nfsip = 101

#I am using a for-each loop to perform the below tasks on all the ESXi hosts 
#reported using the get-vmhost cmdlet. 
#Here we are querying all the ESXi host managed by vCenter. 
#You can modify this to query only specific ESXi hosts in cluster.

    foreach ($vmhost in (get-vmhost))
    {

#Here I am defining a variable $vmnic with value of vmnic1. 
#You can use multiple vmnics by separating them using ","

    $vmnic = vmnic1

#Here is am defining a variable $vs for vSiwtch2 while creating the vSwitch.
    
    $vs =  New-VirtualSwitch -VMHost $vmHost -Name vSwitch2

#Here I am creating a new virtual machine portgroup named "VLAN-600-Std-Switch" 
#and assigning it VLAN 600. 
#The output of this cmdlet is saved in $vlan

    $vlan600 =  New-VirtualPortGroup -VirtualSwitch $vs -Name VLAN-600-Std-Switch -VLanId 600

# I have created a new virtual machine portgroup named "Infra".

    New-VirtualPortGroup -VirtualSwitch $vs -Name Infra -Vlanid 0

# I have created a new virtual machine portgroup named "Infra".

    $vmotion = New-VirtualPortGroup -VirtualSwitch $vs -Name vMotion -VLanId 500

# I have created a new virtual machine portgroup named "NFS" and assigned it VLAN 400. 
#The output of this cmdlet is saved in $nfs

    $nfs = New-VirtualPortGroup -VirtualSwitch $vs -Name NFS -VLanId 400

# We are now creating a VMkernel network interface that will be used for vMotion. 
#I am using the vMotionip+=1 which will increment the last octet of the IP by one.

    New-VMHostNetworkAdapter -VMHost $vmhost -PortGroup $vmotion 
    -VirtualSwitch $vs -IP 192.168.50.$vMotionip -SubnetMask 255.255.255.0 
    -VMotionEnabled: $true

    $vMotionip+=1

# We are now creating a VMkernel network interface that will be used for NFS. 
#I am using the nfsip+=1 which will increment the last octet of the IP by one.

    New-VMHostNetworkAdapter -VMHost $vmhost -PortGroup $nfs 
    -VirtualSwitch $vs -IP 192.168.40.$nfsip -SubnetMask 255.255.255.0 
    -VMotionEnabled: $false

    $nfsip+=1

    }