Solved

Powershell question about Get-ADUser

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

710 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