Tuesday, May 14, 2013

VMware Powercli script to remove the detached device in vsphere 5, Script to remove detached device.

Simple script to remove the detached list from the esxi in the cluster. copy the below and save as remove_detached_device.ps1, run the below script on power shell.

#All host is the cluster with ESXCLI
$e1 = get-cluster -name Clustername| get-vmhost | get-esxcli
#Each ESXi in the cluster with the detached device will be removed.
foreach ($esx in $e1)
{
$esx.storage.core.device.detached.list() | Select -ExpandProperty DeviceUID | Select -First XX(no of disk) | %{ $esx.storage.core.device.detached.remove($_) }
}

 or

Get the list of device that needs to be removed from detached list and put in notepad  as detachlun.txt save in desktop. copy the below and save as remove_detached_device.ps1, run the below script on powershell.

$e1 = get-cluster -name clustername| get-vmhost | get-esxcli
$lun = get-content c:\users\test\desktop\detachlun.txt
foreach ($esx in $e1)
{
$lun | %{ $e1.storage.core.device.detached.remove($_)}
}
used with vsphere 5.0 and VMware vSphere PowerCLI 5.0.1 work perfectly, need some fine tuning to it to produce a report, once done i will update this again.