VMware Cloud Community
dcoz
Hot Shot
Hot Shot
Jump to solution

Grouping datastores my ESX host

Hi guys,

I am trying to create a bit of code that will give me basic details about datastore space etc, but group it by what esx hosts are attached.

I created a simple array but just dont know how to proceed with grouping it based on attached esx host.

$vmh = get-Cluster | get-VMHost

$ds = @()

foreach ($ho in $vmh | get-Datastore)

{

$my ="" |Select-Object name, FreespaceMB

$my.name = $ho.name

$my.freespacemb = $ho.FreeSpaceMB

$ds += $my

}

Any ideas would be greatly appreciated

thanks

DC

0 Kudos
1 Solution

Accepted Solutions
harkamal
Expert
Expert
Jump to solution

$ds = @()

Get-Cluster | Get-VMHost | % {

$myHost = $_

$myHost | Get-DataStore | % { 

$out = "" | Select-Object DSName, FreespaceMB, Host

$out.DSName = $_.Name

$out.FreespaceMB = $_.FreespaceMB

$out.Host = $myHost.Name

$ds += $out 

}

} 

$ds | *Sort* Host

View solution in original post

0 Kudos
3 Replies
harkamal
Expert
Expert
Jump to solution

$ds = @()

Get-Cluster | Get-VMHost | % {

$myHost = $_

$myHost | Get-DataStore | % { 

$out = "" | Select-Object DSName, FreespaceMB, Host

$out.DSName = $_.Name

$out.FreespaceMB = $_.FreespaceMB

$out.Host = $myHost.Name

$ds += $out 

}

} 

$ds | *Sort* Host
0 Kudos
dcoz
Hot Shot
Hot Shot
Jump to solution

Hi harkamal,

Thanks for the help with this.

It works great.

I appreciate the help.

Regards

DC

0 Kudos
harkamal
Expert
Expert
Jump to solution

welcome

there's another cool cmdlet

$ds | Group Name