gd6627
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,SamaccountNam e
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,SamaccountNam
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
In your example you placed the $properties= variable on top of the import-file does that matter?
Thanks for your help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help
* 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.
Open in new window