KevinD
KevinD

Reputation: 103

Link two foreach loops

I'm struggling with a situation where I got two foreach loops.

Here's an example:

foreach($hostname in $csv) 
{
     foreach($dom in $domain)
     {
           $x = ($import -split '=')[1].substring(0)
           & $exe $arg1':'$hostname'.m'$dom'.local'
     }
}

The script should execute my application like this (just an example):

EXAMPLE.exe /h:examplehostname1.mEXAMPLEDOMAIN.local

So every hostname got only one domain. When I execute the script like this, it writes out several different hostnames (how it should be), but every hostname got the same domain (how it shouldn't be). So how can I 'link' those two foreach loops?

Upvotes: 0

Views: 653

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174690

If you have two arrays of corresponding hostnames and domains, like so:

$csv = @('host1','host2','host3')
$doms = @('domain1','domain2','domain3')

use a for loop and index into each array:

for($i = 0; $i -lt $csv.Count; $i++){
  $hostname = $csv[$i]
  $domain   = $doms[$i]
  "{0}.{1}.com" -f $hostname,$domain
}

Result:

host1.domain1.tld
host2.domain2.tld
host3.domain3.tld

Upvotes: 2

Related Questions