Solved

Powershell question about Get-ADUser

Posted on 2014-12-05
2
926 Views
Last Modified: 2014-12-08
Hello,

I have a csv file that has employeeID with first_name and Last_name.

employeeID   FirstName      LastName
-----------------   -----------------    ---------------
111                    John               Doe
222                    Jane                Jones

I also set an attribute in AD called extensionAttribute5.  If the value of extensionAttribute5=1 then make the name change else do not.

I have this script:
Import-Csv update.csv | ForEach `
       $employeeID = $_.employeeID
       $SN=$_.LastName
{
      Get-ADUser -LDAPFilter "(employeeID=$employeeID)"| Set-ADUser -replace @{sn=$SN}
}

But the problem is it will change for all users in the file.

How can I put a condition so it would be something like this:
Get-ADUser -LDAPFilter "(employeenumber=$employeenumber)"| If (extensionAttribute5=1) {Set-ADUser -replace @{sn=$SN}}

Thanks.
0
Comment
Question by:mhmservices
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 40483959
I think both of the following will work.  I'm not sure if the second would produce any errors when it pipes nothing to Set-ADUser.  If so, you may be able to quiet the errors by setting the -errorAction parmater to SilentlyContinue for Set-ADUser.
Import-Csv update.csv | ForEach `
        $employeeID = $_.employeeID
        $SN=$_.LastName
 {
    Get-ADUser -LDAPFilter "(employeeID=$employeeID)" -Properties extensionAttribute5 | ForEach `
    {
        If ($_.extensionAttribute5 -eq 1)
        { $_ | Set-ADUser -replace @{sn=$SN} }
    }

 }


Import-Csv update.csv | ForEach `
    $employeeID = $_.employeeID
    $SN=$_.LastName
{
    Get-ADUser -LDAPFilter "(&(employeeID=$employeeID)(extensionAttribute5=1))" | Set-ADUser -replace @{sn=$SN}
}

Open in new window

0
 

Author Closing Comment

by:mhmservices
ID: 40487348
Thanks footech, the first solutions worked.
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question