Microsoft Virtualization Discussions

How do you sort Aggr/Vol output

cguldelmco
6,898 Views

I am trying to write a simple script to output the Aggregates and Volumes on my filer but the output aoppears in random order.

How can I get the information sorted?

I cut this together out of the Visio Report script.

Import-Module DataONTAP

$FASName = read-host "Enter the FQDN of your NetApp array"
If ($FASName -eq "") { Write-Host "No selection made, script now exiting." ; exit }

Connect-NaController $FASName -Credential (Get-Credential)
$allAGGR = Get-NaAggr
$allVols = Get-NaVol
$allLUNs = Get-NaLun

Foreach ($aggr in $allAGGR) {

   Write-Host $aggr.Name '        ' ($aggr.sizetotal / 1gb)

   Foreach ($volume in $allVols) {

      If ($volume.ContainingAggregate -eq $aggr.Name) {

         Write-Host '       ' $volume.name '        ' ($volume.sizetotal / 1gb)

      }

   }
}

$pauseit = read-host "Press Enter to exit"

8 REPLIES 8

fjohn
6,898 Views

create an object collection.  In your for-each, append each aggregate object to your collection, say $AggCollection for instance.  Then, pipe the aggregate collection throught sort-object specifying the property to sort on (Name I think in your case) with -Property

$AggCollection | sort-object  -Property Name

In http://communities.netapp.com/docs/DOC-6293  I use foreach to populate an object collection with connection objects.

J

cole
6,898 Views

I think you could also do something along these lines:

Foreach ($aggr in $allAGGR | sort Name) {

......

     Foreach ($volume in $allVols | sort Name) {

     ......

cguldelmco
6,898 Views

Thanks Cole!  Worked like a charm.

Sorry fjohn.  Cole's solution was faster and easier.  😉

I'll try collections on otehr projects

-C

cguldelmco
6,898 Views

Projects where I can spell correctly....

cole
6,898 Views

Both of our responses are the same thing, John was just explaining it in more detail.  Both Get-NaAggr and Get-NaVol return a collection of objects when called without the -Name parameter.  My use of sort is just the short version of what John wrote.  'sort' is an alias for Sort-Object and the -Property parameter can be left off in this example.  It is 'best practice' to use the more verbose version of most cmdlets (as John has done) when writing scripts and saving the short version as I have done for one-liner type scripts.  That is typically easier to maintain long after the script is written.  In the case of 'sort', though, I typically use the short version.

Darkstar
6,898 Views

The difference is in the details.

If you "just" want to sort by aggr name, and then, all volumes inside the aggregate by volume name, then the proposed solution is fine.

If, however, you want to sort all volumes by volume name, regardless of the aggregate they are on, then you will have to create the object collection and sort by that afterwards.

-Michael

fjohn
6,898 Views

In powershell, you always have the option of adding properties to a collection.  I demonstrated this technique in the sample to sort snapshots.  It's more versitile.

J

paleon
6,898 Views

You can also sort the collection objects when they are created....

Your code of:

$allAGGR = Get-NaAggr
$allVols = Get-NaVol
$allLUNs = Get-NaLun

Becomes:

$allAGGR = Get-NaAggr | Sort-Object -Property Name
$allVols = Get-NaVol | Sort-Object -Property Name
$allLUNs = Get-NaLun | Sort-Object -Property Name

Public