VMware Cloud Community
ap296n
Contributor
Contributor
Jump to solution

Cannot filter Datastores by Cluster using PowerCLI

Hi, I am trying to retrieve Datastore information and I need the cluster the Datastore is associated with. I for, whatever reason, cannot use clusters in the searchroot parameter as such:

$cluster = Get-Cluster -Name "MyCluster"

Get-View -ViewType Datastore -SearchRoot $cluster.id

This does not return anything for me, where as if I substitute the cluster for a datacenter, I get all of the Datastores in the datacenter, although I need the cluster as well. So I found another way to get the cluster, through the host using this code snippet I whipped up:

$vmhosts = $datastore.Host

$cluster = Get-View -id (Get-View -Id $vmhosts[0].Key | select -Property Parent).Parent | Select -Property Name

Write-Host $cluster.Name

...where $datastore is a Datastore view. This does give me the cluster name, although the script runs extremely slow and takes a very long time to run. Our environment contains several thousand Datastores so you can see why the runtime of the script is a big concern for me. Here is the full function to give context to my issue.

===========================================================================================

# Goes Through Entire vCenter and gets individual SAN data

Function Get-AllSANData($vcenter) {

    $WarningPreference = "SilentlyContinue"

    Connect-VIServer $vcenter -ErrorAction SilentlyContinue -ErrorVariable ConnectError | Out-Null

    write-host "Extracting SAN data from" $vcenter "..."

    write-host "This will take some time, stop staring at me and go do something else..."

   

    # Loop through each Datacenter in the vCenter

    ForEach ($datacenter in Get-Datacenter) {

       

        # Create Datastore view and Loop through each datastore in the cluster

        ForEach ($datastore in Get-View -ViewType Datastore -SearchRoot $datacenter.id -Filter @{"Summary.Type"="VMFS"}) {

           

            $vmhosts = $datastore.Host # This is an Array of all Hosts associated with this SAN Volume

            $hostcount = $vmhosts.Length # Num of Hosts associated with this SAN SAN Volume

           

            if ($hostcount -lt 2) { continue } # Ignore Boot Volumes

               

            $lunsize = $datastore | %{[decimal]::round($_.Summary.Capacity / 1GB)} # Capacity in Bytes converted to GB

            $free = $datastore | %{[decimal]::round($_.Summary.FreeSpace / 1GB)} # Free Space in Bytes converted to GB

            $type = $datastore | %{$_.Summary.Type} # We already know that type will be VMFS but just in case

            $majorversion = $datastore | %{$_.Info.Vmfs.MajorVersion} # Major VMFS Version (5.blah = 5) you get the idea

               

            $cluster = Get-View -id (Get-View -Id $vmhosts[0].Key | select -Property Parent).Parent | Select -Property Name

           

            write-host $datacenter . $cluster.Name . $datastore.Name . $lunsize . $free . $type . $majorversion . $hostcount

        }

    }

  

    Disconnect-VIServer $vcenter -Force -Confirm:$false | Out-Null

    write-host "Done with " $vcenter

}

===========================================================================================

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
ap296n
Contributor
Contributor
Jump to solution

I found a solution some time ago. Thanks for following up. This is what I ended up doing:

# Goes Through Entire vCenter and gets individual SAN data

