0

Trying to import a csv file with list of users and export a csv file with these users + the groups they are member of..

I tried the script from this Topic: Import list of users - Export List of users and Groups

But it didn't work.

Example of my user list file (dab.csv):

ID,FName,LName
AAA062, Emil,Øigaard
193063, Henning,Ørnbak
ZGO064, Peter,Ørskov Madsen
193065, Claus,Ørsted
193066, Ole,Ørsted
193067, Marianne,Østengen
BCZ068, Preben,Østerfelt
193069, Anders,Østergaard
193069, Nigel, Jones
BAB369, Steven, Saunders
193024, Leyla,Özalp
193025, Jale Atabey,Özberk
ZDA026, Murat,Özbey
193027, Paul,Özcan
193028, Mehmet,Simanyayi
193029, Atilla,Özdemir

That's the error i get:

Get-AdUser : The search filter cannot be recognized At line:2 char:29 + ForEach-Object -pv user { Get-AdUser -filter "displayname -eq '$($_.username)' ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
DabServ
  • 1
  • 1

1 Answers1

0

The syntax $_.username expects a column named username in your csv

Powershell also will not expand .property inside double-quotes, so it stops at $_. You can fix by using $($_.property)

if you only have first and last names, then try using a filter for those properties: "GivenName -like '$($_.Fname)' -and surname -like '$($_.LName)'"

And to get the groups in a similar way to the linked question

$csv = Import-Csv C:\Users.csv

# get the groups for each user
$Report = ForEach ($row in $csv) { 
  $ADUser = Get-ADUser -Filter "GivenName -like '$($row.Fname)' -and surname -like '$($row.LName)'" -properties memberof
  $ADUser.MemberOf | 
    Get-ADGroup | 
    Select @{n='Username';e={$ADUser.SamAccountName}},Name
}

# output to csv
$Report | Export-Csv C:\folder\output.csv
Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16