3 Replies Latest reply on Feb 19, 2019 10:14 AM by Zsoldier

    Remove iSCSI datastore from cluser

    DavidMcKnight Novice

      I saw there was just a discussion touching this topic, but I have a more detailed question.



      I have an iSCSI server with several iSCSI targets being shared to my ESXi cluster.  I need to upgrade the server.  I know I could just migrate all the VMs to other iSCSI servers and power off the empty iSCSI server.  I don't think that's the proper way. What I want in the end is to be able to add the recreated iSCSI datastores without any previous vCenter/Host settings carrying over. A clean slate.

      The process I've come up with is:


      1. Migrate VMs off the datastore.
      2. Unmount datastore
      3. Remove datastore
      4. Disconnect datastore
      5. Unmap iSCSI target


      Here's the code I've cobbled together from here and other corners of the internet and it works:


      #Migrating VMs is some I do more manually.


      $iSCSINames = "iSCSI-12-04","iSCSI-12-05","iSCSI-12-06"


      $alarm = Get-AlarmDefinition -Entity $cluster -Name "Cannot connect to storage"

      Set-AlarmDefinition -AlarmDefinition $alarm -Enabled:$false


      foreach ($iSCSIName in $iSCSINames){

        $ds = get-datastore -name $iSCSIName

        $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname

        if ($ds.ExtensionData.Host) {

          $attachedHosts = $ds.ExtensionData.Host

          Foreach ($VMHost in $attachedHosts) {

            $hostview = Get-View $VMHost.Key

            $StorageSys = Get-View $HostView.ConfigManager.StorageSystem

            Write-Host "Unmounting VMFS Datastore $($DS.Name) from host $($hostview.Name)..."



          Write-Host "Removing $($DS.Name) from cluster..."

          Remove-Datastore -VMHost $hostview.name -Datastore $ds -ErrorAction SilentlyContinue -Confirm:$false

          Start-Sleep 10

          Foreach ($VMHost in $attachedHosts) {

            $hostview = Get-View $VMHost.Key

            $StorageSys = Get-View $HostView.ConfigManager.StorageSystem

            $devices = $StorageSys.StorageDeviceInfo.ScsiLun

            Foreach ($device in $devices) {

              if ($device.canonicalName -eq $hostviewDSDiskName) {

                $LunUUID = $Device.Uuid

                Write-Host "Detaching LUN $($Device.CanonicalName) from host $($hostview.Name)..."





            $iScsi = $storageSys.StorageDeviceInfo.HostBusAdapter | where { $_.GetType().Name -eq "HostInternetScsiHba" }

            $iScsiDevice = $iscsi.Device

            $staticTargets = $iScsi | foreach { $_.ConfiguredStaticTarget  } | where { $_.IScsiName -like $iSCSIName }

            if ($staticTargets -ne $null -and $staticTargets.Count -gt 0) {

              $staticTargetsAddress = $staticTargets | foreach {$_.address}

              $staticTargetsAddress = $staticTargetsAddress -join ', '

              Write-Host "Unmapping iSCSI address(es) $($staticTargetsAddress) from host $($hostview.Name)..."

              $storageSys.RemoveInternetScsiStaticTargets($iScsiDevice, $staticTargets)





      start-sleep 10

      Set-AlarmDefinition -AlarmDefinition $alarm -Enabled:$true


      Here are the problems I can’t solve. Hopefully someone can help.  As the code runs it still generates an alarm "Cannot connect to storage". Even though I’ve disable the alarm.  Second, after I reformat the iSCSI server, reinstall the OS, and recreate the iSCSI targets (with the same names it used to have), I scan for new datastores in vCenter.  I see several datastores I can format and add to my cluster.  When done the hosts still remember and applies all the setting they had from the previous iSCSI targets such as RoundRobin.  This tells me I didn't really get the previous iSCSI targets completely removed.  Is there something more I could do to so I can add the new ones with a completely clean slate? 

        • 1. Re: Remove iSCSI datastore from cluser
          Zsoldier Expert

          So to answer you question around the alarm.  It's because you are only disabling the alarm on the "Cluster" object.  You would need to disable the alarm on each "host" object as well to prevent the alarm from firing off.


          Unsure about the settings aspect though.  I do know pathing policies have had defaults changed in the past either from VMware or defined in your ESXi host build configuration that would keep it for certain types.  What other settings are you referring to?

          • 2. Re: Remove iSCSI datastore from cluser
            DavidMcKnight Novice

            > You would need to disable the alarm on each "host" object


            Ok I see a bug for the Get-AlarmDefinition.  The variable $cluster is empty.  Regardless, the code works and returns the reference to the "Cannot connect to storage" alarm that is defined at the root of vCenter.  I can't find a way to disable that alarm at the Host level. (PowerCLI: Enable / Disable Alarm Actions on Hosts / Clusters ).


            >What other settings are you referring to?

            So here's the crux of the problem. When I redo this storage servers I'm installing SSDs.  This should allow me the present the newly created datastores as flash drives. While I was working on this I would add and remove datastores to test the PowerCLI code.  After recreating a datastore and having a host rescan for storage it would remember that that datastore is dettached and I'd have to attach it in vCenter.  Then it would see it as a "HDD" not "Flash".  Well not always, sometimes when I scanned it would.


            Anyway, I either need to finish removing all references to the old datastores, Luns, etc. or (in my particular case) the code to set a storage device to "flash".  I already have the code to attach datastores.

            • 3. Re: Remove iSCSI datastore from cluser
              Zsoldier Expert

              I take that back, it appears as long as you target the cluster object and disable the alarm, it should work now.  Assuming that cluster object is populated.  For the sake of specificity, if you wanted to target a custom set of hosts:


              foreach ($vmhost in $clusterhosts){

              $alarm = Get-AlarmDefinition -Name "Cannot connect to storage" -Entity $vmhost

              Set-AlarmDefinition -AlarmDefinition $alarm -Enabled:$false



              Just tested against vCenter 6.7, Powershell Core 6.1.2, Powercli 10.2.0


              Looking through your code again regarding 'ghost' settings.  There might be a step missing, but unsure.  Don't work w/ iSCSI all that much.