Function Get-AllSANData($vcenter, $fileName, $MyDirectory) {

    $WarningPreference = "SilentlyContinue"

    Connect-VIServer $vcenter -ErrorAction SilentlyContinue -ErrorVariable ConnectError | Out-Null

    write-host "Extracting SAN data from" $vcenter "..."

    write-host "This will take some time, stop staring at me and go do something else..."

   

    # Loop through each Datacenter in the vCenter - MoRef is the ID

    ForEach ($datacenter in Get-View -ViewType Datacenter | Select -Property Name,MoRef) {

        # Loop through each Cluster in the Datacenter

        ForEach ($cluster in Get-View -ViewType ClusterComputeResource -SearchRoot $datacenter.MoRef | Select -Property Name,Datastore) {

       

            # Create Datastore view and Loop through each datastore in the cluster

            ForEach ($datastore in $cluster.Datastore) {

           

                $ds = Get-View -Id $datastore | Select -Property Name,Host,Summary,Info # Create Datastore view with current Datastore in Cluster

               

                $hostcount = $ds.Host.Length # Num of Hosts associated with this SAN Volume

                if ($hostcount -lt 2) { continue } # Ignore Boot Volumes

               

                $type = $ds | %{$_.Summary.Type} # The type needs to be VMFS, not interesting in NAS or any other type

                # Don't filter for VDT Recons - need NFS Storage as well

                if ($type -ne "VMFS") { continue }

               

                $lunsize = $ds | %{[decimal]::round($_.Summary.Capacity / 1GB)} # Capacity in Bytes converted to GB

                $free = $ds | %{[decimal]::round($_.Summary.FreeSpace / 1GB)} # Free Space in Bytes converted to GB

                $uncommitted = $ds | %{[decimal]::round($_.Summary.Uncommitted / 1GB)} # Uncommitted Storage in Bytes converted to GB

                $provisioned = ($lunsize - $free + $uncommitted)

               

                $majorversion = $ds | %{$_.Info.Vmfs.MajorVersion} # Major VMFS Version (5.blah = 5) you get the idea

               

                $upperVC = $vcenter.ToString().ToUpper()

                $upperCL = $cluster.Name.ToString().ToUpper()

                $upperDS = $ds.Name.ToString().ToUpper()

               

                write-host $datacenter.Name . $upperCL . $upperDS . $lunsize . $provisioned . $free . $type . $majorversion . $hostcount

              

                # Output Data to CSV (file is located in same directory that the script runs from)

                $record = $datacenter.Name+","+$upperCL+","+$upperDS+","+$lunsize+","+$provisioned+","+$free+","+$type+","+$majorversion+","+$hostcount

                $record | Out-File -Append $MyDirectory\SANpulls\$fileName -Encoding ASCII

            }

        }   

    }

View solution in original post

Reply
0 Kudos
2 Replies
michaelbrux
Contributor
Contributor
Jump to solution

Are you still looking for a solution?  If so can you clarify what you are trying to do?   

this code doesn't work " Get-View -ViewType Datastore -SearchRoot $cluster.id"  because datastores do not have a direct relationship with clusters.  They are configured to hosts that can be in multiple clusters

Reply
0 Kudos
ap296n
Contributor
Contributor
Jump to solution

I found a solution some time ago. Thanks for following up. This is what I ended up doing:

# Goes Through Entire vCenter and gets individual SAN data

Function Get-AllSANData($vcenter, $fileName, $MyDirectory) {

    $WarningPreference = "SilentlyContinue"

    Connect-VIServer $vcenter -ErrorAction SilentlyContinue -ErrorVariable ConnectError | Out-Null

    write-host "Extracting SAN data from" $vcenter "..."

    write-host "This will take some time, stop staring at me and go do something else..."

   

    # Loop through each Datacenter in the vCenter - MoRef is the ID

    ForEach ($datacenter in Get-View -ViewType Datacenter | Select -Property Name,MoRef) {

        # Loop through each Cluster in the Datacenter

        ForEach ($cluster in Get-View -ViewType ClusterComputeResource -SearchRoot $datacenter.MoRef | Select -Property Name,Datastore) {

       

            # Create Datastore view and Loop through each datastore in the cluster

            ForEach ($datastore in $cluster.Datastore) {

           

                $ds = Get-View -Id $datastore | Select -Property Name,Host,Summary,Info # Create Datastore view with current Datastore in Cluster

               

                $hostcount = $ds.Host.Length # Num of Hosts associated with this SAN Volume

                if ($hostcount -lt 2) { continue } # Ignore Boot Volumes

               

                $type = $ds | %{$_.Summary.Type} # The type needs to be VMFS, not interesting in NAS or any other type

                # Don't filter for VDT Recons - need NFS Storage as well

                if ($type -ne "VMFS") { continue }

               

                $lunsize = $ds | %{[decimal]::round($_.Summary.Capacity / 1GB)} # Capacity in Bytes converted to GB

                $free = $ds | %{[decimal]::round($_.Summary.FreeSpace / 1GB)} # Free Space in Bytes converted to GB

                $uncommitted = $ds | %{[decimal]::round($_.Summary.Uncommitted / 1GB)} # Uncommitted Storage in Bytes converted to GB

                $provisioned = ($lunsize - $free + $uncommitted)

               

                $majorversion = $ds | %{$_.Info.Vmfs.MajorVersion} # Major VMFS Version (5.blah = 5) you get the idea

               

                $upperVC = $vcenter.ToString().ToUpper()

                $upperCL = $cluster.Name.ToString().ToUpper()

                $upperDS = $ds.Name.ToString().ToUpper()

               

                write-host $datacenter.Name . $upperCL . $upperDS . $lunsize . $provisioned . $free . $type . $majorversion . $hostcount

              

                # Output Data to CSV (file is located in same directory that the script runs from)

                $record = $datacenter.Name+","+$upperCL+","+$upperDS+","+$lunsize+","+$provisioned+","+$free+","+$type+","+$majorversion+","+$hostcount

                $record | Out-File -Append $MyDirectory\SANpulls\$fileName -Encoding ASCII

            }

        }   

    }

Reply
0 Kudos