HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
Array Setup and Networking
cancel
Showing results for 
Search instead for 
Did you mean: 

quick powershell script for adding esxi hosts

 
jgudnason45
Occasional Visitor

quick powershell script for adding esxi hosts

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  #############################################################

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

2 REPLIES
jcates98
Trusted Contributor

Re: quick powershell script for adding esxi hosts

Hi Jared, nice work! Thanks for sharing it with the community.  I love seeing the Nimble Powershell Toolkit put to good use.

One comment I did want to make was about the binding of your VMkernel ports to the iSCSI software adapter. It looks like you are setting up iSCSI connections on two separate subnets. In this configuration, VMware states that you should not be binding the VMkernel ports to the software iSCSI adapter.  Check out VMware KB 2038869 for more detail.

Cheers!

Julian

jgudnason45
Occasional Visitor

Re: quick powershell script for adding esxi hosts

well learn something new everyday.

I wasn't aware of the binding discussion, i've just always bound ports, didn't even realize the software initiator would work without having bound ports..  (although i do have more dead paths with unbound, but i dont think that's actually an issue)

Thanks.