Microsoft Virtualization Discussions

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

bdmorrison
7,879 Views

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!!

1 ACCEPTED SOLUTION

cknight
7,878 Views

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.

View solution in original post

10 REPLIES 10

cknight
7,879 Views

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.

bdmorrison
7,853 Views

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! 

fjohn
7,853 Views

-Descending instead of -Ascending.

J

bdmorrison
7,854 Views

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

paleon
7,854 Views

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

paleon
7,854 Views

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"
}

bdmorrison
7,854 Views

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

Thanks! 

paleon
7,854 Views

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.

bdmorrison
7,855 Views

It doesn't work...

I keep getting -1.

I'll continue to hack away at it.


Thanks!

bdmorrison
6,691 Views
$a = Get-NaIgroup | ForEach-Object {$_.Initiators} | Get-NaLunMapByInitiator | sort LunID | Where-Object { $_.LUNID -ne 0 } |  Select-Object -ExpandProperty LUNID -First 1
$b = ($a - 1)

The above is what I came up with, and does in fact work. 

Public