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
}
===========================================================================================