3 Replies Latest reply on Feb 14, 2017 9:19 AM by LucD

    Another Datastore Scripting Question

    DavidMcKnight Novice

      I need to do some storage server upgrades.  That means removing all the mapped iSCSI datastores being shared from a particular storage server. Normally I would do it manually from inside of the vSphere client, just to make sure that the datastores are empty.  So after I'm confident a datastore is empty I would unmount the datastore then delete it from inside of vSphere Client.  My problem is, when I go back to the storage server it says that all my hosts are still connected to iSCSI Target.  So the question is: Can I/How do I get my VMHosts to disconnect/logout of my storage server.  I understand I don't have to, but I would still like to know if there is a way to do it.  I guess my biggest problem is, that once I manually delete a datastore from my cluster I can't find a way to reference it or its LUNs to even attempt to clean up any lingering associations between my hosts and my decommissioned storage server.


      How would I get a list of LUNs that are not actively being used in datastores?  I can't think of a reason why I would have a LUN and not have it being associated to an active datastore.


      In the end, I think, I would use something like:




      to have a host disconnect/logout from an iSCSI session?


      Second question.  Along the same line as the first.  When I bring online the replacement storage server I'll be adding new iSCSI datastores to my cluster.  With my recent upgrade to vCenter 6.5 and being forced to use that god awful web interface to manage my VMware infrastructure, I can't find a way to see the iSCSI IQN on the page where I add a new datastore, so if I have multiple iSCSI targets ready to add to my cluster I can't tell which datastore is which to assign it the proper name.  At present I can manually get a list of the available device UUIDs then go back to the storage adapters configuration page, list all the LUNs there, then one by one cross-reference the UUID and get the IQN.


      Anyway, how can get a list of all the LUNs no being used in datastores and get the IQN of that LUN?

      I hope these questions make sense, and thanks.

        • 1. Re: Another Datastore Scripting Question
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          Try something like this.


          $report = foreach($esx in Get-VMHost){

              $dsTab = @{}

              foreach($ds in Get-Datastore -VMHost $esx){


                      $ds.ExtensionData.Info.Vmfs.Extent | %{






                  $hbaTab = @{}

                  $esx.ExtensionData.Config.StorageDevice.HostBusAdapter | %{



                  $lunTab = @{}

                  $esx.ExtensionData.Config.StorageDevice.ScsiLun | %{



                  $esx.ExtensionData.Config.StorageDevice.ScsiTopology | %{

                      foreach($adapter in ($_.Adapter | where{$_.Adapter -match "InternetScsi"})){

                          $iqnTab = @{}

                          foreach($lun in $esx.ExtensionData.Config.StorageDevice.MultipathInfo.Lun){

                              $lun.Path | where{$_.Adapter -eq $adapter.Adapter} | %{




                          $adapter.Target | %{

                              $obj = [ordered]@{

                                  VMHost = $esx.Name

                                  HBA = $hbaTab[$adapter.Adapter]

                                  ScsiID = $_.Lun[0].Lun

                                  CanonicalName = $lunTab[$_.Lun[0].ScsiLun]

                                  IQN = $iqnTab[$_.Lun[0].ScsiLun]

                                  Datastore = $dsTab[$lunTab[$_.Lun[0].ScsiLun]]


                              New-Object psobject -Property $obj






          $report | Format-Table -AutoSize


          It produces a report something like this.

          When the Datastore property is empty, it indicates a LUN that is not used.


          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Another Datastore Scripting Question
            DavidMcKnight Novice

            Wow, that looks a lot more complicated than I thought it would.  I don't feel as bad not being able to figure this out on my own.  That being said, I'm going to assume it's right and take a some time to chew on your code.


            One last point of verification.  Given the information I now have access to.  What function do I call get a host to properly disconnect a remote iSCSI session?


            Thanks again.

            • 3. Re: Another Datastore Scripting Question
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              Something like this


              Get-VMHostHba -VMHost esx1* -Type IScsi | Get-IScsiHbaTarget | Remove-IScsiHbaTarget -WhatIf



              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz