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

asked on

Need help with AD Script

I need assistance in getting the below script to export to a csv? I tried using the | and export -csv at the end of the IF statement and it doesn't work? help lease


Import-csv "C:\fnfn.csv" | foreach{
    $UserName=$_.fn + "*"
    $lastName=$_.ln + "*"
    $user =Get-AdUser -filter{Name -like $UserName -and SurName -like $lastName} -Properties * | select *
   
    if($user){
        #all properties and fields are already in the $user if found, just need to filter whichever you need in here (or you can modify the $user 's variable select statement)
        $user | select Name,Surname,SamaccountName
SOLUTION
Avatar of Adam Brown
Adam Brown
Flag of United States of America 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
Avatar of oBdA
oBdA

* You have a csv with first and last name and you still need to use wildcards to find the object? Really?
* You're trying to match the "Name" attribute (which is the AD object's "common name") with the "first name" column of your csv? Really?
Note that first and last name may not be enough to uniquely identify an AD user. You can have hundreds of user objects named "John Doe". To uniquely identify a user object, you need its SamAccountName or User Principal Name.
And never tell the AD cmdlets to retrieve all properties if you throw away nearly all of them anyway.
Try this; it uses the wildcards (which you should get rid of, if possible), and it matches the first name csv column against the givenName attribute.
It'll return a column "Error" which will be set if the user couldn't be found.
$properties = 'samAccountName', 'name', 'givenName', 'sn'
Import-Csv -Path "C:\Temp\fnfn.csv" | ForEach-Object {
	If ($ADUser = Get-ADUser -Filter "(givenName -like '$($_.fn)*') -and (sn -like '$($_.ln)*')" -Properties $properties) {	## may be more than one object!
		$ADUser | Select-Object -Property $properties
	} Else {
		$_ | Select-Object -Property @{n='givenName'; e={$_.fn}}, @{n='sn'; e={$_.ln}}, @{n='Error'; e={'No matching AD user found!'}}
	}
} |
	Select-Object -Property ($properties + 'Error') |
	Export-Csv -NoTypeInformation -Path "C:\Temp\"

Open in new window

Avatar of gd6627

ASKER

Hi Adam the csv has a list of first names and lastnames  what it does is go and match the list against AD where the first name and the last name match then the end results should be a csv file with the requested information
Avatar of gd6627

ASKER

Hey obDa , thanks for your input but if there are several john Doe's I will need them as well , I only have names in my excel sheet so I am looking for those names on the sheet if duplicates come up it doesn't matter because PS will know that no two users can have the same samaccount so two john doe's may show up as samaccount = johndoe1 and johndo2

In your example you placed the $properties= variable on top of the import-file does that matter?

Thanks for your help
ASKER CERTIFIED SOLUTION
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
Avatar of gd6627

ASKER

Thanks for your help