Acerbity
Acerbity

Reputation: 527

Powershell array of arrays loop process

I need help with loop processing an array of arrays. I have finally figured out how to do it, and I am doing it as such...

$serverList = $1Servers,$2Servers,$3Servers,$4Servers,$5Servers

$serverList | % {
    % {
        Write-Host $_
        }
    }

I can't get it to process correctly. What I'd like to do is create a CSV from each array, and title the lists accordingly. So 1Servers.csv, 2Servers.csv, etc... The thing I can not figure out is how to get the original array name into the filename. Is there a variable that holds the list object name that can be accessed within the loop? Do I need to just do a separate single loop for each list?

Upvotes: 1

Views: 3220

Answers (3)

Acerbity
Acerbity

Reputation: 527

I was able to resolve this issue myself. Because I wasn't able to get the object name through, I just changed the nature of the object. So now my server lists consist of two columns, one of which is the name of the list itself.

So...

$1Servers = += [pscustomobject] @{
        Servername = $entry.Servername
        Domain = $entry.Domain
        }

Then...

$serverList = $usaServers,$devsubServers,$wtencServers,$wtenclvServers,$pcidevServers

Then I am able to use that second column to name the lists within my foreach loop.

Upvotes: 0

JPBlanc
JPBlanc

Reputation: 72610

You can try :

$1Servers = "Mach1","Mach2"
$2Servers = "Mach3","Mach4"
$serverList = $1Servers,$2Servers
$serverList | % {$i=0}{$i+=1;$_ | % {New-Object -Property @{"Name"=$_} -TypeName PsCustomObject} |Export-Csv "c:\temp\$($i)Servers.csv" -NoTypeInformation }

I take each list, and create new objects that I export in a CSV file. The way I create the file name is not so nice, I don't take the var name I just recreate it, so if your list is not sorted it will not work.

It would perhaps be more efficient if you store your servers in a hash table :

$1Servers = @{Name="1Servers"; Computers="Mach1","Mach2"}
$2Servers = @{Name="2Servers"; Computers="Mach3","Mach4"}
$serverList = $1Servers,$2Servers
$serverList | % {$name=$_.name;$_.computers | % {New-Object -Property @{"Name"=$_} -TypeName PsCustomObject} |Export-Csv "c:\temp\$($name).csv" -NoTypeInformation }

Upvotes: 3

Hunter Eidson
Hunter Eidson

Reputation: 1909

Much like JPBlanc's answer, I kinda have to kludge the filename... (FWIW, I can't see how you can get that out of the array itself).

I did this example w/ foreach instead of foreach-object (%). Since you have actual variable names you can address w/ foreach, it seems a little cleaner, if nothing else, and hopefully a little easier to read/maintain:

$1Servers = "apple.contoso.com","orange.contoso.com"
$2Servers = "peach.contoso.com","cherry.contoso.com"
$serverList = $1Servers,$2Servers
$counter = 1

foreach ( $list in $serverList ) {
    $fileName = "{0}Servers.csv" -f $counter++
    "FileName: $fileName"
    foreach ( $server in $list ) {
        "-- ServerName: $server"
    }
}

Upvotes: 1

Related Questions