Search code examples
powershellvmwarepowershell-3.0powercli

PowerCLI - How to filter datastores by hosts which use them


How would I filter an array of data stores so that I am left with those that have a Host in their ExtensionData.Host array?

I have tried:

$Datastores = Get-Datastore
$HostDS = $Datastores | Where-Object{$_.ExtensionData.Host -contains $clusterhost}

But this doesn't work, because the host array in $Datastore.ExtensionData.Host is not of the datastore objects, but of DatastoreHostMount objects. The host mount objects have the IDs of the hosts I want, but I do not know how to compare them against the ID of the $clusterhost I am trying to compare them against.

Can I accomplish what I am trying to do here through filtering? I am trying to avoid another call of Get-Datastore for efficiency.


Solution

  • there is a key in the object that you need use in your where clause:

    $hostds = Datastores | where-object{$_.extensiondata.host.key -eq $clusterhost}
    
    Name                               FreeSpaceGB      CapacityGB
    ----                               -----------      ----------
    Name2                            x,xxx.xxx       xx,xxx.xxx
    Name1                                x,xxx.xxx       xx,xxx.xxx
    
    
    
    $datastores.extensiondata.host  |gm
    
    TypeName: VMware.Vim.DatastoreHostMount
    
    Name        MemberType Definition                                                
    ----        ---------- ----------                                                
    Equals      Method     bool Equals(System.Object obj)                            
    GetHashCode Method     int GetHashCode()                                         
    GetType     Method     type GetType()                                            
    ToString    Method     string ToString()                                         
    Key         Property   VMware.Vim.ManagedObjectReference Key {get;set;}          
    LinkedView  Property   VMware.Vim.DatastoreHostMount_LinkedView LinkedView {get;}
    MountInfo   Property   VMware.Vim.HostMountInfo MountInfo {get;set;} 
    

    That key had the host name in it...