VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

foreach loop help needed

looking to run a foreach loop with 2 variables here

$vmwareluns = get-content c:\netappluns.txt

$igroup = get-content c:\igroup.txt

foreach ($lun in $vmwareluns) {

     add-nalunmap $lun $igroup

}

but fails

do I need a second loop here?

every line in igroup needs to go with all the lines in $lun

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can use nested ForEach loops

$vmwareluns = get-content c:\netappluns.txt

$igroups = get-content c:\igroup.txt

foreach($group in $igroups){

  foreach ($lun in $vmwareluns) {

     add-nalunmap $lun $igroup

  }

}


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

View solution in original post

0 Kudos
3 Replies
CRad14
Hot Shot
Hot Shot
Jump to solution

Maybe a Do loop might work better

$vmwareluns = get-content c:\netappluns.txt

$igroup = get-content c:\igroup.txt

$i=0

$count= $igroup.count

DO

{


add-nalunmap $lun[$i] $igroup[$i]


$i++


}

Until ($i -eq ($count-1))



Or atleast something like that...


Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
LucD
Leadership
Leadership
Jump to solution

You can use nested ForEach loops

$vmwareluns = get-content c:\netappluns.txt

$igroups = get-content c:\igroup.txt

foreach($group in $igroups){

  foreach ($lun in $vmwareluns) {

     add-nalunmap $lun $igroup

  }

}


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

0 Kudos
tdubb123
Expert
Expert
Jump to solution

Thanks Lucd. That works

0 Kudos