Link to home
Start Free TrialLog in
Avatar of Alex
AlexFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Can't filter on given name and surname

Morning

$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}

Open in new window


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.PSCustomObject'.
At line:2 char:31
+ ... n $HRusers){Get-ADUser -Filter {(GivenName -eq $HRuser.Firstname) -an ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

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 *

Open in new window


Any help would be great.

Thanks,
Alex
Avatar of ste5an
ste5an
Flag of Germany image

Check your attribute names. The default attributes are sn (surname) and givenName (first name).
Avatar of Alex

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 :(
sn, not surname.
Avatar of oBdA
oBdA

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

Open in new window

Avatar of Alex

ASKER

Ohhhhh

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}

Open in new window


Get-ADUser : Property: 'Firstname' not found in object of type: 'System.Management.Automation.PSCustomObject'.
At line:2 char:31
+ ... n $HRusers){Get-ADUser -Filter {(Givenname -eq $HRuser.Firstname) -an ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Avatar of Alex

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.ActiveDirectory.Management.Commands.GetADUser
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
}

Open in new window

You shouldn't  only use a ScriptBlock.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

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 Alex

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