jrob24
jrob24

Reputation: 454

How to loop through variables?

I have a several scripts that are using PowerCLI to pull info from all of our virtual center servers and dumping into an HMTL file. At this time I have one script setup for each virtual center server, howerver I want to modify it so that I have one main script that will loop through each virtual center and create a HTML for each one instead of having to maintain several different scripts. I have tried declaring each VC as a variable, for example:

$vc = "vc1" , "vc2"

however that only generated one HTML file using the last variable. What would be the best way to accomplish this? Thanks for any help.

Upvotes: 1

Views: 3782

Answers (2)

Jonny
Jonny

Reputation: 2683

Try this:

"Server01","Server02" | ForEach-Object {
  Connect-VIServer -Server $_ -User user -Password password;
}

Also add the rest of the code that you need to apply to each Server with in the ForEach-Object's { }. You can then name each HTML file with the $_ (server name inside the loop) variable as well.

Upvotes: 2

Yanagi
Yanagi

Reputation: 43

foreach is what you want I think

Upvotes: 0

Related Questions