VMware Cloud Community
jrich523
Contributor
Contributor

Remove-DataStore issue

$datastores = get-datastore | >{$_.parentfolder.name -eq "deletethese"}

$vmhost = get-vmhost 'fqdn'

$datastores | %{remove-datastore -datastore $_ -vmhost $vmhost}

this will delete the first, then when it gets to the second it crashes saying there are resources in use... i rerun the whole thing (grab DS and try to remove) and it deletes the one it just had a problem with and then throws a problem with the next one...

I have to remove about 25 datastores that i'd prefer not to do by hand, whats going on here?

Tags (1)
0 Kudos
3 Replies
mattboren
Expert
Expert

Hello, jrich523-

Hm, I was not able to reproduce the "crashes" that you talk about.  I wonder, what version of PowerCLI are you using?  You can get that info by running:

PS C:\> Get-PowerCLIVersion

How does the Remove-Datastore do if you pass the array of datastores ("$datastores") directly to it, instead of one datastore at a time in a Foreach-Object loop?  So, the last line would be like (with the confirmation suppression added):

Remove-Datastore -Datastore $datastores -VMHost $vmhost -Confirm:$false

(since, per the docs, the cmdlet accepts an array of datastore objects -- you do not have to pass them in one at a time)

And, I assume that the ">" in the first line is just a type, and that it is really a "%" in your code -- is that true?

0 Kudos
jrich523
Contributor
Contributor

PowerCLI Version

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

VMware vSphere PowerCLI 4.1 build 264274

This  version does not support an array

how ever i think i found the problem. If i monitor the vcenter console i see that it does a recan VMFS on all hosts (san disk) and i've put a sleep in at the end of the command (allowing the scan to complete) and it seems to be working. here is the final command.

$datastores | %{write-host "Removing $($_.name)";Remove-Datastore -Datastore $_ -VMHost $hosts[0] -Confirm:$false; sleep 5}

$datastores = Get-Datastore | ?{$_.parentfolder.name -eq "datastore"}

This time i was able to get many more out but not all. this is fine because it will only take a few runs to get them all.

Thanks

Justin

0 Kudos
jrich523
Contributor
Contributor

Time was needed between removes for the recan of VMFS that was automatically kicked off upon removal

0 Kudos