Output Data to CSV

旧巷老猫 提交于 2019-12-02 08:35:10

问题


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()

回答1:


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


来源:https://stackoverflow.com/questions/30356587/output-data-to-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!