[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Powershell Nested For loop

Posted on 2013-05-23
14
Medium Priority
?
562 Views
Last Modified: 2013-05-24
I have two column of data listed as following.
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

Open in new window


I am trying to write a script to display the following -
esx10v has 50
esx11v has 51
....
esx21v has 61

Open in new window


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
    }
}

Open in new window

0
Comment
Question by:Mazdajai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 1200 total points
ID: 39192495
$ips=50..61;
$vms=10..21;
$i=0
$vms | %{
write-host ("esx{0}v has {1}" -f $_, $ips[$i++])
}

Open in new window

0
 
LVL 3

Assisted Solution

by:Mahoney-84
Mahoney-84 earned 600 total points
ID: 39192623
$ips=50..61
$vms=10..21
$x=0;Foreach($vm in $vms){write-host $vm "has" $ips[$x];$x++}
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 39192631
Perfect! Do you mind explain the difference?

("esx{0}v has {1}" -f $_, $ips[$i++])

Open in new window

{write-host $vm "has" $ips[$x];$x++} 

Open in new window

0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
LVL 42

Assisted Solution

by:Meir Rivkin
Meir Rivkin earned 1200 total points
ID: 39192660
$vms | %{ -  means loop $vms
in powershell each item in the object you iterate is stored in default object called $_
"esx{0}v has {1}" is the string being formatted (via -f switch), {0} and {1} are key holders to place the formatting arguments.
so the 1st argument replaces {0}, 2nd argument replaces {1} and so on.
in .Net u use String.Format() which has the same idea.
i use $i as variable counter so for each item in $vms i take the matched item in $ips (by index)
0
 
LVL 3

Assisted Solution

by:Mahoney-84
Mahoney-84 earned 600 total points
ID: 39192673
Sedgwick declared a string and then declared how to format it.

I just dumped my variables to the screen in a specific format.

Basically the same - but Sedgwick's will be more useful in complex scenarios
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 200 total points
ID: 39192745
In more complex scenarios it would be better to use a CSV file or custom object to store the relation, or at least a twodimensional array like
$vms = @( (50, 'esx10v'), (51, 'esx11v') <# aso #>)
$vms | % { write-host "$($_[1]) has IP $($_[0])" }

Open in new window

0
 
LVL 21

Author Comment

by:Mazdajai
ID: 39194847
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?
0
 
LVL 3

Expert Comment

by:Mahoney-84
ID: 39194890
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
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 39194900
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 -

$ips=50..61
$vms=10..21
$x=0;Foreach($vm in $vms){write-host $vm "has" "10.10.10."$ips[$x];$x++} 

Open in new window

$ips=50..61
$vms=10..21
$x=0;Foreach($vm in $vms){write-host $vm "has" 10.10.10.$ips[$x];$x++} 

Open in new window

0
 
LVL 21

Author Comment

by:Mazdajai
ID: 39194910
I am trying to use it as a script for automation, column A is the last octet of an ip address.
0
 
LVL 3

Expert Comment

by:Mahoney-84
ID: 39194937
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.

$ips=50..61
$vms=10..21
$x=0;Foreach($vm in $vms){$mytempvariable =  $vm "has" $ips[$x];$x++;Do-something $mytempvariable}

Open in new window

0
 
LVL 21

Author Comment

by:Mazdajai
ID: 39194970
Will do. Thanks.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39194982
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)" }

Open in new window

You can use $_.VM and $_.IP for the current values of the CSV.
0
 
LVL 3

Expert Comment

by:Mahoney-84
ID: 39195024
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 -
I missed this comment between page refreshes - sorry
$ips=50..61;
$vms=10..21;
$i=0
$vms | %{
write-host ("esx{0}v has 10.10.10.{1}" -f $_, $ips[$i++])
}

Open in new window

0

Featured Post

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question