Solved

Powershell question about Get-ADUser

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

IoT Devices - Fast, Cheap or Secure…Pick Two

The IoT market is growing at a rapid pace and manufacturers are under pressure to quickly provide new products. Can you be sure that your devices do what they're supposed to do, while still being secure?

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

627 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