Wednesday, March 5, 2014

Detaching RDMs from all ESXi Hosts

After removing the RDM disks from the VMs, the LUN must be detached from every host in order to prevent an All paths down condition (APD). This is a tedious task to do manually within the vSphere client, so the following powerCLI script can be used to automatically remove it from all hosts in the cluster.

function Detach-Disk{
    param(
        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,
        [string]$CanonicalName    )
   
    $storSys = Get-View $VMHost.Extensiondata.ConfigManager.StorageSystem
    $lunUuid = (Get-ScsiLun -VmHost $VMHost | where {$_.CanonicalName -eq $CanonicalName}).ExtensionData.Uuid
   
    $storSys.DetachScsiLun($lunUuid)
}

Connect-VIServer -Server "vcenter01"


foreach($esx in (Get-Cluster | Get-VMHost)){
  Get-ScsiLun -VMhost $esx -LunType disk | Where {$_.CanonicalName -like "<naa id>"} | %{
    Detach-Disk -VMHost $esx -CanonicalName $_.CanonicalName
  }
}

Disconnect-VIServer -Server "vcenter01" -Force -Confirm false

No comments:

Post a Comment