VMware Cloud Community
aaronmiller85
Enthusiast
Enthusiast
Jump to solution

Sorting issue

This is sorting 'Available' by lexigraphic...I want it decending from greatest to smallest.

Can anyone tell me why it is sorting this way and how to fix it?

<pre>

#header info

    $style = "<style>BODY{font-family: Arial; font-size: 10pt;}"

    $style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"

    $style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"

    $style = $style + "TD{border: 1px solid black; padding: 5px; }"

    $style = $style + "</style>"

$report = New-Object -TypeName System.Collections.ArrayList

foreach($cluster in Get-Cluster){

    Get-VMHost -Location $cluster | Get-Datastore | %{

   

        $info = "$($_.Datacenter),"

        $info += "$($cluster.Name),"

        $info += "$($_.Name),"

        $capacity = [math]::Round($_.capacityMB/1024,2)

        $info += "$($capacity),"

        $provision = ([math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,2))

        $info += "$($provision),"

        $Available = ([math]::Round(([math]::Round($_.capacityMB/1024,2)) - ([math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,2)),2))

        $info += "$($available),"

        $avail = ([math]::Round(([math]::Round($_.capacityMB/1024,2)) - ([math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,2)),2))

        $capacity = ([math]::Round($_.capacityMB/1024,2))

        $percent = "{0:N2}" -f (($avail/$capacity)*100)

        $info +=  "$($percent),"

        [void]$report.add($info)

    }

}

$html += "<br><br><br>"

$html += ($report | ConvertFrom-Csv -Header "DataCenter","Cluster", "Name", "Capacity", "Provisioned", "Available", "PctFree" | sort-object  "Available" -descending) | convertto-html -head $style

$html > Report.html

</pre>

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could try something like this

$html = ''

#header info

    $style = "<style>BODY{font-family: Arial; font-size: 10pt;}"

    $style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"

    $style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"

    $style = $style + "TD{border: 1px solid black; padding: 5px; }"

    $style = $style + "</style>"

$report = @()

foreach($cluster in Get-Cluster){

    Get-VMHost -Location $cluster | Get-Datastore | %{

        $info = '' | Select DataCenter,Cluster,Name,Capacity,Provisioned,Available,PctFree

        $info.DataCenter = $_.Datacenter.Name

        $info.Cluster = $cluster.Name

        $info.Name = $_.Name

        $capacity = [math]::Round($_.capacityMB/1024,2)

        $info.Capacity = $capacity

        $provision = ([math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,2))

        $info.Provisioned = $provision

        $info.Available = $capacity - $provision

        $percent = "{0:P2}" -f (($capacity - $provision)/$capacity)

        $info.PctFree =  $percent

        $report += $info

    }

}

$html += "<br><br><br>"

$report | Sort-Object -Property Available -Descending | convertto-html -head $style > Report.html


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

You're building $info as a string, not as an object with propertiies.

The resulting CSV only has one property, hence the Sort-Object doesn't know of any property named Available


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could try something like this

$html = ''

#header info

    $style = "<style>BODY{font-family: Arial; font-size: 10pt;}"

    $style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"

    $style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"

    $style = $style + "TD{border: 1px solid black; padding: 5px; }"

    $style = $style + "</style>"

$report = @()

foreach($cluster in Get-Cluster){

    Get-VMHost -Location $cluster | Get-Datastore | %{

        $info = '' | Select DataCenter,Cluster,Name,Capacity,Provisioned,Available,PctFree

        $info.DataCenter = $_.Datacenter.Name

        $info.Cluster = $cluster.Name

        $info.Name = $_.Name

        $capacity = [math]::Round($_.capacityMB/1024,2)

        $info.Capacity = $capacity

        $provision = ([math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,2))

        $info.Provisioned = $provision

        $info.Available = $capacity - $provision

        $percent = "{0:P2}" -f (($capacity - $provision)/$capacity)

        $info.PctFree =  $percent

        $report += $info

    }

}

$html += "<br><br><br>"

$report | Sort-Object -Property Available -Descending | convertto-html -head $style > Report.html


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

0 Kudos
aaronmiller85
Enthusiast
Enthusiast
Jump to solution

Thanks againg LucD, you are truely a hero in the powercli world

0 Kudos