Solved

Powershell question about Get-ADUser

Posted on 2014-12-05
2
897 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

786 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