Showing posts with label powercli. Show all posts
Showing posts with label powercli. Show all posts

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

    }
 
 

Wednesday, November 6, 2013

Create a custom ESXi ISO for NetApp NAS Plugin using PowerCLI and SAVE TIME !!

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


In my previous blog “vStorage APIs for Array Integration (VAAI) & NetApp – How to set it right?” I shared some pre-checks you should consider before using vStorage APIs and NetApp storage. I have highlighted about NFS VAAI Pluing that is required to be installed on the ESXi hosts if you want to leverage NFS VAAI and NetApp NFS Storage. This can be an additional implementation task and would require significant efforts if you are you are a cloud service provider or have a large infrastructure with 100s of ESXi hosts.
To address this you can create custom ESXi ISO with the NFS VAAI Plugin. In this blog I will discuss how you can create a custom ESXi ISO in easy steps using VMware PowerCLI. NOTE: There are few GUI based products which allows you to do the same task however their installation may not be permitted in organizations with strict compliance and policies. Hence I chose to use VMware PowerCLI to do this.
To create the custom ISO you need to download the following components:
  • VMware PowerCLI
  • VMware Offline Bundle: Download the offline bundle from vmware for the version of ESXi that you want to install. In the screenshot below I am downloading the offline bundle for vSphere 5.1 however in the example I am using ESXi 5.1 Update 1.
     




Once VMware PowerCLI is installed, launch PowerCLI (I am not covering about setting execution policy while using PowerCLI in this blog). In this scenario I am creating a custom ESXi 5.1 Update 1 ISO with NetApp NASPlugin version 18.
  1. Add the offline bundle to the software depot
PowerCLI C:> Add-EsxSoftwareDepot -DepotUrl C:\ESXi\update-from-esxi5.1-5.1_update01.zip
Depot Url
---------
zip:C:\ESXi\update-from-esxi5.1-5.1_update01.zip?index.xml


  1. List the different image profiles that are now available.
PowerCLI C:> Get-EsxImageProfile | Format-Table -AutoSize
Name                             Vendor       Last Modified        Acceptance Level
----                             ------       -------------        ------------
ESXi-5.1.0-20130402001-no-tools  VMware, Inc. 3/23/2013 9:30:37 PM PartnerSu...
ESXi-5.1.0-20130401001s-no-tools VMware, Inc. 3/23/2013 9:30:37 PM PartnerSu...
ESXi-5.1.0-20130402001-standard  VMware, Inc. 3/23/2013 9:30:37 PM PartnerSu...
ESXi-5.1.0-20130401001s-standard VMware, Inc. 3/23/2013 9:30:37 PM PartnerSu...


  1. Use one of the existing image profiles to create a clone of a new image profile that we will use to customize with the NetApp NASPlugin.
PowerCLI C:> New-EsxImageProfile -CloneProfile ESXi-5.1.0-20130402001-standard -Name ESXi-NFSVAAI -Vendor NetApp
Name                           Vendor          Last Modified   Acceptance Level
----                           ------          -------------   ----------------
ESXi-NFSVAAI                   NetApp          3/23/2013 9:... PartnerSupported


  1. Add the NetApp NAS Plugin to the software depot
PowerCLI C:> Add-EsxSoftwareDepot -DepotUrl C:\ESXi\NetAppNasPlugin.v18.zip
Depot Url
---------
zip:C:\ESXi\NetAppNasPlugin.v18.zip?index.xml


  1. List the NetApp NAS Plugin
PowerCLI C:> Get-EsxSoftwarePackage | Where-Object {$_.name -match "Netapp"}
Name                     Version                        Vendor     Creation Date
----                     -------                        ------     ------------
NetAppNasPlugin          1.0-018                        NetApp     4/29/2012...


  1. Add the NetApp NAS Plugin to the cloned Image profile
PowerCLI C:> Add-EsxSoftwarePackage -ImageProfile ESXi-NFSVAAI -SoftwarePackage NetAppNasPlugin
Name                           Vendor          Last Modified   Acceptance Level
----                           ------          -------------   ----------------
ESXi-NFSVAAI                   NetApp          9/13/2013 2:... PartnerSupported


  1. Export the Image profile as an ISO image
PowerCLI C:> Export-EsxImageProfile -ImageProfile ESXi-NFSVAAI -ExportToISO -filepath C:\ISO\ESXi5.1-NFSVAAIv18.iso

I personally felt that it’s easy to use PowerCLI and will continue to use PowerCLI in future, leave a comment if you have a different opinion.