VMware Cloud Community
fscked
Contributor
Contributor
Jump to solution

Getting too many results

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 = 2
Connect-VIServer $ServerName
foreach($cluster in Get-Cluster | where {$_.name -like $cl_filter_pattern}){
    Get-VMHost -Location $cluster | Get-Datastore | %{
        $vms = $cluster | Get-VM
foreach ($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 -UseCulture
Disconnect-VIServer $ServerName -Confirm:$false

Where am I going wrong?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I rearranged the loops a bit, and I use a hash table as a lookup for the datastores.

That avoids doing multiple Get_View cmdets for the same datastore.

I use the datastoreId list for each VM.

Let me know if this produces a better result ?

$ServerName = "servername" 
$report
= @() $cl_filter_pattern = "**PCI"
$digits = 2

Connect-VIServer
$ServerName
foreach($cluster in Get-Cluster | where {$_.name -like $cl_filter_pattern}){   $dsTab = @{}   Get-VMHost -Location $cluster | Get-Datastore |

  where {$_.ExtensionData.Summary.MultipleHostAccess} | %{     $dsTab.Add($_.Id,$_)   }   foreach ($vm in Get-VM -Location $cluster){     $vm.DatastoreIdList | %{       $ds = $dsTab[$_]       $info = "" | select DataCenter, Cluster, Datastore, VM, Capacity, Provisioned, Available, PercFree
     
$info.Datacenter = $ds.Datacenter.Name
      $info.Cluster = $cluster.Name
      $info.Datastore = $ds.Name
      $info.VM = $vm.Name
      $info.Capacity = [math]::Round($ds.capacityMB/1024,$digits)       $info.Provisioned = [math]::Round(($ds.ExtensionData.Summary.Capacity - $ds.ExtensionData.Summary.FreeSpace + $ds.ExtensionData.Summary.Uncommitted)/1GB,$digits)       $info.Available = [math]::Round($info.Capacity - $info.Provisioned,$digits)       $info.PercFree = [math]::Round(100 * $ds.FreeSpaceMB/$ds.CapacityMB,$digits)       $report += $info
    }   } } $report | Export-Csv "C:\datastore-vms.csv" -NoTypeInformation -UseCulture
Disconnect-VIServer $ServerName -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Are your datastores shared between clusters ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

fscked
Contributor
Contributor
Jump to solution

Yes, lots of clusters, lots of datastores and lots of vms. :smileyshocked:

That's the problem, my logic in my brain isn't correlating what I am doing wrong.

All I am trying to do is get a list of vms from a wild card cluster filter and then list the vm names, cluster names, datastore names, and freespace (in percentage) of the listed datastores and export it.

Seems like it should be easy, but my brain is muddled and I am missing something obvious.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I rearranged the loops a bit, and I use a hash table as a lookup for the datastores.

That avoids doing multiple Get_View cmdets for the same datastore.

I use the datastoreId list for each VM.

Let me know if this produces a better result ?

$ServerName = "servername" 
$report
= @() $cl_filter_pattern = "**PCI"
$digits = 2

Connect-VIServer
$ServerName
foreach($cluster in Get-Cluster | where {$_.name -like $cl_filter_pattern}){   $dsTab = @{}   Get-VMHost -Location $cluster | Get-Datastore |

  where {$_.ExtensionData.Summary.MultipleHostAccess} | %{     $dsTab.Add($_.Id,$_)   }   foreach ($vm in Get-VM -Location $cluster){     $vm.DatastoreIdList | %{       $ds = $dsTab[$_]       $info = "" | select DataCenter, Cluster, Datastore, VM, Capacity, Provisioned, Available, PercFree
     
$info.Datacenter = $ds.Datacenter.Name
      $info.Cluster = $cluster.Name
      $info.Datastore = $ds.Name
      $info.VM = $vm.Name
      $info.Capacity = [math]::Round($ds.capacityMB/1024,$digits)       $info.Provisioned = [math]::Round(($ds.ExtensionData.Summary.Capacity - $ds.ExtensionData.Summary.FreeSpace + $ds.ExtensionData.Summary.Uncommitted)/1GB,$digits)       $info.Available = [math]::Round($info.Capacity - $info.Provisioned,$digits)       $info.PercFree = [math]::Round(100 * $ds.FreeSpaceMB/$ds.CapacityMB,$digits)       $report += $info
    }   } } $report | Export-Csv "C:\datastore-vms.csv" -NoTypeInformation -UseCulture
Disconnect-VIServer $ServerName -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
fscked
Contributor
Contributor
Jump to solution

Luc, my friend, you are the man.

Thank you so much for fixing that for me. It works perfect!

Reply
0 Kudos