locked
Set-ADUser from CSV with empty values RRS feed

  • Question

  • Hello Everyone, 

    i'm exporting a CSV file from a an SQL view i have that looks like that :

    emp_bin,emp_mail,DIRECT_MANAGER_GIN,emp_mobile,emp_phone
    B10035000,jim.Danijela@test.com,,,
    B0170051,,,+33 (0) 1 7 712 12 12,
    B0179113,,,+33 (0) 1 11111111,
    B0030096,john.smith@test.com,,+33 (0) 1 12121212,

    as you can see some fields are empty, 

    and when i run my script:

    $CSVData = Import-Csv -Path C:\Scripts\file_export_from_db.csv
    
    $Users = Get-ADUser -Filter * -Properties SamAccountName -SearchBase "OU=Users,OU=Office,OU=test,DC=test,DC=COM"
    
    
    Foreach ($Line in $CSVData) {
        $emp_gin = $Line.emp_gin
        $Username = ($Users | Where {$_.SamAccountName -eq $emp_gin}).SamAccountName
        $Params = 
            @{        
            Identity = $Username
            EmailAddress = $Line.emp_mail
    	mobile = $Line.emp_mobile        
    	OfficePhone = $Line.emp_phone                
              }
        If ($Username) {Set-ADUser  @Params; Write-Host "Set new attributes info for $Username"}    
    } 

    i getting the errors below

    Set-ADUser : replace
    At line:16 char:31
    +     If ($Username) {Set-ADUser <<<<   @Params; Write-Host "Set new attributes info for $Username"}    
        + CategoryInfo          : InvalidOperation: (t0097986:ADUser) [Set-ADUser], ADInvalidOperationException
        + FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser

    can anyone help me understand how to fix this ?

    Thanks


    Hitch Bardawil

    Friday, February 15, 2019 3:56 PM

Answers

  • Hey Hitch Bardawil

    Your code can be improved a lot better but just to fix the issue with the empty value, you will need to create the $Param dynamically like below and use it. It will not assign the empty value into your $Param hash table.

    $Param = @{} If ($Username){ $Param.Add('Identity ',$Username) } If ($Line.emp_mail){ $Param.Add('EmailAddress',$Line.emp_mail) } If ($Line.emp_mobile){ $Param.Add('mobile',$Line.emp_mobile) } If ($Line.emp_phone){ $Param.Add('OfficePhone',$Line.emp_phone) }


    hope this helps.

    • Marked as answer by HitchB52 Monday, February 18, 2019 11:28 AM
    Friday, February 15, 2019 5:00 PM

All replies

  • First, you don't specify the user in your Set-ADUser statement. Specify $Username. I think that accounts for your error message.

    Second, you must test for missing values or the Set-ADUser will raise errors. You cannot assign missing or blank values to any attribute. If desired, you can use the -Clear parameter to clear an attribute.

    Edit: I missed that your $Params collection included Identity. But my second point applies, and is addressed by Naw.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Friday, February 15, 2019 4:58 PM
  • Hey Hitch Bardawil

    Your code can be improved a lot better but just to fix the issue with the empty value, you will need to create the $Param dynamically like below and use it. It will not assign the empty value into your $Param hash table.

    $Param = @{} If ($Username){ $Param.Add('Identity ',$Username) } If ($Line.emp_mail){ $Param.Add('EmailAddress',$Line.emp_mail) } If ($Line.emp_mobile){ $Param.Add('mobile',$Line.emp_mobile) } If ($Line.emp_phone){ $Param.Add('OfficePhone',$Line.emp_phone) }


    hope this helps.

    • Marked as answer by HitchB52 Monday, February 18, 2019 11:28 AM
    Friday, February 15, 2019 5:00 PM
  • This is great thanks a lot!

    if you have any suggestions on how to improve my code it would be very greatful  :) 

    Cheers!



    Hitch Bardawil

    Monday, February 18, 2019 11:30 AM