以CSV格式排列数据

时间:2017-03-09 21:38:20

标签: powershell active-directory export-to-csv

我有一个PS脚本,可以查询AD的成员。工作得很好,除了输出首先是用户信息,然后是组名。我想在第一列中的组名。我试过在$Group.Name循环中移动foreach。没有帮助。

到目前为止,这是代码。

$groups = Get-ADGroup -Filter * -SearchBase "OU=mygroup,OU=AllGroups,DC=***,DC=local"

$Table = @()

$Record = @{
  "Group Name" = ""
  "Name" = ""
  "Username" = ""
}

foreach ($Group in $Groups) {
  $Arrayofmembers = Get-ADGroupMember -Identity $Group -Recursive # | select name,samaccountname

  foreach ($Member in $Arrayofmembers) {
    $Record."Name" = $Member.Name
    $Record."UserName" = $Member.SamAccountName
    $Record."Group Name" = $Group.Name
    $objRecord = New-Object PSObject -property $Record
    $Table += $objrecord
  }
}

$Table | Export-Csv "D:\logs\AlfrescoUsers.csv" -NoTypeInformation

1 个答案:

答案 0 :(得分:2)

使属性哈希表成为有序哈希表:

$Record = [ordered]@{
  "Group Name" = ""
  "Name" = ""
  "Username" = ""
}