I am trying to write a script to get all the vms in a set of clusters and the datastores they are on with the amount of datastore freespace. For some reason it is getting ALL the vms on the datastores.
$ServerName = "servername"$report = @()$cl_filter_pattern = "*PCI"$digits = 2Connect-VIServer $ServerNameforeach($cluster in Get-Cluster | where {$_.name -like $cl_filter_pattern}){Get-VMHost -Location $cluster | Get-Datastore | %{$vms = $cluster | Get-VMforeach ($vm in $vms){$info = "" | select DataCenter, Cluster, Datastore, VM, Capacity, Provisioned, Available, PercFree$info.Datacenter = $_.Datacenter$info.Cluster = $cluster.Name$info.Datastore = $_.Name$info.VM = $vm.Name$info.Capacity = [math]::Round($_.capacityMB/1024,2)$info.Provisioned = [math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,2)$info.Available = [math]::Round($info.Capacity - $info.Provisioned,2)$info.PercFree = [math]::Round(100*$_.FreeSpaceMB/$_.CapacityMB,$digits)$report += $info}}}$report | Export-Csv "C:\datastore-vms.csv" -NoTypeInformation -UseCultureDisconnect-VIServer $ServerName -Confirm:$false
Where am I going wrong?