Reputation: 3524
First, this is how my csv look like
ip,Component,User,Pwd,type
172.22.0.67,desktopstudio,Administrator,Activlan2015,xendesktop
10.10.10.0,controller,Administrator,Activlan2015,xendesktop
172.22.0.67,storefront,Administrator,Activlan2015,xendesktop
10.10.10.0,desktopdirector,Administrator,Activlan2015,xendesktop
172.22.0.2,licenseserver,Administrator,Activlan2015,xenapp
And this is my script :
$Csv = "C:\springfield\Citrix\CitrixComposants.csv"
$data = Import-Csv $Csv
Foreach ($Server in $Data)
{
Import-module C:\springfield\Citrix\CitrixDeploymentActivlanModule.ps1
Deploy-Citrix -component $Server.component -ip $Server.ip -username $Server.user -pwd $Server.Pwd -type $Server.type
}
When i execute this code, this is how function take parameters :
Deploy-Citrix -component desktopstudio -ip 172.22.0.67 ......
Deploy-Citrix -component controller -ip 10.10.10.0 ......
Deploy-Citrix -component storefront -ip 172.22.0.67 ......
Deploy-Citrix -component desktopdirector -ip 10.10.10.0 ......
Deploy-Citrix -component licenseserver -ip 172.22.0.2 ......
I would like to combine "Component" value, when the ip is the same.
In this CSV, we have three unique ip 172.22.0.67, 10.10.10.0 and 172.22.0.2, and i want to combine "Component" value for each unique IP, to execute my Foreach loop 3 time only :
Deploy-Citrix -component desktopstudio,storefront -ip 172.22.0.67 ......
Deploy-Citrix -component controller,desktopdirector -ip 10.10.10.0 ......
Deploy-Citrix -component licenseserver -ip 172.22.0.2 ......
My function can already take array of string :
Function Deploy-Citrix {
[cmdletBinding(SupportsShouldProcess=$True)]
Param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string[]]$component,
....
)
How can i do that?
Thank you
Upvotes: 0
Views: 76
Reputation: 36277
Are you familiar with piping to Group?
$Data | Group IP
That will group things by the IP property, and should spit back:
Count Name Group
----- ---- -----
2 172.22.0.67 {@{ip=172.22.0.67; Component=desktopstudio; User=Administrator; P...
2 10.10.10.0 {@{ip=10.10.10.0; Component=controller; User=Administrator; Pwd=A...
1 172.22.0.2 {@{ip=172.22.0.2; Component=licenseserver; User=Administrator; Pw...
The Group property there will have a record for each record in $data that matches that group's IP value. So let's pipe that into a ForEach and just for s***s and giggles we'll perform a -join operation on the component values for that group's records:
$data | group ip | ForEach{$_.group.component -join ","}
desktopstudio,storefront
controller,desktopdirector
licenseserver
Huh, looks like what you were trying to do. Ok, cool, so how do we work that into your purposes?
Foreach($Grouping in ($data|group ip)){
$group = $Grouping.group
$comps = $group.component -join ","
Deploy-Citrix -component $comps -ip $group[0].ip -username $group[0].user -pwd $group[0].Pwd -type $group[0].type
}
Upvotes: 1