Powershell - 附加组名和成员

时间:2013-08-07 16:31:45

标签: powershell

我正在尝试编写一个脚本,该脚本将创建一个包含该组及其成员名称的CSV文件。我的脚本失败并显示错误:

附加对象没有与以下列对应的属性

我确信这是一个简单的错误,我会匆匆忙忙但我需要快速获取数据。

$SearchBase = "OU=Groups,DC=domain,DC=local"

$SPGroups = Get-ADGroup -filter * -SearchBase $SearchBase | select name

Foreach ($i in $SPGroups){
    #$Members += get-ADGroupMember -Identity $i.name | select name
    $i.name | Export-Csv -append -Path c:\bin\membersof.csv
    get-ADGroupMember -Identity $i.name | select name | Export-CSV -append -Path c:\bin\membersof.csv
    }

谢谢!

2 个答案:

答案 0 :(得分:1)

试试这个:

$SearchBase = "OU=Groups,DC=domain,DC=local"

Get-ADGroup -Filter * -SearchBase $SearchBase | % {
  $group = $_.name
  Get-ADGroupMember -Identity $group |
      select @{n='Group';e={$group}}, @{n='Member';e={$_.Name}}
} | Export-CSV 'C:\bin\membersof.csv'

答案 1 :(得分:0)

#Create Custom Data Container
$header = @("Group,Member") #Columns
$temp = @()
$data = @()
$data += $header
$SearchBase = "OU=Groups,DC=domain,DC=local"
Get-ADGroup -filter * -SearchBase $SearchBase | % {
    #For Each Group
    $Groups = $_.name
    get-ADGroupMember -Identity $groups | % {
        #For Each Member
        $Members = $_.Name
        #Add Row
        $temp = "$Groups,$Members"
        $data += $temp
    }
}
#output data to csv
$data | ConvertFrom-Csv | Export-Csv -Path c:\bin\membersof.csv -NoTypeInformation