将数据输出到CSV [英] Output Data to CSV
本文介绍了将数据输出到CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我如何输出CSV格式的数据吗?我想拥有它,以便清楚地显示哪些用户属于哪个组:
Can someone please tell me how I can output data in a CSV format? I want to have it so that it clearly shows what users belong to what group:
$site = Get-SPSite http://www.dev.com
$groups = $site.RootWeb.sitegroups
foreach ($grp in $groups) {
"Group: " + $grp.name
foreach ($user in $grp.users) {
" User: " + $user.name
}
}
$site.Dispose()
推荐答案
要将数据导出到CSV,请使用 Export-Csv
.但是请注意,Export-Csv
将对象的属性导出为CSV字段,因此您必须相应地准备数据.
For exporting data to CSV use Export-Csv
. Note, however, that Export-Csv
exports the properties of objects as the CSV fields, so you have to prepare your data accordingly.
$groups | ForEach-object {
$grp = $_
$grp.Users | ForEach-Object {
New-Object -Type PSObject -Property @{
'User' = $_.Name
'Group' = $grp.Name
}
}
} | Export-Csv 'C:\path\to\output.csv' -NoType
如果您希望每组一行,并且所有用户都在一个字段中,则可以执行以下操作:
If you want one line per group with all the users in a single field, you could do something like this:
$groups | ForEach-Object {
New-Object -Type PSObject -Property @{
'Group' = $_.Name
'Users' = $_.Users -join '/'
}
} | Export-Csv 'C:\path\to\output.csv' -NoType
这篇关于将数据输出到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文