I've done something similar at my job and basically did what you're doing and running a loop for each user. Pull the data from AD into an object, and then updating records in a custom object. So, something like this:
$DomainController = "companydc02.companycolo.pvt"
$companyUsersOU = "OU=company,DC=companycolo,DC=pvt"
$MasterFile = "C:\scripts\edir\edir_headers01.csv"
$CSVADMismatch = "C:\scripts\edir\edir_headers01_1.csv"
$CSV = Import-Csv $MasterFile
$Output = @()
ForEach($User in $CSV){
Remove-Variable Current
$Current = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(employeeNumber -eq $ClockNumber)'
If($Current){ #If AD match is found
$Output += New-Object PSObject -Property @{
"Last Name"=$Current.surname
"First Name"=$Current.givenname
"Last 4 SSN"=$User.'Last 4 SSN'
"Other Fields"="Other Values"
}
}else{ #If no AD match
$Output += New-Object PSObject -Property @{
"Last Name"=$User.'Last Name'
"First Name"=$User.'First Name'
"Last 4 SSN"=$User.'Last 4 SSN'
"Other Fields"="Other Values"
}
}
}
$Output|Export-Csv C:\Somefile.csv -NoTypeInformation
You will need to fill in various fields, but that imports your master file, loops through each user and looks them up in AD. If they are found it creates a new object from both the existing user and from AD, if they aren't found it just uses the values from the existing user. Then it adds that object to a master listing. Lastly I had it output that to a new CSV.
Edit: So, you didn't use my code, but you are saying you are getting errors with my code. What you have above doesn't resemble what I gave you at all (except parts that are commented out). If you want help, use what is given you. If you won't use what is given you don't ask for help.
What you have won't work because your hashtables are constructed wrong. The parts that are @{n="Name";e={Expression}}
have some random letters thrown in there. n=
is short for Name=
and e=
is short for Expression=
, so what it should be saying is things like:
Name of the object = "Last Name"
Expression to determine the value = {$User.'Last Name'}
but you have random letters thrown in there like @{o='Last Name';p={$User.'Last Name'}}
and Powershell has no idea what o=
and p=
is supposed to mean, so it throws errors.
Now, you have this as a function, and I'm not sure why. You aren't treating it like a function really. In the spirit of keeping it a function for some reason I've re-done what you have above to keep it as one. This sets it up as a function, and then calls that function, and outputs it to a CSV.
Function CheckAD{
Param($MasterFile = "C:\scripts\edir\edir_headers01.csv",
$DomainController = "companydc02.companycolo.pvt",
$companyUsersOU = "OU=company,DC=companycolo,DC=pvt")
$CSV = Import-Csv $MasterFile
ForEach($User in $CSV)
{
$ClockNumber = $User."company Employee ID"
Remove-Variable AD
$AD = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(employeeNumber -eq $ClockNumber)' -Properties surname,givenName,employeeNumber,department,mail
New-Object PSObject -Property @{"Last Name"=if($AD){$AD.surname}else{$User.'Last Name'}
"First Name"=if($AD){$AD.givenName}else{$User.'First Name'}
"Last 4 SSN"=$User."Last 4 SSN"
"Month of Birth"=$User."Month of Birth"
"Day of Birth"=$User."Day of Birth"
"COMPANY Employee ID"=if($AD){$AD.employeeNumber}else{$User.'Company Employee ID'}
"Titles"=$User."Titles"
"Department Name"=if($AD){$AD.department}else{$User.department}
"Phone Number"=$User."Phone Number"
"Email Address"=if($AD){$AD.mail}else{$User.'Email Address'}
}
}
}
$CSVADMismatch = "C:\scripts\edir\edir_headers01_1.csv"
CheckAD | Sort "Last Name" | Export-CSV $CSVADMisMatch -NoTypeInformation