2016-11-29 163 views
0

我想的CSV文件導出爲「ParentGroupName」欄目,「成員名稱」,「顯示名稱」PowerShell的導出CSV的列

目前,它是出口三個DATAS一列。

function getGroups{ 

    $Groups += Get-ADGroup -Filter * -SearchBase "ou=Groups,ou=DCM,ou=NTG,dc=prod,dc=main,dc=ntgov" | Select-Object -ExpandProperty samaccountname 

    return $Groups 
} 

Measure-Command{ 


$Groups = getGroups 
write-host "Groups:" $Groups.Count 

} 

Measure-Command{ 



$date = $(get-date).ToString("dd MMM yyyy") 
$global:FileName = "Active Directory Group Membership - DCM -" + $date 

$stringBuilder = New-Object System.Text.StringBuilder 

foreach ($GroupName in $Groups){ 

     Get-ADGroupMember -Identity $GroupName | Sort-Object $_.SamAccountName | ForEach-Object { 
      $ParentGroupName = (Get-ADGroup -Identity $GroupName).SamAccountName 
      $MemberName = $_.SamAccountName # Member of the Group. 


      if ($_.ObjectClass -eq 'group') { 



       $DisplayName = ' - ' 

      } elseif ($_.ObjectClass -eq 'user') { 


       $a = (Get-ADUser -Identity $MemberName -Properties Displayname) 
       $DisplayName = $a.DisplayName 

      } 



      $null = $stringBuilder.Append("$ParentGroupName, $MemberName, $DisplayName") 


     } 
    } 


outputArray = $stringBuilder.ToString() 


out-file C:\Users\augut\Desktop\$FileName.csv 


outputArray | out-file C:\Users\augut\Desktop\$FileName.csv 
} 

回答

1

通過手動構建CSV文件,您正在爲自己頭痛。這可以通過爲每個需要記錄的屬性創建一個自定義對象構建一個自定義對象,然後將這些對象填充到一個數組中並將該數組導出到一個CSV文件中來簡化。

function getGroups{ 
    $Groups += Get-ADGroup -Filter * -SearchBase "ou=Groups,ou=DCM,ou=NTG,dc=prod,dc=main,dc=ntgov" | Select-Object -ExpandProperty samaccountname 
    return $Groups 
} 
$Groups = getGroups 
write-host "Groups:" $Groups.Count 

$date = $(get-date).ToString("dd MMM yyyy") 
$global:FileName = "Active Directory Group Membership - DCM -" + $date 

$results = @(); 

foreach ($GroupName in $Groups){ 
     Get-ADGroupMember -Identity $GroupName | Sort-Object $_.SamAccountName | ForEach-Object { 
      $ItemProperties = @{ 
       "ParentGroupName" = (Get-ADGroup -Identity $GroupName).SamAccountName; 
       "MemberName" = $_.SamAccountName 
      } 

      if ($_.ObjectClass -eq 'group') { 
       $ItemProperties.Add("DisplayName","-"); 
      } elseif ($_.ObjectClass -eq 'user') { 
       $ItemProperties.Add("DisplayName",(Get-ADUser -Identity $MemberName -Properties DisplayName).DisplayName); 
      } 
      $MyItem = New-Object -TypeName psobject -property $ItemProperties; 
      $Results += $MyItem; 
      $ItemProperties = $null; 
     } 
    } 
$results | export-csv -path "C:\Users\augut\Desktop\$FileName.csv" -NoTypeInformation 
+0

感謝它主要作品接受這似乎爲每個組名和用戶名重複一個名字的顯示名: 例如: mjms \t傑恩Fairnington \t DCM_NTH_FOI TP6 \t傑恩Fairnington \t DCM_NTH_FOI yfair \t傑恩Fairnington \t DCM_NTH_FOI ktimi \t傑恩Fairnington \t zzMinisterVowles_O BXL職員 的職員 amart \t傑恩Fairnington \t DCM OCM科長\t傑恩Fairnington \t DCM_DNH_SPIO_RW FMD \t傑恩Fairnington \t DCM_DNH_SPIO_RW MAT \t傑恩Fairnington \t DCM_DNH_SPIO_RW aleon \t傑恩Fairnington \t DCM OCM首席bqb \t Jayne Fairnington \t DCM OCM Chief of Staffs – GuzziT

+0

@GuzziT我剛剛做了一個小編輯,嘗試新版本。 – alroc

+0

嘗試更新的版本,但沒有變化 – GuzziT