Search code examples
powershellpowercli

PowerCLI - find datastores in a available to a custer


I'm trying to find a way to get a list of datastores available in a cluster. So far I got this far:

$1 = get-view -viewtype ClusterComputeResource
$1 | select name,datastore

that will list clasters and datastores but the names do not match those in vCenter server. I can see both names but without the custer info when running this:

Get-Datastore | select id,name

How do I get those information together so I have ClusterName (Name - from the first command), DatastoreName (Name - from the second command).


Solution

  • Right it took me a while but I found a way of doing it with the use of Join-Object fuction from here: https://blogs.msdn.microsoft.com/powershell/2012/07/13/join-object/

    $cluster = get-cluster | select name | % {$counter = -1} {$counter++; $_ | Add-Member -Name Cluster_ID -Value $counter -MemberType NoteProperty -PassThru}
    $cluster | ft -auto
    $clusterid = read-host "select cluster"
    $VMCLUSTER = $cluster[$clusterid]
    
    # Select Datastore
    $1 = get-view -viewtype ClusterComputeResource | where name -like $vmcluster.name | select name,datastore
    $table1 = @()
    foreach ($datastore in $1.Datastore) {
    $table = " " | select datastore_ID,Cluster_Name
    $table.datastore_ID = $datastore
    $table.Cluster_name =  $1.Name
    $table1 += $table
    }
    
    $datastores = Join-Object -Left $table1 -Right (Get-datastore | select id,name,FreeSpaceGB,CapacityGB) -Where { $args[0].datastore_ID -eq $args[1].id} -LeftProperties 'Cluster_Name' -RightProperties * -Type AllInLeft
    $datastores | % {$counter = -1} {$counter++; $_ | Add-Member -Name Datastore_ID -Value $counter -MemberType NoteProperty -PassThru} | select Datastore_ID,Cluster_Name,Name,FreeSpaceGB,CapacityGB | Sort-Object FreeSpaceGB -Descending | ft -AutoSize
    $datastoreid = read-host "Select Datastore (enter Datastore_ID)"
    $selected_datastore = $datastores[$datastoreid]