Reputation: 143
Here is the output I am working with:
Pool Name: Pool 2
Pool ID: 1
LUNs: 1015, 1080, 1034, 1016, 500, 1002, 1062, 1041, 1046, 1028, 1009, 1054, 513, 1058, 1070, 515, 1049, 1083, 1020, 1076, 19, 509, 1057, 1021, 525, 1019, 518, 1075, 29, 23, 1068, 37, 1064, 506, 1024, 1026, 1008, 1087, 1012, 1006, 1018, 502, 1004, 1074, 1030, 1032, 39, 1014, 1005, 1056, 1044, 2, 1033, 1001, 16, 1061, 1040, 1045, 1027, 26, 1023, 1053, 1037, 1079, 512, 520, 1069, 1039, 514, 1048, 1082, 523, 508, 524, 517, 522, 1066, 1089, 1067, 529, 528, 1063, 505, 1081, 527, 1007, 1086, 1051, 1011, 1035, 1017, 501, 1003, 1042, 1073, 1085, 1029, 1010, 24, 1013, 1055, 1043, 1059, 52, 1071, 516, 1050, 1084, 1000, 1077, 1060, 1072, 510, 1022, 1052, 526, 1036, 1078, 511, 35, 519, 1038, 521, 1047, 507, 6, 1065, 1025, 1088, 503, 53, 1031, 504
Pool Name: Pool 1
Pool ID: 0
LUNs: 9, 3, 34, 10, 12, 8, 7, 0, 38, 27, 18, 4, 42, 21, 17, 28, 36, 22, 13, 5, 11, 25, 15, 32, 1
Pool Name: Pool 4
Pool ID: 2
LUNs: (this one is empty)
What I would like to do is store each one of the "LUNs:" into their own variables (array?). Then take my number and search for it in all arrays, in this example there are three. If it matches my number for example "34" the program will output Your number is in Pool 1
I know how to pull the LUN lines I need with Regex expressions and I know how to compare the results with an if statement but get lost combining the two and even more lost when thinking about outputting the correct "Pool Name".
EDIT I should add the total number of pools can change as well as the LUN number lists.
Upvotes: 0
Views: 45
Reputation: 200293
Convert the output into a single string, replace colons with equals signs and split the string at double line breaks, then convert the fragments into objects using ConvertFrom-StringData
and New-Object
and split the LUN string into an array:
$data = ... | Out-String
$pools = $data -replace ': +','=' -split "`r`n`r`n" |
% { New-Object -Type PSCustomObject -Property (ConvertFrom-StringData $_) } |
select -Property *,@{n='LUNs';e={$_.LUNs -split ', '}} -Exclude LUNs
With that you can get the pool name of a pool containing a given LUN like this:
$pools | ? { $_.LUNs -contains 34 } | select -Expand 'Pool Name'
Upvotes: 1
Reputation: 8889
I'm sure there's an easier way... Is that what you need?
$Number = 42
$Lun1=1015, 1080, 1034, 1016, 500, 1002, 1062, 1041, 1046, 1028, 1009, 1054, 513, 1058, 1070
$Lun2=9, 3, 34, 10, 12, 8, 7, 0, 38, 27, 18, 4, 42, 21, 17, 28, 36, 22, 13, 5, 11, 25, 15, 32
$Lun3=$null
$Lun1Length=$Lun1.Length
$Lun2Length=$Lun2.Length
$Lun3Length=$Lun3.Length
[Array]$Luns = $Lun1, $Lun2, $Lun3
foreach ($Lun in $Luns)
{
if ($Lun -contains $Number)
{
Switch ($Lun.Length)
{
$Lun1Length {"$Number in Lun1"}
$Lun2Length {"$Number in Lun2"}
$Lun3Length {"$Number in Lun3"}
}
}
}
42 in Lun2
Upvotes: 0