Importing updated CSV into Active Directory 'replace' error

Hi guys,

I have a script which exports current user info from AD into a CSV file with saMAccountName, department, description, title, company and office.

This works great. I then can edit it and upload it back into AD with the following script:

$data=import-csv c:\aduserlist.csv

foreach ($_ in $data)

{

if ($_.sAMAccountName -ne ''){

    $identity = $_.sAMAccountName

}

else {

    $identity = ''

}

if ($_.company -ne ''){

    $company = $_.company

}

else {

    $company = ''

}

if ($_.department -ne ''){

    $department = $_.department

}

else {

    $department = ''

}

if ($_.description -ne ''){

    $description = $_.description

}

else {

    $description = ''

}

if ($_.title -ne ''){

    $title = $_.title

}

else {

    $title = ''

}

if ($_.office -ne ''){

    $office = $_.office

}

else {

    $office = ''

}

set-ADuser -identity $identity -company $company -department $department -description $description -title $title -office $office }

}

This works, any field I update, it updates in AD - BUT, any field that is blank it updates with other user info ( ie. If I leave Title blank, it will update it in AD with the last title entered in the CSV file. ) So if User1 title is listed as Manager in the CSV, and User2 does not have a title ( ie, blank cell ) it will give User2 the same Manager title, rather than just leaving it blank - which is what I want it to do.

So basically, I want to to realize that if a cell is blank, to leave it blank in AD/update it to be blank


Any help appreciated!


  • Edited by kb123nz Tuesday, October 23, 2012 7:05 PM
October 23rd, 2012 6:55pm

What's throwing your code off is the use of the $_ variable a a loop variable in the foreach statement.  Use any name, like $user.  $_ is a reserved variable, and should never be written to.
Free Windows Admin Tool Kit Click here and download it now
October 23rd, 2012 7:31pm

So if I change

foreach ($_ in $data)

to

foreach ($_anything in $data)

that would work?

October 23rd, 2012 7:33pm

OK This is what I have now:

$data=import-csv c:\aduserlist.csv
foreach ($datarecord in $data)
{
if ($datarecord.sAMAccountName -ne ''){
    $identity = $datarecord.sAMAccountName
}
else {
    $identity = ''
}
if ($datarecord.company -ne ''){
    $company = $datarecord.company
}
else {
    $company = ''
}
if ($datarecord.department -ne ''){
    $department = $datarecord.department
}
else {
    $department = ''
}
if ($datarecord.description -ne ''){
    $description = $datarecord.description
}
else {
    $description = ''
}
if ($datarecord.title -ne ''){
    $title = $datarecord.title
}
else {
    $title = ''
}
if ($datarecord.office -ne ''){
    $office = $datarecord.office
}
else {
    $office = ''
}
set-ADuser -identity $identity -company $company -department $department -description $description -title $title -office $office }

Not updating still and gives me error:

set-aduser : replace

at line:37 char:11

+ set-ADuser <<<< -identity $identity -company $company -department $department -description $description -title $title -office $office

+categoryinfo : invalidoperation: (administrator:Aduser) [set-aduser]. adinvalidoperationexception

+fullyqualifiederrorid : replace,microsoft.activedirectory.management.commands.setaduser

It gives the same for 4 other users who have blank fields in the csv file.

Free Windows Admin Tool Kit Click here and download it now
October 23rd, 2012 8:33pm

Hi,

I would like suggest you refer to the below similar threads for more information:

Existing AD User Details Bulk Update

http://social.technet.microsoft.com/Forums/en-US/winserverDS/thread/09a9a88a-8151-405f-97c8-bf411fd71beb/

Simple PowerShell Script to Bulk Update or Modify Active Directory User Attributes

http://blog.webactivedirectory.com/2011/07/18/simple-powershell-script-to-bulk-update-or-modify-active-directory-user-attributes/

Updating AD users in bulk

http://richardspowershellblog.wordpress.com/2012/03/22/updating-ad-users-in-bulk/

Regards,

Yan Li

October 26th, 2012 6:48am

Here is what I finally figured out.  Was getting that damn error and tried everything.  What I needed to do was use {&{ in order to have the other fields update with the Identity set in the first string.  What seemed to be happening is that it was loosing the Identity after the first command of "Office $_.Office"

Here is my script to import user csv and update the fields I want.  Everything after the $_. are my Headers in my CSV:

Import-Csv Test-Import.csv | % {&{ Set-ADUser -Identity $_.sAMAccountName -Office $_.Office} {-Title $_.Title} {-Description $_.Description} {-Department $_.Department} {-OfficePhone $_.OfficePhone} {-Fax $_.Fax} {-MobilePhone $_.MobilePhone} {-Company $_.Company} {-StreetAddress $_.StreetAddress} {-City $_.City} {-PostalCode $_.PostalCode} {-State $_.State} {-Country $_.Country}{-HomePage $_.HomePage} }


Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 12:55am

Here is what I finally figured out.  Was getting that damn error and tried everything.  What I needed to do was use {&{ in order to have the other fields update with the Identity set in the first string.  What seemed to be happening is that it was loosing the Identity after the first command of "Office $_.Office"

Here is my script to import user csv and update the fields I want.  Everything after the $_. are my Headers in my CSV:

Import-Csv Test-Import.csv | % {&{ Set-ADUser -Identity $_.sAMAccountName -Office $_.Office} {-Title $_.Title} {-Description $_.Description} {-Department $_.Department} {-OfficePhone $_.OfficePhone} {-Fax $_.Fax} {-MobilePhone $_.MobilePhone} {-Company $_.Company} {-StreetAddress $_.StreetAddress} {-City $_.City} {-PostalCode $_.PostalCode} {-State $_.State} {-Country $_.Country}{-HomePage $_.HomePage} }


June 23rd, 2015 12:59am

You are adding more incorrect information to a three year old thread.  If you have a uesiton please start you own topic as this one was closed years ago,

See forum guidelines.

Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 2:09am

Here is what I finally figured out.  Was getting that damn error and tried everything.  What I needed to do was use {&{ in order to have the other fields update with the Identity set in the first string.  What seemed to be happening is that it was loosing the Identity after the first command of "Office $_.Office"

Here is my script to import user csv and update the fields I want.  Everything after the $_. are my Headers in my CSV:

Import-Csv Test-Import.csv | % {&{ Set-ADUser -Identity $_.sAMAccountName -Office $_.Office} {-Title $_.Title} {-Description $_.Description} {-Department $_.Department} {-OfficePhone $_.OfficePhone} {-Fax $_.Fax} {-MobilePhone $_.MobilePhone} {-Company $_.Company} {-StreetAddress $_.StreetAddress} {-City $_.City} {-PostalCode $_.PostalCode} {-State $_.State} {-Country $_.Country}{-HomePage $_.HomePage} }


June 23rd, 2015 4:54am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics