Link to home
Start Free TrialLog in
Avatar of mobot
mobotFlag for United States of America

asked on

Importing ExtensionAttribute into AD user accounts

I am running the following script:

Import-Csv C:\scripts\import\adimport041317.csv |ForEach-Object {
    Set-ADUser $_.samAccountName -Replace @{
        ExtensionAttribute1=$_.ExtensionAttribute1
        ExtensionAttribute2=$_.ExtensionAttribute2
        ExtensionAttribute3=$_.ExtensionAttribute3
        ExtensionAttribute4=$_.ExtensionAttribute4
        ExtensionAttribute5=$_.ExtensionAttribute5
   }
   }

I receive the following error:
Set-ADUser : replace
At C:\scripts\import\UpdateADScript.ps1:2 char:5
+     Set-ADUser $_.samAccountName -Replace @{
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (******:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser
 
Set-ADUser : Cannot find an object with identity: '' under: 'DC=*****,DC=org'.


I know I am missing something with this script
Avatar of sirbounty
sirbounty
Flag of United States of America image

Could it be simply a blank (or errant) line in your source file?

Import-Csv C:\scripts\import\adimport041317.csv |ForEach-Object { If ($_.samAccountName -ne $null) {
    try {
      Set-ADUser $_.samAccountName -Replace @{
        ExtensionAttribute1=$_.ExtensionAttribute1
        ExtensionAttribute2=$_.ExtensionAttribute2
        ExtensionAttribute3=$_.ExtensionAttribute3
        ExtensionAttribute4=$_.ExtensionAttribute4
        ExtensionAttribute5=$_.ExtensionAttribute5
  catch { write-error $error[0].exception.message }
  }
   }
   }

Open in new window

Avatar of mobot

ASKER

At line:9 char:8
+   catch { write-error $error[0].exception.message }
+        ~
Missing '=' operator after key in hash literal.
At line:12 char:5
+    }
+     ~
The Try statement is missing its Catch or Finally block.
At line:1 char:100
+ ... ame -ne $null) {
+                    ~
Missing closing '}' in statement block.
At line:1 char:65
+ Import-Csv C:\scripts\import\adimport041317.csv |ForEach-Object { If ($_.samAcco ...
+                                                                 ~
Missing closing '}' in statement block.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEqualsInHashLiteral
Ugh - have to learn to paste into ise first...oops.  :^)

Import-Csv C:\scripts\import\adimport041317.csv | `
ForEach-Object {
    If ($_.samAccountName -ne $null) {
        try {
            Set-ADUser $_.samAccountName -Replace @{
            ExtensionAttribute1=$_.ExtensionAttribute1
            ExtensionAttribute2=$_.ExtensionAttribute2
            ExtensionAttribute3=$_.ExtensionAttribute3
            ExtensionAttribute4=$_.ExtensionAttribute4
            ExtensionAttribute5=$_.ExtensionAttribute5
            } #end set-aduser 
    } catch { 
        write-error $error[0].exception.message }
    } # end try/catch
} #end foreach-object

Open in new window

Avatar of mobot

ASKER

Now I see this:
replace
At line:2 char:16
+ ForEach-Object {
+                ~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
Avatar of mobot

ASKER

OK, I discover that if a field is blank in my csv my script would not run.  So how can I run it with mutliple accounts and possible blank fields

This works for me but it is one record with one attribute
Import-Csv C:\scripts\import\adimport041317.csv |ForEach-Object {
    Set-ADUser $_.samAccountName -Replace @{ExtensionAttribute4=$_.ExtensionAttribute4}}
Avatar of mobot

ASKER

Now my entire script is working as long as I do not include attributes that could be blank,  How do I get around that issue, if it is null?

Import-Csv C:\scripts\import\adimport041317.csv |ForEach-Object {
    Set-ADUser $_.samAccountName -Replace @{
        ExtensionAttribute1=$_.ExtensionAttribute1
        ExtensionAttribute2=$_.ExtensionAttribute2
        ExtensionAttribute4=$_.ExtensionAttribute4
   }
   }
ASKER CERTIFIED SOLUTION
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mobot

ASKER

It's the weirdest thing, it keeps hanging on the EstensionAtribute3 because it is blank but if I put a variable in it; it runs without any errors.

Set-ADUser : replace
At line:13 char:3
+         Set-ADUser $_.samAccountName -Replace @{
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (PRaven:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Avatar of mobot

ASKER

Thank you so much.  I will run the scrip with out blank data.  Not sure why it is still not working but you have been an awesome help.   Have a Happy Eater.
Hi Moto,

 Since we check if it is NULL the item that was tripping it up likely had some sort of whitespace character in it which AD would not accept, perhaps a "TAB" character.

Thanks for the points and happy easter.

Ben