Subscribe
Accepted Solution

PS Newbie -- Sort by LunID. What am I missing?

So I did this:

Get-NaIgroup | ForEach-Object {$_.Initiators} | Get-NaLunMapByInitiator | sort ascending | FL InitiatorGroup,LunID,Path

I'm expecting

InitiatorGroup : igrptst2
LunId          : 255
Path           : /vol/vol1/tstqtree/tstqtree2
InitiatorGroup : igrptst
LunId          : 255
Path           : /vol/vol1/tstqtree/tstqtree
InitiatorGroup : igrptst
LunId          : 254
Path           : /vol/vol1/test/testlun
Instead I'm getting (all same initiator group):
LunId: 162
LunId: 163
LunId: 158
LunId: 159
LunId: 160
LunId: 164
LunId: 168
LunId: 169
LunId: 171
I went as far as

Get-NaIgroup | ForEach-Object {$_.Initiators} | Get-NaLunMapByInitiator | sort ascending | FL LunId,InitiatorGroup,Path

Wondering if I needed LunId first to sort by. 

In the grand scheme of things, the desired end result is to query LunId's find the last LunId and subtract one (-1) for a new LUN/LUN mapping. 

Is what I want possible?  What am I missing?  Where have I fallen short? 

Thanks!!

Re: PS Newbie -- Sort by LunID. What am I missing?

You just have to tell it which property to sort on:

PS C:\> Get-NaIgroup | ForEach-Object {$_.Initiators} | Get-NaLunMapByInitiator | sort -Property LunId | Format-Table LunId,InitiatorGroup,Path -AutoSize

LunId InitiatorGroup                                               Path
----- --------------                                               ----
    0 viaRPC.iqn.1991-05.com.microsoft:x3550rre8.rtprre.testdomain /vol/vol1/lun4
    0 viaRPC.iqn.1991-05.com.microsoft:x3550rre7.rtprre.testdomain /vol/vol1/testlun
    1 viaRPC.iqn.1991-05.com.microsoft:x3550rre7.rtprre.testdomain /vol/vol1/testlun2
    2 viaRPC.iqn.1991-05.com.microsoft:x3550rre8.rtprre.testdomain /vol/vol1/lun3
    2 viaRPC.iqn.1991-05.com.microsoft:x3550rre7.rtprre.testdomain /vol/vol1/lun3

Or you could write a simple script to return the next available LUN ID.

Re: PS Newbie -- Sort by LunID. What am I missing?

Oh it's so simple (or obvious).  Makes me think it's one of those things I should have known...but still learning.

How much I get next available LUN ID?  I know the default behavior when mapping LUNs is to go 0, 1, 2, 3.  We've actually started a 255, 254, 253 etc...

So I need last LUD ID -1 I guess...

Thanks again!!  Very much appreciated! 

Re: PS Newbie -- Sort by LunID. What am I missing?

-Descending instead of -Ascending.

J

Re: PS Newbie -- Sort by LunID. What am I missing?

If I use my above script with -descending instead of -ascending, same behavior.  

Re: PS Newbie -- Sort by LunID. What am I missing?

I noticed in your previous code, you had specified "ascending" in the "Sort-Object" flags.  An ascending sort is the default, and cannot be specified by flag.  To sort in descending order, please make sure that "-descending" flag includes the "-" at the beginning of the word.

Below are 3 PS snippets that offer different sorted output.

 

#Provides a list of LUNs sorted by LunID in ascending order without respect to the igroup name

Get-NaIgroup | ForEach-Object {$_.Initiators} | Get-NaLunMapByInitiator | Sort-Object -Property LunId | Format-Table LunID,InitiatorGroup,Path -autosize

#Provides a list of LUNs sorted by LunID in descending order without respect to the igroup name

Get-NaIgroup | ForEach-Object {$_.Initiators} | Get-NaLunMapByInitiator | Sort-Object -Property LunId -descending | Format-Table LunID,InitiatorGroup,Path -autosize

#Provides a list of LUNs sorted by LunID in descending order and by igroup name in ascending order

$igroups = Get-NaIgroup | Sort-Object -Property InitiatorGroupName

$table = foreach ($igroup in $igroups) {
    $igroup.Initiators | Get-NaLunMapByInitiator | Sort-Object -Property LunId -Descending
}

$table | Format-Table LunID,InitiatorGroup,Path -autosize

Please let me know which of these (if any) resolves the problem.

Bill

Re: PS Newbie -- Sort by LunID. What am I missing?

Alternatively, if you just want the number that is one less than the lowest LunID in a give igroup, the code would be...

$igroups = Get-NaIgroup | Sort-Object -Property InitiatorGroupName

foreach ($igroup in $igroups) {
    $sortedLunMapping = $igroup.Initiators | Get-NaLunMapByInitiator | Sort-Object -Property LunId -Descending
    $nextLunId = ($sortedLunMapping[0].LunID) - 1
    Write-Host "For $($igroup.InitiatorGroupName), the next LUN ID is $nextLunId"
}

Re: PS Newbie -- Sort by LunID. What am I missing?

WOW!  Thank you for that!  Much appreciated!  I'll give it a whack and see what happens!

Thanks! 

Re: PS Newbie -- Sort by LunID. What am I missing?

I realized last night that I sorted the LunID in descending order.  I think the LunID sort should be in ascending order to get the correct result.  Sorry for the error.  So, the updated code would be....

$igroups = Get-NaIgroup | Sort-Object -Property InitiatorGroupName

foreach ($igroup in $igroups) {
    $sortedLunMapping = $igroup.Initiators | Get-NaLunMapByInitiator | Sort-Object -Property LunId
    $nextLunId = ($sortedLunMapping[0].LunID) - 1
    Write-Host "For $($igroup.InitiatorGroupName), the next LUN ID is $nextLunId"
}

Good luck.  Please let me know whether it works.

Re: PS Newbie -- Sort by LunID. What am I missing?

It doesn't work...

I keep getting -1.

I'll continue to hack away at it.


Thanks!