Alex
asked on
Can't filter on given name and surname
Morning
So, what this is meant to do is pair both the first name and surname and then pump out all the other details for me. For some reason I'm getting the following error
I'm wondering if I need to pull all users and then do a
Any help would be great.
Thanks,
Alex
$HRusers = import-csv 'C:\temp\HR Datanew.csv'
Foreach ($HRUser in $HRusers){Get-ADUser -Filter {(GivenName -eq $HRuser.Firstname) -and (Surname -eq $HRuser.surname)} | select-object Samaccountname, DisplayName, Surname, Firstname | Export-csv c:\temp\HRrestructured.csv -Append}
So, what this is meant to do is pair both the first name and surname and then pump out all the other details for me. For some reason I'm getting the following error
Get-ADUser : Property: 'Firstname' not found in object of type: 'System.Management.Automation.PSCust omObject'.
At line:2 char:31
+ ... n $HRusers){Get-ADUser -Filter {(GivenName -eq $HRuser.Firstname) -an ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.Argumen tException ,Microsoft .ActiveDir ectory.Man agement.Co mmands.Get ADUser
I'm wondering if I need to pull all users and then do a
Where-object {(GivenName -eq $HRuser.Firstname) -and (Surname -eq $HRuser.surname)}" after the get-aduser -filter *
Any help would be great.
Thanks,
Alex
Check your attribute names. The default attributes are sn (surname) and givenName (first name).
ASKER
Yeah I used those
$HRusers = import-csv 'C:\temp\HR Datanew.csv'
Foreach ($HRUser in $HRusers){Get-ADUser -Filter {(GivenName -eq $HRuser.Firstname) -and (Surname -eq $HRuser.surname)} | select-object Samaccountname, DisplayName, Surname, Firstname | Export-csv c:\temp\HRrestructured.csv -Append}
It's still throwing it out back at me :(
$HRusers = import-csv 'C:\temp\HR Datanew.csv'
Foreach ($HRUser in $HRusers){Get-ADUser -Filter {(GivenName -eq $HRuser.Firstname) -and (Surname -eq $HRuser.surname)} | select-object Samaccountname, DisplayName, Surname, Firstname | Export-csv c:\temp\HRrestructured.csv
It's still throwing it out back at me :(
sn, not surname.
Try it like this:
$HRusers = Import-Csv -Path 'C:\temp\HR Datanew.csv'
$HRusers | ForEach-Object {
Get-ADUser -Filter "(GivenName -eq '$($_.Firstname)') -and (surname -eq '$($_.surname)')" |
Select-Object -Property Samaccountname, DisplayName, Surname, Firstname
} | Export-Csv -NoTypeInformation -Path c:\temp\HRrestructured.csv
ASKER
Ohhhhh
Nope, tried that, also verified that my data is correct and there aren't any blank spaces.
Get-ADUser : Property: 'Firstname' not found in object of type: 'System.Management.Automat ion.PSCust omObject'.
At line:2 char:31
+ ... n $HRusers){Get-ADUser -Filter {(Givenname -eq $HRuser.Firstname) -an ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Syst em.Argumen tException ,Microsoft .ActiveDir ectory.Man agement.Co mmands.Get ADUser
Nope, tried that, also verified that my data is correct and there aren't any blank spaces.
$HRusers = import-csv 'C:\temp\HR Datanew.csv'
Foreach ($HRUser in $HRusers){Get-ADUser -Filter {(Givenname -eq $HRuser.Firstname) -and (sn -eq $HRuser.surname)} | select-object Samaccountname, DisplayName, Surname, Firstname | Export-csv c:\temp\HRrestructured.csv -Append}
Get-ADUser : Property: 'Firstname' not found in object of type: 'System.Management.Automat
At line:2 char:31
+ ... n $HRusers){Get-ADUser -Filter {(Givenname -eq $HRuser.Firstname) -an ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Syst
ASKER
oDbA
Your one does this
Get-ADUser : The search filter cannot be recognized
At line:3 char:2
+ Get-ADUser -Filter "(GivenName -eq '$($_.Firstname)') -and (surna ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8254 ,Microsoft .ActiveDir ectory.Man agement.Co mmands.Get ADUser
Your one does this
Get-ADUser : The search filter cannot be recognized
At line:3 char:2
+ Get-ADUser -Filter "(GivenName -eq '$($_.Firstname)') -and (surna ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8254
btw, the Filter property of Get-ADUser is a string, thus it should be something like this:
$HRusers = Import-Csv 'C:\temp\users.csv'
Foreach ($HRUser in $HRusers)
{
$filter = "GivenName -Eq '" + $HRuser.Firstname + "' -And sn -Eq '" + $HRuser.Surname + "'"
Get-ADUser -Filter $filter |
Select-Object SamAccountName, DisplayName, SN, GivenName |
Export-Csv 'C:\Temp\HRrestructured.csv' -Append
}
You shouldn't only use a ScriptBlock.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Gents,
Yep, I was stupid on two counts, I used lastname in my CSV file, changed it to Surname and modified my properties as well.
Works fine now, thank you again.
Regards
Alex
Yep, I was stupid on two counts, I used lastname in my CSV file, changed it to Surname and modified my properties as well.
Works fine now, thank you again.
Regards
Alex