Link to home
Start Free TrialLog in
Avatar of gd6627
gd6627Flag for United States of America

asked on

powershell

I have this script am working with to extract memebers of a group. The script works but I like to modify it to be more user friendly

This is the script:
import-csv c:\PapersaveGroups.csv | Select -ExpandProperty Groupnames | foreach {Get-QADGroup $_ `
| select name, @{Name="Members";Exp={ $_.Members -join ", "}}} | Export-Csv 'c:\PWGroup CSV Files\getMembersOfgroup.csv'

This is the output:

Name                            Members
TEST GROUP      CN=USer Name,OU=Users,OU=test,OU=test1,OU=test2,DC=domain,DC=net,


I like the output to be like this :

Name of group                Members

Group    1                           displayname
                                             displayname
Group 2                               displayname
                                             

Please help
Avatar of SubSun
SubSun
Flag of India image

Try this code and see if it works for you..
import-csv c:\PapersaveGroups.csv | Select -ExpandProperty Groupnames | foreach {Get-QADGroup $_ `
 | select name,@{Name="Members";Exp={ ($_.Members | Get-QADUser | Select -ExpandProperty DisplayName) -join "`n"}}} | Export-Csv 'c:\PWGroup CSV Files\getMembersOfgroup.csv'

Open in new window

Try this:

$groups = import-csv c:\PapersaveGroups.csv | Select -ExpandProperty Groupnames 
foreach {$group in $groups) {
   $obj = new-object psobject
   $record = get-qadgroup
   $obj | add-member noteproperty -name "Name of Group" -$value $record.name
   $x=0
   foreach ($mbr in $record.members) {
     $x++
     $obj | add-member noteproperty -name "Group$x" -value $mbr.displayname
   }
  $records += $obj
}
$records | Export-Csv 'c:\PWGroup CSV Files\getMembersOfgroup.csv'

Open in new window

Avatar of gd6627

ASKER

subsun:  thanks for the reply . I tried the script you submitted but it resulted on the output of one member only when I ran it . the group had 4 members ?
Avatar of gd6627

ASKER

Sirbounty:

I get the below error when trying to run your code

Add-Member : The SecondValue parameter is not necessary for a member of type "NoteProperty" and should no
t be specified. Do not specify the SecondValue parameter when adding members of this type.
At C:\AppData\Local\Temp\7b1a19ba-ab51-4ce3-9bdb-19b267711a4e.ps1:17 char:21
+    $obj | add-member <<<<  noteproperty -name "Name of Group" -$value $record.name
    + CategoryInfo          : InvalidOperation: (:) [Add-Member], InvalidOperationException
    + FullyQualifiedErrorId : Value2ShouldNotBeSpecified,Microsoft.PowerShell.Commands.AddMemberCommand
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial