我有以下PowerShell代码:
[xml]$xml = Get-Content uccxResourceList_forReference.xml
$xml.resources.resource |
Select-Object firstname, lastname, extension,
@{Name="Team"; Expression={($_.team.name)}} |
Sort Team |
Format-Table
生成如下表:
firstName lastName extension Team --------- -------- --------- ---------------- Homer Simpson 1000 SafetyInspectors Frank Grimes 1001 SafetyInspectors Lenford Leonard 1002 SafetyInspectors Carlton Carlson 1003 SafetyInspectors Montgomery Burns 2000 Executives Waylon Smithers 2001 Executives
我想做的是将每个团队输出到自己的文件中。因此,不只是最后一个简单的| Out-File teamlist.txt
,而是我想输出一个文本文件,其中包含所有“ SafetyInspectors”和另一个包含所有“ Executives”的文件。
我知道我可以通过随后的foreach
循环来完成此操作,但是我认为它也可以在管道中完成,而我只是不知道该怎么做。
答案 0 :(得分:1)
我希望输出到一个csv文件(可以轻松地再次导入),所以:
[xml]$xml = Get-Content uccxResourceList_forReference.xml
$xml.resources.resource |
Select-Object firstname, lastname, extension,
@{Name="Team"; Expression={($_.team.name)}} |
Group-Object Team | ForEach-Object {
$_.Group | Export-Csv ("{0}.csv" -f $_.Name) -NoTypeInformation
}
应返回如下内容:
> gc .\Executives.csv
"firstName","lastName","extension","Team"
"Montgomery","Burns","2000","Executives"
"Waylon","Smithers","2001","Executives"
> gc .\SafetyInspectors.csv
"firstName","lastName","extension","Team"
"Homer","Simpson","1000","SafetyInspectors"
"Frank","Grimes","1001","SafetyInspectors"
"Lenford","Leonard","1002","SafetyInspectors"
"Carlton","Carlson","1003","SafetyInspectors"