Gajendra D Ambi
Gajendra D Ambi

Reputation: 4233

Use 2 arrays in one loop

I want to use 2 arrays in one loop, but I am failing each time to find out how?

$hosts = "1.1.1.1,2.2.2.2,3.3.3.3"
$vmotionIPs = "1.2.3.4,5.6.7.8,7.8.9.0"
foreach ($host in $hosts) ($vmotionIP in $vmotionIPs)
  New-VMHostNetworkAdapter -VMHost $host-VirtualSwitch myvSwitch `
    -PortGroup VMotion -IP $vmotionIP -SubnetMask 255.255.255.0 `
    -VMotionEnabled $true

I know the above syntax is wrong but I just hope it conveys my goal here.

Upvotes: 0

Views: 1833

Answers (4)

Gajendra D Ambi
Gajendra D Ambi

Reputation: 4233

Thank you all for your answers. I ended up using the do while instead. This allows us to loop through as many as arrays as we want at the same time or include multiple arrays in one loop.

$hosts  = @("1.1.1.1","2.2.2.2","3.3.3.3")
$vmotionIPs  = @("1.2.3.4","5.6.7.8","7.8.9.0")
[int]$n = 0
do
{
$vmhost = $hosts[$n]
$vmotionIP = $vmotionIPs[$n]
New-VMHostNetworkAdapter -VMHost $vmhost-VirtualSwitch myvSwitch -PortGroup VMotion -IP $vmotionIP -SubnetMask 255.255.255.0 -VMotionEnabled $true
$n++
} while ($n -lt $hosts.count)

Upvotes: 0

Gajendra D Ambi
Gajendra D Ambi

Reputation: 4233

Thank you for your information. What you suggested worked for me some other script but i ended up achieving this using the following. first i generated a series of ip addresses like this

$fixed = $host1.Split('.')[0..2]
$last = [int]($host.Split('.')[3])
$max = Read-Host "Maximum number of hosts that you want to configure?"
$max_hosts = $max - 1
$hosts = 
$last..($last + $max_hosts) | %{
[string]::Join('.',$fixed) + "." + $_
}

and then i did

$vMotion1_ip1 = Read-Host "the 1st vmotion ip of the 1st host?"
$fixed = $vMotion1_ip1.Split('.')[0..2]
$last = [int]($vMotion1_ip1.Split('.')[3])
$max_hosts = $max - 1
$vMotions = 
$last..($last + $max_hosts) | %{
[string]::Join('.',$fixed) + "." + $_
}
$first = [string]::Join('.',$fixed) + "." + $_

foreach ($vmhost in $vMotions) {write-host "$vmhost has the following      network ("$first$(($last++))", "255.255.255.0")"}

not exactly like this but something along this way.

Upvotes: 0

Alexander Obersht
Alexander Obersht

Reputation: 3275

The most straightforward way is to use a hashtable:

$hosts = @{
    "1.1.1.1" = "1.2.3.4" # Here 1.1.1.1 is the name and 1.2.3.4 is the value
    "2.2.2.2" = "5.6.7.8"
    "3.3.3.3" = "7.8.9.0"
}

# Now we can iterate the hashtable using GetEnumerator() method.
foreach ($hostaddr in $hosts.GetEnumerator()) { # $host is a reserved name
    New-VMHostNetworkAdapter -VMHost $hostaddr.Name -VirtualSwitch myvSwitch `
        -PortGroup VMotion -IP $$hostaddr.Value -SubnetMask 255.255.255.0 `
        -VMotionEnabled $true
}

Upvotes: 3

Bacon Bits
Bacon Bits

Reputation: 32170

First, your arrays aren't arrays. They're just strings. To be arrays you'll need to specify them as:

$hosts = "1.1.1.1","2.2.2.2","3.3.3.3";
$vmotionIPs = "1.2.3.4","5.6.7.8","7.8.9.0";

Second, $host is a reserved variable. You should avoid using that.

Third, I'm assuming you want the first host to use the first vmotionIP, the second host to use the second vmotionIP, etc.

So, the standard way to do this is to do this:

$hosts = "1.1.1.1","2.2.2.2","3.3.3.3";
$vmotionIPs = "1.2.3.4","5.6.7.8","7.8.9.0";

for ($i = 0; $i -lt $hosts.Count; $i++) {
    New-VMHostNetworkAdapter -VMHost $hosts[$i] `
        -VirtualSwitch myvSwitch `
        -PortGroup VMotion `
        -IP $vmotionIPs[$i] `
        -SubnetMask 255.255.255.0 `
        -VMotionEnabled $true;
}

Or you can use the hashtable method @AlexanderObersht describes. This method changes the least about your code, however.

Upvotes: 3

Related Questions