Another Powershell AD question

Soldato
Joined
8 Mar 2005
Posts
3,939
Location
London, UK
I've been trying to export a list of members from AD groups which match a certain condition EG. *ABC* in the AD group name and I've been struggling to find the correct sequence of piped commands to accomplish this.

I've ended up trying to use the Quest addons, but am still having no luck with the format.

I require the data to be in CSV format similar to;
Groupname,Groupnamedescription,logonname,...

Code:
Add-PSSnapin Quest.ActiveRoles.ADManagement
$Groups = Get-QADGroup *ABC*

foreach ($Group in $Groups) {

$Group | out-file .\Groups.txt -Append
Get-QADGroupMember $Group  | out-file .\Groups.txt -Append 

}

This outputs the detail I need but not in the correct format. Substituting out-file with Export-CSV merely exports a group list and throws out a few errors when the script runs.

Any help/pointers, much appreciated.
Cheers, Paul.
 
Hmm thanks Woogie;however the above outputs similarly to the first script in the OG and then seemingly it repeats the same logon name 3 times.
AD_GroupNAME
Logonname1
Logonname1
Logonname1

As opposed to the desired output format as;
AD_GroupNAME,Logonname1, Fullname...
AD_GroupNAME,Logonname2, Fullname...
AD_GroupNAME,Logonname3, Fullname...
AD_GroupNAME2,Logonname4, Fullname...
AD_GroupNAME2,Logonname5, Fullname...
AD_GroupNAME3,Logonname6, Fullname...

hmm, perhaps I've missed something here.

EDIT -Actually it appears the above script consolidates members to groups so it actually outputs;
AD_GroupNAME, AD_GroupNAME2, ...
Logonname1
Logonname1
Logonname1
Hmm strange;


Cheers, Paul
 
Last edited:
Hmmm, I posted the wrong code but shut my pc down since then... Ill see if I can dig it out again later.
 
In the format you want I am struggling to get the description in there as well due to the way you select that object and me being fairly new to powershell but to get you started the following should help (I really did give you the wrong code on Friday)

Code:
#Variables

$Groups = Get-ADGroup -filter {name -like "GROUPNAME"}
$ADGroupMembers = @()
$ADFileBuild =@()
$FileOut = @()


 FOREACH($Group IN $Groups)
    {
    $ADGroupMembers = Get-ADGroupMember $Group
    
    FOREACH($Member IN $ADGroupMembers)
        {
        $ADFilebuild += $Group.NAME+ " " + $Member.SamAccountName + $Member.NAME
        }
    }

$FileOut = $ADFileBuild
$FileOut |Out-file c:\Groups.csv

That will give you the desired format of

AD_GroupNAME,Logonname1, Fullname...
AD_GroupNAME,Logonname2, Fullname...
AD_GroupNAME,Logonname3, Fullname...
AD_GroupNAME2,Logonname4, Fullname...
AD_GroupNAME2,Logonname5, Fullname...
AD_GroupNAME3,Logonname6, Fullname...
 
Last edited:
Woogies way is a good way of doing it, I just made two small tweaks to add in group description too

Code:
#Variables

$Groups = Get-ADGroup -filter {name -like "*GroupName*"} -properties description
$ADGroupMembers = @()
$ADFileBuild =@()
$FileOut = @()


 FOREACH($Group IN $Groups)
    {
    $ADGroupMembers = Get-ADGroupMember $Group
    
    FOREACH($Member IN $ADGroupMembers)
        {
        $ADFilebuild += $Group.NAME+ " " + $Group.Description + $Member.SamAccountName + $Member.NAME
        }
    }

$FileOut = $ADFileBuild
$FileOut | out-file c:\groups.csv
 
Back
Top Bottom