Creating list of users in AD groups. Input is a CSV file of groups, content per line is DN, Name, GroupType, GroupScope - extracted using ‘get-qadgroup’. Code builds one output file per input group. each file has a unique line for the group, and then one line for each member. First column of output file named ‘Level’ and the content should be “GROUP” for the first data line, and 'Member" for the remainder. Here’s the problem - I’m using ‘get-qadgroupmember’ and performing a check with foreach{if($_.type -eq “group”) and adding a line to an output text file if the member is a group = that works. My select-object with a calculated property is not working - instead of the word “Member” appearing in the first column of the output CSV, I am getting nothing - no errors either. Please review my code, and thanks.
$seed = "Level,DN,DisplayName,ClassName,Type,LogonName" # .csv file seed record
# find members in security groups
$fn = 1 # counter for file names
$csv = import-csv list.of.secgroups.csv # contains group list - DN, Name, Grouptype, Groupscope
foreach ($group in $csv)
{
Add-Content file$fn.secgroups.output.csv "$seed" #prep output csv
# build unique first line in file for group
$grp = new-object psobject
$grp | add-member -membertype NoteProperty -name "Level" -value "GROUP"
$grp | Add-Member -MemberType NoteProperty -name "DN" -value $group.DN
$grp | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $group.name
$grp | export-csv file$fn.secgroups.output.csv -append -notype -force
# get members from each group
get-qadgroupmember $group.dn -sizelimit(0) -ea Continue |
foreach{if($_.type -eq "group") {add-content output.list.txt "$file$fn $_ contains groups"} else {$_}
Select-Object @{Name="Level"; Expression = {"Member"}}, DN, DisplayName, Classname, Type, LogonName } `
| export-csv file$fn.secgroups.output.csv -delim "," -append -notype -force
$fn = $fn + 1
}
It doesn’t appear as if anything is being piped to Select-Object. Am I reading that right? The ForEach contains an If{}, and then an Else{}, and then the Select with nothing in front of it?
That’s what makes me think Select is getting nothing. I kind of expected a pipe character after the closing of the Else. But I’m not sure if that’s what you intended or not?
Thanks for the reply - I’m getting back into PowerShell after a long absence.
The Select is getting everything except the calculated property ‘Member’. My output CSV file has the properties for each user The curly brace after the select-object is the closing one from the foreach{if(… statement - and there is also a backtick (`) after that brace.
This worked when it was simply get-qadgroupmember | Select-object | export-csv. Once I inserted the ‘foreach{if(’ is when I started having issues.
There might be something bad in your pasted code, then, because I’m not seeing the curly brackets line up the way you’re describing. In any event, your custom property hash table is absolutely correct, so if it’s not working, then I suspect Select-Object isn’t getting what you think. As I noted, you don’t have a pipe character in front of Select-Object - that’s different from your simpler example. Try reformatting your code a bit and confirm that it’s the way you think.
Thanks for the insight - I got it to work with the following mod - moved the foreach{if( line after the select-object and before the export-csv - piped all the way through