0

good day to all. I've been trying to get information about groups and subgroups in Active Directory I've tried many variants like the one below,

What I essentially need is, to get a CSV of all the groups in AD that contain "infolink" in their name, and the columns I need are:

  • GiveName
  • SN
  • Username
  • Mail
  • Group
  • ManagedBy

But no matter how I put it I only get some of the things I need. Does someone already have a bit of code that could make my life a bit easier? If so, I'd be immensely grateful.

    Import-Module ActiveDirectory

    $Groups = (Get-AdGroup -filter * | Where {$_.name -like "*Infolink*"} |         select name -ExpandProperty name)

    $Table = @()

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


    Foreach ($Group in $Groups) {

      $Arrayofmembers = Get-ADGroupMember -identity $Group -recursive |         select name,samaccountname

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

      }
    }

    $Table | export-csv "D:\Infolink.csv" -NoTypeInformation
Mikev
  • 2,012
  • 1
  • 15
  • 27
Nahuster
  • 13
  • 6
  • 1
    use the `SamAccountName` to look them up with `Get-ADUser`. You will get more infos than with `Get-ADGroupMember` – T-Me Mar 04 '19 at 16:04
  • Could you please show me how that would go in that peace of code? I'm quite new to this $Arrayofmembers = Get-SamAccountName -identity $Group -recursive | ? – Nahuster Mar 04 '19 at 17:01

1 Answers1

0

Try this,

Import-Module ActiveDirectory
$groups = Get-ADGroup -Filter "name -like '*Infolink*'" -Properties ManagedBy
$table = foreach ($group in $groups) {
    $Arrayofmembers = Get-ADGroupMember -Identity $group -Recursive
    $ArrayofmembersUsers = $Arrayofmembers | Get-ADUser -Properties Mail
    $ArrayofmembersUsers | Add-Member -Name GroupInfoLinkName -MemberType NoteProperty -Value $group.Name -Force
    $ArrayofmembersUsers | Add-Member -Name GroupInfoLinkManageBy -MemberType NoteProperty -Value $group.ManagedBy -Force
    $ArrayofmembersUsers
}
$table | Select-Object -Property GivenName, SurName, SamAccountName, Mail, GroupInfoLinkName, GroupInfoLinkManageBy | Export-Csv "D:\Infolink.csv" -NoTypeInformation

Some Pointers..

  • Use the Filter on Get-ADGroup else you're getting all groups in AD and then filtering.
  • PSObject are great but if your Object already has the majority of the properties you require then Add-Member is helpful to add 1 or 2 more.
  • Many cmdlets have a Properties parameter, you'll see I've used this to include properties that were not included by default. Unfortunately Get-ADGroupMember is not one of those cmdlets so piping to Get-ADUser helps provide a workaround.
jfrmilner
  • 1,458
  • 10
  • 11