首先,这就是我的csv的样子
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
这是我的剧本:
$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
}
当我执行此代码时,这就是函数采用参数的方式:
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 ......
我想结合“组件”值,当ip是相同的时候。
在这个CSV中,我们有三个唯一的ip 172.22.0.67,10.10.10.0和172.22.0.2,我想为每个唯一的IP组合“Component”值,只执行我的Foreach循环3次:
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 ......
我的函数已经可以获取字符串数组:
Function Deploy-Citrix {
[cmdletBinding(SupportsShouldProcess=$True)]
Param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string[]]$component,
....
)
我该怎么做?
谢谢
答案 0 :(得分:1)
您熟悉管道组吗?
$Data | Group IP
那将按IP属性对事物进行分组,并且应该吐回来:
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...
Group属性将在$ data中的每条记录中记录与该组的IP值相匹配的记录。因此,让我们进入ForEach,只是为了s ***和咯咯笑,我们将对该组记录的组件值执行-join操作:
$data | group ip | ForEach{$_.group.component -join ","}
desktopstudio,storefront
controller,desktopdirector
licenseserver
嗯,看起来就像你想要做的那样。好的,很酷,那么我们如何将其用于您的目的呢?
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
}