Reputation: 133
$Results = @()
$Users = Import-CSV C:\Filter\test1.CSV -Header First,Last,Email
Foreach($User in $Users){
$UserProps = get-aduser -Filter "GivenName -eq '$($user.GivenName)' -and Surname -eq '$($user.Surname)'" -Properties GivenName,Surname,EmailAddress,Department
$Results += New-Object PSObject -Property @{
FirstName = $User.GivenName
LastName = $User.Surname
EmailAddress = $User.EmailAddress
Department = $UserProps.Department}
}
$Results
This is test1.CSV 3 columns include:
First Last Email
Westley XXX [email protected]
Dave XXX [email protected]
Gareth XXX [email protected]
Paul XXX [email protected]
What I want it to do is run through the CSV and pull active account information from PowerShell, however I dont think its doing that as my name should be printing out something else
It should be printing out DavidJ as a test since I've edit my account, but instead its printing Dave as my first name how it is in the spreadsheet (CSV).
get-aduser : The search filter cannot be recognized At line:2 char:18 + $UserProps = get-aduser -Filter "GivenName -eq '$($user.GivenName)' -and Sur ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Expected output:
EmailAddress FirstName Department LastName
------------ --------- ---------- --------
[email protected] westley mydepartment hislastnamefromAD
[email protected] davidj (from AD) mydepartment mylastnamefromAD
[email protected] gareth mydepartment hislastnamefromAD
[email protected] paul mydepartment hislastnamefromAD
Upvotes: 1
Views: 1209
Reputation: 437588
Your imported objects have .First
. and .Last
properties, yet you're trying to use properties named .GivenName
and .SurName
in your Get-ADUser
-Filter
argument.
Furthermore, as Jason's answer points out, you're trying to populate the result objects from the imported-from-CSV $User
variable rather than from the queried-from-AD $UserProps
variable.
These problems have been fixed in this simpler, more idiomatic reformulation of your code:
$Results = Import-CSV C:\Filter\test1.CSV -Header First,Last,Email |
Get-ADUser -Filter "GivenName -eq '$($_.First)' -and Surname -eq '$($_.Last)'" -Properties GivenName,Surname,EmailAddress,Department |
Select-Object @{ l = 'FirstName'; e = 'GivenName' },
@{ l = 'LastName'; e = 'Surname' },
EmailAddress,
Department
# Output results.
$Results
Upvotes: 0
Reputation: 1465
In your example:
$Results = @()
$Users = Import-CSV C:\Filter\test1.CSV -Header First,Last,Email
Foreach($User in $Users){
$UserProps = get-aduser -Filter "GivenName -eq '$($user.GivenName)' -and Surname -eq '$($user.Surname)'" -Properties GivenName,Surname,EmailAddress,Department
$Results += New-Object PSObject -Property @{
FirstName = $User.GivenName
LastName = $User.Surname
EmailAddress = $User.EmailAddress
Department = $UserProps.Department}
}
$Results
You are using $User.GivenName instead of $UserProps. The $User variable is only using what was provided in the csv file. You will need to change the value provided for your custom object to using the return from Active Directory in the form of $UserProps.
FirstName = $UserProps.GivenName
Upvotes: 1