jtivan
Contributor
Contributor

how to detach rdm disks from cluster simultaneously?

Hi I have such a script. but it detach rdm disks sequentially. tell me how to convert a script so that it simultaneously disables rdm from all cluster hosts

Connect-VIServer "my-vcerver"
$filepath = "D:\scripts\usov_is\naa.csv"
$Clustername = "AZRC"
$LunIDs = @()

Import-Csv $filepath |

foreach {
$LunIDs = $LunIDs + $_.naa

}
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)
}

$ClusterHosts = Get-Cluster $Clustername | Get-VMHost
Foreach ($VMHost in $ClusterHosts)
{
         Foreach($LUNid in $LunIDs)
    {
        Write-Host "Detaching" $LUNid "from" $VMHost -ForegroundColor "Black"
        Detach-Disk -VMhost $VMHost -CanonicalName $LUNid
    }
    
}

1 Reply
LucD
Leadership
Leadership

With the DetachScsiLunEx method (vSphere 6.*) you can detach multiple LUNs in 1 call.

If we use the method in async mode (the _Task extension), it will return immediately.

Connect-VIServer "my-vcerver"

$filepath = "D:\scripts\usov_is\naa.csv"

$Clustername = "AZRC"

$LunIds = Import-Csv $filepath | Select -ExpandProperty naa

function Detach-Disks {

    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 -contains $_.CanonicalName}).ExtensionData.Uuid

    $storSys.DetachScsiLunEx_Task($lunUuid)

}

$ClusterHosts = Get-Cluster $Clustername | Get-VMHost

Foreach ($VMHost in $ClusterHosts)

{

    Write-Host "Detaching $($LUNid -join '|') from $VMHost" -ForegroundColor "Black"

    Detach_Disks -VMHost $VMHost -CanonicalName $LunIds

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference