locked
CSV Import with blanks to the AD RRS feed

  • Question

  • Hello,

    i want import a csv-file with blanks to the AD.

    Here is my script:

    Import-Module ActiveDirectory
    Import-Csv -Path C:\test.csv -Delimiter ";"
    
    foreach ($user in $users)
    
    {
    Set-ADUser -Identity $user.Samaccountname -givenName $($user.givenName) -initial $($user.initial) -Surname $($user.sn) -DisplayName $($user.displayname) -description $($user.description) -Office $($user.physicaldeliveryofficename) -OfficePhone $($user.telephonenumber) -emailaddress $($user.mail)
    }

    I have problems with the blanks.

    Do you have tips for me?

    Greets

    Tobi

    Tuesday, June 26, 2018 8:17 AM

All replies

  • You cannot import nulls or blanks into an AD CmdLet.


    \_(ツ)_/

    Tuesday, June 26, 2018 8:22 AM
  • Hello Jrv,

    thanks for the link.

    I have change the script.

    Here is the change from the script:

    # Retrieve values from the CSV.
    $Samaccountname = $user.Samaccountname
    $initials = $user.initial
    
    # Retrieve existing values of attributes for this user.
    $ADUser = Get-ADUser -Identity $Samaccountname -Properties initials
    
    # Make sure the user is found in AD.
    If ($ADUser)
        {
    # Hash table of the attributes to update for this user.
    $PropUpdate = @{}
    
    # Only update each attribute if it differs from the existing value and is not missing.
    If (($initials) -And ($initials -ne $ADUser.initials))
    {
    $PropUpdate.Add("initials",$initials)
    }
    
    # Only update if there is at least one attribute to update.        
    If ($PropUpdate.Count -ge 1)
    {
    
    # Use the Set-ADUser cmdlet to assign the new attribute values.
    Set-ADUser -Identity $Samaccountname -Replace $PropUpdate
    }
    }
    Else {"User with ID $Samaccountname not found."}
    }
    
    The Script works - but the initials are not assign / cleared from the script.

    Tuesday, June 26, 2018 9:05 AM
  • To understand why go back and read the whole thread carefully.  What you are doing won't work.  A hash cannot clear fields.


    \_(ツ)_/

    Tuesday, June 26, 2018 9:09 AM
  • At the bottom of the thread you write zero fields are deleted. Does this mean the zero fields in the AD or in the CSV?
    Tuesday, June 26, 2018 9:21 AM
  • At the bottom of the thread you write zero fields are deleted. Does this mean the zero fields in the AD or in the CSV?

    Please read what I wrote and what it refers to.  It refers to a specific method of deleting and never says anything about "zeroing" anything.  Words matter.  If you read the word and then change it to another word you cannot possibly understand the statements.  The is no mention of "xeroing".


    \_(ツ)_/

    Tuesday, June 26, 2018 9:31 AM