2 Replies Latest reply: Oct 3, 2016 9:19 AM by Jared Gudnason RSS

    quick powershell script for adding esxi hosts

    Jared Gudnason Newbie

      Well I wont say this is the most polished script, and you will need to modify a few of the specific variables to your environment, but it's quick, dirty, and works.

       

      in my environment, i only have a single initiator group for all hosts.  Wrote this script to add additional esxi hosts via iscsi to the nimble.

      need vmware powerCLI installed, and the nimble power shell toolkit as well.

       

      script;

      gets vmware hosts.

      creates a standard vswitch with specified vmnics  (set vmnics specific to your environment)

      creates two vmkernel adapters. bound to each vmnic (active / disabled)

      enables software iscsi initiator

      bounds vmkernal adapters to the iscsi initiator

      registeres the iqn on the nimble initiator group

      adds the target ip's to the iscsi initiator

      rescans

       

       

      # ############################################################################################

      #

      #

      #   Network config for Standard Edition Hosts - Control Cluster.

      #

      #       Also can be modified to add iscsi to existing hosts.

      #

      #

      # ############################################################################################

      # #########  BEGIN HERE  #####################################################################

      # ############################################################################################

      import-module NimblePowerShellToolKit

      Connect-VIServer yourVcenterServer

       

       

      #  VARIABLES

      $vmhost = get-vmhost | where-object { $_.Name -like "filter*"}

       

       

      $targets = "1.2.3.4","1.2.4.4"    #iScsi targets to add to hosts (if not already there)

      $ip1 = "10.1.1."    # base IP address for iScsi vlan 1

      $ip2 = "10.1.2."    # base IP address for iScsi vlan 2

       

      #Array of last octect IP addresses for corresponding host names

      $ipHost = @{"host1" = "101";

                  "host2" = "102";

                  "host3" = "103"

                }

       

      $nimbleCreds = Get-Credential -Message "Enter Credentials for Nimble Array" -UserName "admin"

      Connect-NSGroup -Group 1.2.3.4 -credential $nimbleCreds

      # grab initiatorGroup from Nimble.

      $nimbIGroup = Get-NSInitiatorGroup -name initiatorGroup

       

       

      # ############################################################################################

       

       

      foreach ($esx in $vmhost) {

          $oct = $IPHost.get_Item($esx.Name.Split(".")[0])

       

       

      #vmStorage Switch - this can be a standard switch for all hosts.

       

       

          #Create/Get Virtual Switch

          if (-not($vSwitch = Get-VirtualSwitch -VMHost $esx -Name vmStorage 2>null))

              { $vSwitch = New-VirtualSwitch -VMHost $esx -Name "vmStorage" -Nic vmnic4,vmnic5 }

          #$vSwitch = Get-VirtualSwitch -VMHost $esx -Name vmStorage

       

       

          #1 Portgroup and nic policy

          if (-not ( $pg = Get-VirtualPortGroup -VirtualSwitch $vSwitch  -Name "iSCSI 1" 2>null ))

                   { $pg = New-VirtualPortGroup -VirtualSwitch $vSwitch  -Name "iSCSI 1" -VLanID 1 }

       

       

              $pg | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive vmnic4 -MakeNicUnused vmnic5

              New-VMHostNetworkAdapter -VMHost $esx -PortGroup $pg -virtualSwitch $vSwitch -IP "$ip1$oct" -SubnetMask 255.255.255.0

       

       

              #store the vmkx name for this adapter

              $vmk0 = $(Get-VMHostNetworkAdapter -VMHost $esx -PortGroup $pg -virtualSwitch $vSwitch ).Name

       

       

          #2 Portgroup and nic policy

          if (-not ( $pg = Get-VirtualPortGroup -VirtualSwitch $vSwitch  -Name "iSCSI 2" 2>null ))

                   { $pg = New-VirtualPortGroup -VirtualSwitch $vSwitch  -Name "iSCSI 2" -VLanID 2 }

       

       

              $pg | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive vmnic5 -MakeNicUnused vmnic4

              New-VMHostNetworkAdapter -VMHost $esx -PortGroup $pg -virtualSwitch $vSwitch -IP "$ip2$oct" -SubnetMask 255.255.255.0

       

       

              #store the vmkx name for this adapter

              $vmk1 = $(Get-VMHostNetworkAdapter -VMHost $esx -PortGroup $pg -virtualSwitch $vSwitch ).Name

       

       

        #iSCSI setup

       

       

          #Create Software iSCSI Adapter

          get-vmhoststorage $esx | set-vmhoststorage -softwareiscsienabled $True

       

       

          #Get Software iSCSI adapter HBA number and put it into an array

          $HBA = Get-VMHostHba -VMHost $esx -Type iScsi | Where-Object {$_.Model -eq "iSCSI Software Adapter"}

       

       

          #add the vmkernel adapters to the software iscsi adapter.

          $Esxcli = Get-EsxCli -VMHost $esx

          $Esxcli.iscsi.networkportal.add($HBA, $Null, $vmk0)

          $Esxcli.iscsi.networkportal.add($HBA, $Null, $vmk1)

       

       

         #add the iniator to the Nimble initiator

         if (-not(Get-NSInitiator | where-object { $_.iqn -eq $HBA.IScsiName } ))

           #create the initiator if it doesn't exist

           { New-NSInitiator -initiator_group_id $nimbIGroup.id -access_protocol iscsi -label $esx.name -iqn $HBA.IScsiName }

       

       

       

       

          # add the targets to the software iscsi initiator

          foreach ($target in $targets){

              if (Get-IScsiHbaTarget -IScsiHba $HBA -Type Send | Where {$_.Address -cmatch $target}) {

              Write-Host "The target $target does exist on $esx" -ForegroundColor Green

              }     else {

                Write-Host "The target $target doesn't exist on $esx" -ForegroundColor Red

       

       

                Write-Host "Creating $target on $esx ..." -ForegroundColor Yellow

       

       

                New-IScsiHbaTarget -IScsiHba $hba -Address $target    

              }

          }

          $esx | Get-VMHostStorage -RescanAllHba

       

       

       

       

      }

       

       

      # ############################################################################################

      # ##############  END CODEBLOCK  #############################################################

      # ############################################################################################