4 Replies Latest reply: Jan 31, 2016 8:46 PM by Matt Allford RSS

    PowerCLI to setup hosts networking and storage ready for iSCSI LUNS

    Dean Lewis Wayfarer

      Hey,

       

      I am by no means a scripting person, but the other day I had quite a few greenfield hosts to deploy, so I decided to use powercli to make life easier, and do the job quicker. Admittedly it took longer for me to actually find all the commands, but still, they can be used in the future.

       

      Where in red you need to edit to fit your environment

       

      #Setup which host to target

      $VMhost = 'hostname'

       

      #create vSwitch2 for storage, add vmnics, add two vmkernels with Storage IPs, setup NIC teaming (based on the fact you probably have vSwitch0 for mgmt and vSwitch1 for VM traffic)

      $vs2 = get-vmhost $VMhost | new-virtualswitch -Name vSwitch2 -Nic 'vmnic2','vmnic5' -Mtu 9000 -NumPorts 120

      New-VMHostNetworkAdapter -VMhost $VMhost -virtualswitch $vs2 -portgroup iSCSI_ESX_01 -ip IP_ADDR -subnetmask SUBNET_MASK -Mtu 9000

      New-VMHostNetworkAdapter -VMhost $VMhost -virtualswitch $vs2 -portgroup iSCSI_ESX_02 -ip IP_ADDR -subnetmask SUBNET_MASK -Mtu 9000

      Get-VirtualPortGroup -VMhost $host -virtualswitch $vs2 -Name iSCSI_ESX_01 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive vmnic2 -MakeNicUnused vmnic5

      Get-VirtualPortGroup -VMhost $host -virtualswitch $vs2 -Name iSCSI_ESX_02 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive vmnic5 -MakeNicUnused vmnic2

       

      #Create Software iSCSI Adapter

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

       

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

      $HBA = Get-VMHostHba -VMHost $VMHost -Type iSCSI | %{$_.Device}

       

      #Set your VMKernel numbers, Use ESXCLI to create the iSCSI Port binding in the iSCSI Software Adapter,

      $vmk1number = 'vmk1'

      $vmk2number = 'vmk2'

      $esxcli = Get-EsxCli -VMhost $VMhost

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

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

       

      #Setup the Discovery iSCSI IP addresses on the iSCSI Software Adapter

      $hbahost = get-vmhost $VMhost | get-vmhosthba -type iscsi

      new-iscsihbatarget -iscsihba $hbahost -address IP_ADDR

       

      #Rescan the HBA to discover any storage

      get-vmhoststorage $VMhost -rescanallhba -rescanvmfs

       

      If anyone wants to improve the above, reach out, I put this together in an afternoon/evening during the install I was on. So it was rushed and best effort.


      This will also appear on my blog as well www.educationalcentre.co.uk.

        • Re: PowerCLI to setup hosts networking and storage ready for iSCSI LUNS
          Matt Allford Newbie

          G'day Dean,

           

          I wrote a similar script a few months back, so I'll put a couple of comments below as to where mine differed

           

          I think you get the iSCSI HBA settings twice, which probably isn't needed

          $HBA = Get-VMHostHba -VMHost $VMHost -Type iSCSI | %{$_.Device}


          $hbahost = get-vmhost $VMhost | get-vmhosthba -type iscsi

           

          With this particular line, the server could have additional hardware iSCSI HBAs, so I there in a where-object to make sure it was the software adapter:

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

           

          I can't recall if Nimble Connection Manager (NCM) automatically adjusts the timeout values on the iSCSI software Adapter. In our environment (long story), some of our hosts connecting to Nimble Storage are using hardware adapter and some are using software adapter. Nimble best practice is to set the iSCSI Login Timeout, Noop Timout and Noop Interval to 30 seconds each, which can be done with the following as you have already imported esxcli:

           

          $esxcli.iscsi.adapter.param.set($hbahost.device,$false,'LoginTimeout','30')

          $esxcli.iscsi.adapter.param.set($hbahost.device,$false,'NoopOutTimeout','30')

          $esxcli.iscsi.adapter.param.set($hbahost.device,$false,'NoopOutInterval','30')

           

           

          As I was making this change to production servers as we were migrating from an old SAN to the Nimble storage, I also decided to place the host into maintenance mode before making these changes (just in case). Also I believe that the changes to the timeouts above require a reboot to apply, so I incorporated a host reboot into the script as well.

           

          The other thing I did, was instead of having to modify the values below, I created a CSV file where I could put the values, and then just import the CSV at the start of the script and run a foreach loop to modify each host.

           

          I've taken your script and added my suggestions above, as well as placed it in a foreach.

           

          The CSV would look something like this, obviously with extra rows with specific info per host

            

          hostnamePortgroup1Portgroup2Uplink1Uplink2IP1IP2VMK1NumberVMK2NumberiSCSITarget
          Server1iSCSI-Nimble1iSCSI-Nimble2vmnic6vmnic7172.17.248.18172.17.248.19vmk1vmk2172.17.248.249

           

          And the script is below. I haven't actually tested it but I've read over it and I think I have all of the variables lined up correctly

           

          $HostData = import-csv C:\Scripts\Nimble_vSwitch.csv

           

           

          foreach ($Host in $HostData){

          #Place Host in maintenance mode

          Set-VMHost -VMHost $Host.hostname -State Maintenance

           

          #create vSwitch2 for storage, add vmnics, add two vmkernels with Storage IPs, setup NIC teaming (based on the fact you probably have vSwitch0 for mgmt and vSwitch1 for VM traffic)

          $vs2 = New-VirtualSwitch -VMHost $Host.hostname -Name vSwitch2 -Nic $Host.Uplink1,$Host.Uplink2 -Mtu 9000 -NumPorts 120

          New-VMHostNetworkAdapter -VMhost $Host.hostname -virtualswitch $vs2 -portgroup $host.Portgroup1 -ip $Host.IP1 -subnetmask 255.255.255.0 -Mtu 9000

          New-VMHostNetworkAdapter -VMhost $Host.hostname -virtualswitch $vs2 -portgroup $host.Portgroup2 -ip $Host.IP2 -subnetmask 255.255.255.0 -Mtu 9000

          Get-VirtualPortGroup -VMhost $Host.hostname -virtualswitch $vs2 -Name $Host.IP1 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $Host.Uplink1 -MakeNicUnused $Host.Uplink2

          Get-VirtualPortGroup -VMhost $Host.hostname -virtualswitch $vs2 -Name $Host.IP2 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $Host.Uplink2 -MakeNicUnused $Host.Uplink1

           

          #Create Software iSCSI Adapter

          get-vmhoststorage $Host.hostname | set-vmhoststorage -softwareiscsienabled $True

           

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

          $hbahost = Get-VMHostHba -VMHost $Host.hostname -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}

           

          #Use ESXCLI to create the iSCSI Port binding in the iSCSI Software Adapter

          $esxcli = Get-EsxCli -VMhost $Host.hostname

          $Esxcli.iscsi.networkportal.add($hbahost.device, $Null, $Host.vmk1number)

          $Esxcli.iscsi.networkportal.add($hbahost.device, $Null, $Host.vmk2number)

           

           

          #Using esxcli, configure the LoginTimeout, NoopTimeout and NoopInterval values in seconds

          $esxcli.iscsi.adapter.param.set($hbahost.device,$false,'LoginTimeout','30')

          $esxcli.iscsi.adapter.param.set($hbahost.device,$false,'NoopOutTimeout','30')

          $esxcli.iscsi.adapter.param.set($hbahost.device,$false,'NoopOutInterval','30')

           

          #Setup the Discovery iSCSI IP addresses on the iSCSI Software Adapter

          new-iscsihbatarget -iscsihba $hbahost -address $Host.iSCSITarget

           

           

          #Restart the ESXi Host

          Restart-VMHost -VMHost $Host.hostname -Confirm:$false

           

           

          }

           

           

          If time permits I was hoping to build this into a function so then someone could just run a cmdlet such as Create-NimblevSwitch with the various parameters.

            • Re: PowerCLI to setup hosts networking and storage ready for iSCSI LUNS
              Dean Lewis Wayfarer

              Wow Matt!

               

              Cheers

               

              So I knew I put it into a CSV file, but time meant it was quicker for me to just change the script for each host,

               

              Thanks for the other information however, I'll look to incorporate that if the NCM doesn't do it already, and thanks for pointing out the duplicated effort. The great thing is, I understand the changes you've made, which shows my powershell knowledge is somewhat improving. Hopefully by the end of the year I'm creating proper toolkits

                • Re: PowerCLI to setup hosts networking and storage ready for iSCSI LUNS
                  Matt Allford Newbie

                  No worries Dean - thank you for originally sharing! I was meant to before Christmas but got tied up with some other stuff.

                   

                  I've got a few hosts to build soon so I'll find some time to make a proper powershell function and publish it. I'll also check whether the modifications to the timeouts are required after NCM is installed (I think it is).

                   

                  Powershell is a wonderful platform, I'm nowhere near skilled in it as I want to be, but I know enough to usually make my job a lot simpler or in some cases such as this, just automate the configuration if other tools aren't available in the environment such as host profiles.

              • Re: PowerCLI to setup hosts networking and storage ready for iSCSI LUNS
                Matt Allford Newbie

                So, I got around to creating a powershell script for this task.

                Decided it was probably easier to blog about it, so head on over to PowerCLI Function – Configure ESXi Host for Connectivity to Nimble iSCSI SAN | Matt Alllford's Blog for some information.

                 

                As noted, it is a v1.0 script. Happy to accept feedback, good or bad. Hopefully this helps automate some of the work that might come up for those that don't have host profiles within the VMWare environment.

                Example of the script:

                 

                Create-NimbleVSwitch.ps1 -HostMaintenanceMode -Hostname ESXi1 -Uplink1 vmnic1 -Uplink2 vmnic2 -vSwitchName iSCSIVSwitch -PortGroup1Name iSCSIPG1 -PortGroup2Name iSCSIPG2 -iSCSIAddress1 192.168.0.10 -iSCSIAddress2 192.168.0.11 -iSCSITargetAddress 192.168.0.250 -iSCSIVLAN 800 -MTU 9000 -RebootHost