将广告组详细信息和成员计数导出到csv文件

时间:2018-03-07 08:37:42

标签: powershell

我有一个脚本来收集每组中的AD组和成员数。 任何人都可以帮我导出csv中的输出

$GroupArray = Get-ADGroup -Properties Modified, Members -Filter * -SearchBase "DC=oimstage,DC=corp,DC=com"
 Foreach($Group In $GroupArray)
 {
     Write-Host
     Write-Host "Group Name:"$Group.Name
     Write-Host "Total members:"($Group.Members).count
     Write-Host "Modified date:"$Group.modified
     Write-Host
 } 

1 个答案:

答案 0 :(得分:0)

您希望使用custom PowerShell object导出选择性属性。试试这个 -

$GroupArray = @() 
Get-ADGroup -Properties Modified, Members -Filter * -SearchBase "DC=oimstage,DC=corp,DC=com" | 
ForEach-Object { 
    $obj = New-Object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name "Group Name" $_.Name
    $obj | Add-Member -MemberType NoteProperty -Name "Total members" $_.Members.Count
    $obj | Add-Member -MemberType NoteProperty -Name "Modified date" $_.modified
    $GroupArray += $obj
} 

您可以向自定义对象添加任意数量的属性并将其存储在数组中。然后,您可以通过csv cmdlet将数组导出到Export-Csv。您可以根据自己的要求修改$obj