Mazdajai
asked on
Powershell Nested For loop
I have two column of data listed as following.
I am trying to write a script to display the following -
My code does not work for obvious reason, is it possible to write it in a loop statement?
A B
50 esx10v
51 esx11v
52 esx12v
53 esx13v
54 esx14v
55 esx15v
56 esx16v
57 esx17v
58 esx18v
59 esx19v
60 esx20v
61 esx21v
I am trying to write a script to display the following -
esx10v has 50
esx11v has 51
....
esx21v has 61
My code does not work for obvious reason, is it possible to write it in a loop statement?
$ips=50..61;$vms=10..21;
foreach ($vm in $vms) {
foreach ($ip in $ips) {
write-host esx"$vm"v has $ips
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the feedback. The mulch-dimensional array is interesting but how can I scale in a csv?
Sedgwick,
i am not familiar with the -f switch. This works fine in lab for displaying, but when I remove the write-host and attempt to run it as a script, it did not execute. Any idea or documentation?
Sedgwick,
i am not familiar with the -f switch. This works fine in lab for displaying, but when I remove the write-host and attempt to run it as a script, it did not execute. Any idea or documentation?
what exactly are you trying to do?
You either have to write it to the screen out write each iteration to a new variable
Those are your options
You either have to write it to the screen out write each iteration to a new variable
Those are your options
ASKER
Mahoney-84,
Is there a way to append string before $ips[$x] ? It seems to changed the output of my code when I add $ips[$x].
For example -
Is there a way to append string before $ips[$x] ? It seems to changed the output of my code when I add $ips[$x].
For example -
$ips=50..61
$vms=10..21
$x=0;Foreach($vm in $vms){write-host $vm "has" "10.10.10."$ips[$x];$x++}
$ips=50..61
$vms=10..21
$x=0;Foreach($vm in $vms){write-host $vm "has" 10.10.10.$ips[$x];$x++}
ASKER
I am trying to use it as a script for automation, column A is the last octet of an ip address.
Hmmm?? - Need a little more info to help you. Your original question - you needed to format two columns in a specific format (string) - We gave you that answer.
If you want to do something else with the information -spin up a new question or give us some more info
If you don't want to write the output to the screen, then you have to write it to a multi dimensional array, a hash table, or each item to a variable.
If you want to do something else with the information -spin up a new question or give us some more info
If you don't want to write the output to the screen, then you have to write it to a multi dimensional array, a hash table, or each item to a variable.
$ips=50..61
$vms=10..21
$x=0;Foreach($vm in $vms){$mytempvariable = $vm "has" $ips[$x];$x++;Do-something $mytempvariable}
ASKER
Will do. Thanks.
If you want to use a CSV as input:
<# CSV file format:
IP;VM
50;esx10v
51;esx11v
#>
import-csv 'VMs.csv' | % { write-host "$($_.VM) has IP $($_.IP)" }
You can use $_.VM and $_.IP for the current values of the CSV.
Is there a way to append string before $ips[$x] ? It seems to changed the output of my code when I add $ips[$x].I missed this comment between page refreshes - sorry
For example -
$ips=50..61;
$vms=10..21;
$i=0
$vms | %{
write-host ("esx{0}v has 10.10.10.{1}" -f $_, $ips[$i++])
}
ASKER
Open in new window
Open in new window