VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

remove-datastore_function

Hi Luc ,

would you like to check following function and suggest if anything needed to be modified .

this is to remove-datastore from each and every esxi in cluster.

since we dont have  multiple vmhost option .

pastedImage_0.png

below is the code .

function dismount-datastores

{

    [cmdletbinding()]

    param(

        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]

        [string[]]$datastores,

        [Parameter(Mandatory=$true)]

        [string]$clustername

    )

    $cluster=Get-Cluster $clustername

   

    $vmhosts=(get-vmhost -Location $cluster).name

    $vmhosts

    #$report = @()

    foreach($d in $datastores)

    {

  

   $datastore=Get-Datastore $d

  $vms=get-vm -Datastore $datastore

  #$output = New-Object -TypeName PSObject

  #$output|Add-Member -MemberType NoteProperty -Name 'datastorename' -Value $datastore.name

  #$output|Add-Member -MemberType NoteProperty -Name 'vmsindatastore' -Value $vms.name

 

  #$report += $output

           

           

           

           

 

  if($vms -eq $null)

  {

 

for ($i=0; $i -lt $vmhosts.count;$i++)

{

Remove-Datastore -Datastore $datastore -vmhost $vmhosts[$i] -WhatIf

}

}

}

#$report|Out-GridView

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That function should be ok, but removing a datastore requires more than just having no VMs on the datastore.

See my Test If The Datastore Can Be Unmounted post for a function that tests all possible reasons why a datastore can not be removed.


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

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

That function should be ok, but removing a datastore requires more than just having no VMs on the datastore.

See my Test If The Datastore Can Be Unmounted post for a function that tests all possible reasons why a datastore can not be removed.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks Luc,

i found following after running your function:

pastedImage_0.png

however we are on 6.5 vsphereand if we go by webclient only two conditions should be met

pastedImage_1.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you look at KB2004605 there is a bit more listed, also for vSphere 6.5


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks Luc.

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

just thought of asking if you are part of powershellers community also Smiley Happy?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That group (not a community) was set up quite some time ago by a non-VMware person.

It is hardly used and I only very rarely look at it.

As far as I'm concerned that group could be removed, seen the overlap with the PowerCLI community.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

i see you always reply .sometimes for non powercli questions also.

thnaks for massive  global support .

is it fine if i can post some powershell specific  questions here i mean not entirely powercli.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, you would not be the first one :smileygrin:


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks Luc.

0 Kudos