Reply to Message

View discussion in a popup

Replying to:
BenLiebowitz
Expert
Expert

Remove a datastore that is in use

So, we have some datastores we're looking to retire, however removing them from the web client gives us a "datastore is in use" error.  We worked with VMware support, and they gave us a bunch of partedutil commands, but I'd like to try and automate this to reduce the human error factor. 

The commands to run are:

partedUtil getptbl /vmfs/devices/disks/naa.1234567890

partedUtil delete /vmfs/devices/disks/naa.1234567890

Everything else I can do with get-scsilun

This is what I have so far...

1. connect to vcenter

2. list the clusters

3. list the hosts in the selected cluster

4. list the datastores/luns on the host

5. browse the foor of the datastore selected (to verify nothing is on it and it's the one to be removed)

6. This would be where the partedutil commands go.  Smiley Happy

Thanks in advance!

----------------------

# Define the LunDatastoreName function

New-VIProperty -Name lunDatastoreName -ObjectType ScsiLun -Value {

    param($lun)

    $ds = $lun.VMHost.ExtensionData.Datastore | %{Get-View $_} | `

        where {$_.Summary.Type -eq "VMFS" -and

            ($_.Info.Vmfs.Extent | where {$_.DiskName -eq $lun.CanonicalName})}

    if($ds){

        $ds.Name

    }

} -Force | Out-Null

# connect to selected vCenter

connect-viserver $vcenter

# List Clusters

write-host ""

Write-host "Choose which Cluster where the LUN you want to remove presented:"

write-host "(it may take a few seconds to build the list)"

write-host ""

$ICLUSTER = get-cluster | Select Name | Sort-object Name

$i = 1

$ICLUSTER | %{Write-Host $i":" $_.Name; $i++}

$HCLUSTER = Read-host "Enter the number for the host to Patch."

$SCLUSTER = $ICLUSTER[$HCLUSTER -1].Name

write-host "You have selected $($SCLUSTER)."

start-sleep -s 3

# List hosts to select

write-host ""

Write-host "Choose which vSphere host that has the LUN presented."

write-host "(it may take a few seconds to build the list)"

write-host ""

$IHOST = get-cluster $SCLUSTER | Get-VMhost | Select Name | Sort-object Name

$i = 1

$IHOST | %{Write-Host $i":" $_.Name; $i++}

$DSHost = Read-host "Enter the number for the host to Patch."

$SHOST = $IHOST[$DSHost -1].Name

write-host "you have selected" $SHOST"."

# List all scsi devices

write-host ""

Write-host "Choose which LUN to remove.  *** VERIFY VIA THE vSPHERE CLIENT ***"

write-host "(it may take a minute to build the list)"

write-host ""

$ILUN = get-scsilun -vmhost $SHOST | Select LunDatastoreName, CanonicalName, RuntimeName, CapacityGB | Sort lundatastorename

$i = 1

$ILUN | %{Write-Host $i":" $_.lunDatastoreName, $_.RuntimeName, $_.CapacityGB; $i++}

$DSLUN = Read-host "Enter the number for the LUN to remove."

$SLUN = $ILUN[$DSLUN -1].LunDatastoreName

write-host "you have selected" $SLUN"."

# List the files on the LUN you selected

get-childitem (get-datastore $SLUN).datastorebrowserpath

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.