JRN
JRN

Reputation: 279

Set title based off employeeid

I have written a script which I want to read a CSV file and grad the employee ID and set the users title based on the employee ID. I am having trouble with the script looping all the samaccount name on each run through the CSV.

$CSVFiles = Import-CSV "C:\T2\SetUserAttributes\EIDTitle.csv"

ForEach($CSVFile in $CSVFiles) { Get-ADUser -Filter * -Properties employeeid | ? {(($CSVFile.EmployeeID))} | Select SamAccountName -ExpandProperty SamAccountName | Sort-Object SamAccountName }

This is what the CSV File Looks Like

EmployeeID  Title
109216  Associate
109215  Vice President
108686  Vice President

Upvotes: 0

Views: 193

Answers (1)

ArcSet
ArcSet

Reputation: 6860

Based off your example looks like you are wondering how to do the If statement efficiently. TheIncorrigible1 pointed out in the comments that calling Get-ADUser in a foreach is extremely inefficient. You can call Get-AdUser once and pipe the output to a "If" that looks for Contains against the EmployeeID property.

$Titles = Import-CSV "C:\T2\SetUserAttributes\EIDTitle.csv"
Get-ADUser -Filter * -Properties employeeid | ?{
    $Titles.EmployeeID.Contains($_.EmployeeId)
}

But when i read the full post I saw that you wanted to change the Title in AD to correspond to the Title in the CSV file. The Diffrence is instead of a if we will go to a foreach-object statement put the AD-Users into a var called $Users and then do a where statement on the CSV variable. At that point we will set the title using a Set-ADUser and output the information to the console.

$Titles = Import-CSV "C:\T2\SetUserAttributes\EIDTitle.csv"
Get-ADUser -Filter * -Properties employeeid | %{
    $User = $_
    $Titles | Where{$_.EmployeeID -eq $USer.EmployeeID} | Set-ADUser $User -Title $_.Title | %{Write-Output "$($User.SamAccountName) : $($_.Title)"}
} 

Upvotes: 1

Related Questions