Solved

Powershell question about Get-ADUser

Posted on 2014-12-05
2
860 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
2 Comments
 
LVL 39

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A procedure for exporting installed hotfix details of remote computers using powershell
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now