Powershell list users with filtered groups

Triforce2000
Triforce2000 used Ask the Experts™
on
I want to list all users with filtered AD groups, so I want all groups beginning with BLOG.
I've got a CSV with the name of all users.

I've tryed this code:
$users = Import-Csv 'C:\temp\old farm\users.csv' -Delimiter ";"

$Report = @()

foreach ($user in $users){
    $name  = $user.name

    $gebruiker = get-aduser -Filter {(displayname -like $name) -AND (enabled -eq $true)} -Properties *
    $sam = $gebruiker.SamAccountName
    $usergroupcollection = Get-ADPrincipalGroupMembership $sam | ? {$_.name -like "blog*"}
    
   
    
    $usergroupmembership = @()

    foreach ($UserGroup in $usergroupcollection) {
        $GroupDetails = Get-ADGroup -Identity $UserGroup
        $usergroupmembership += $GroupDetails.Name
    }

    $groups = $usergroupmembership -join ','

    $out = New-Object PSObject
    $out | Add-Member -MemberType NoteProperty -Name Name -Value $gebruiker.Name
    $out | Add-Member -MemberType NoteProperty -Name Sam -Value $gebruiker.SamAccountName
    $out | Add-Member -MemberType NoteProperty -Name Groups -Value $groups

    $report += $out
}

$Report | Sort-Object Name | FT -AutoSize

Open in new window


But there is an error on this
Knipsel.PNG
Has anyone a solution for this please?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
The solution is technically rather easy; stop using Name/DisplayName to identify AD users. the DisplayName you're filtering for is not guaranteed to be unique, and you have at least one Name/DisplayName in your csv that matches more than one account.
Use SamAccountName, UserPrincipalName, or the DistinguishedName to uniquely identify users.
Then you have a column "Name" in your csv, which you use in the query to find users with a DisplayName matching said Name, but you're returning a new column "Name", now with the actual Name of the AD Object, which might be totally different from the respective Name in your input csv. That's confusing at best, as there is no real relation between the input csv and the output. You should change the input column to "DisplayName".
Example: AD user with a Name of "Doe, John" and a DisplayName of "John Doe". If your csv "Name" column has "John Doe" in it, it will find the user based on its DisplayName, and return it with the column Name now as "Doe, John".
The case if an input name can not be found in AD is not properly handled.
And there's no need to retrieve the AD group - everything you return (which is only the group's Name) is already present in the objects returned by Get-ADPrincipalGroupMembership.
Finally, adding the "FT -AutoSize" directly in the script prevents you from exporting the script's output to a csv.
Try it like this; it still uses the "Name" column - you'll find it as DisplayName in the output, while the former output "Name" is now called "AD Name".
You can pipe the script's output to "ft -au", or "Out-GridView", or "Export-Csv", or whatever you need for further processing.
$users = Import-Csv 'C:\temp\old farm\users.csv' -Delimiter ';'

$users | ForEach-Object {
	If ($adUsers = Get-ADUser -Filter "(displayname -like '$($_.Name)') -and (enabled -eq 'true')" -Property DisplayName) {
		$adUsers | Select-Object -Property `
			@{n='Sam'; e={$_.SamAccountName}},
			DisplayName,
			@{n='AD Name'; e={$_.Name}},
			@{n='Groups'; e={(Get-ADPrincipalGroupMembership -Identity $_.SamAccountName | Where-Object {$_.Name -like 'blog*'} | Select-Object -ExpandProperty Name) -join ', '}}
	} Else {
		$_ | Select-Object -Property `
			@{n='Sam'; e={'<N/A>'}},
			@{n='DisplayName'; e={$_.Name}},
			@{n='AD Name'; e={'<N/A>'}},
			@{n='Groups'; e={'<N/A>'}}
	}
} | Sort-Object -Property Name

Open in new window

Chirag NagrekarAssociate - Sys Admin

Commented:
What do you get when you print $sam ?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial