locked
importing Contacts from csv into AD RRS feed

  • Question

  • Hi,

    i have a csv file with one name and 4 phone number columns. I want to import those data into AD as a Contact. I get an error when i import a contact that has an empty phone number (does not matter which column is empty).

    So i tried to make an if statement to check if the variable has data in it, but i cant get it to work.

    CSV content (Outlook export):

    Name;Telefon geschäftlich;Telefon geschäftlich 2;Weiteres Telefon;Mobiltelefon
    Test 123;+00 1234677;;;+00 1234677
    Test 123 456;+00 1234677;;;+00 1234677

    Powershell Script:

    $contacts = import-csv .\contacts.csv -delimiter ";" -Encoding UTF8
    For ($i=1; $i -le $contacts.count; $i++) {
    
        New-ADObject -name $contacts[$i-1].Name -Path "OU=Contacts,OU=ACPOOE,DC=acpooe,DC=demo" -ProtectedFromAccidentalDeletion $false -Type Contact -OtherAttributes @{
            
            if ($contacts[$i-1]."Mobiltelefon") {mobile=$contacts[$i-1]."Mobiltelefon";}
            if ($contacts[$i-1]."Telefon geschäftlich") {telephoneNumber=$contacts[$i-1]."Telefon geschäftlich";}
            if ($contacts[$i-1]."Telefon geschäftlich 2") {homePhone=$contacts[$i-1]."Telefon geschäftlich 2";}
            if ($contacts[$i-1]."Weiteres Telefon") {ipPhone=$contacts[$i-1]."Weiteres Telefon"}
        }
    }

    I get the following Error:

    At C:\Users\Administrator\Desktop\test\Untitled1.ps1:6 char:11
    +         if ($contacts[$i-1]."Mobiltelefon") {mobile=$contacts[$i-1]."Mobiltelefo ...
    +           ~
    Missing '=' operator after key in hash literal.
    At C:\Users\Administrator\Desktop\test\Untitled1.ps1:2 char:42
    + For ($i=1; $i -le $contacts.count; $i++) {
    +                                          ~
    Missing closing '}' in statement block.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingEqualsInHashLiteral



    Wednesday, July 2, 2014 3:39 PM

Answers

  • I just inserted a - into empty Cells so the PowerShell Script could import it correctly
    • Marked as answer by Elton_Ji Tuesday, August 12, 2014 2:36 AM
    Wednesday, July 30, 2014 9:44 AM

All replies

  • Hi,

    What error do you receive when you just try to import the contact?

    The error messages posted above indicate that you're missing a closing brace and that your hash isn't set up properly. I'd also suggest doing any tests before calling New-ADObject instead of trying to do the test inside of it.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, July 2, 2014 4:03 PM
  • EDIT (7/23/14): This can be ignored, as the troll this was in response to has thankfully been banned.
    Are you so ignorant that doesn't understand that variables in PowerShell require a $ in front of their names?

    Are you so ignorant you can't read?

    I asked a specific question. The idea was to fix the problem, not use a bandaid.

    • DERP DERP DERP DERP


    EDIT: Also, you're very wrong.

    Go look at -OtherAttributes and how it's used.

    Feel stupid yet?

    You should.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Thursday, July 3, 2014 12:52 PM
  • bump
    Wednesday, July 23, 2014 11:26 AM
  • Hi,

    Try building your hash first and then populating it only with data that exists:

    Import-Csv .\contacts.csv -Delimiter ';' -Encoding UTF8 | ForEach {
    
        $otherAttributes = @{
            mobile=''
            telephoneNumber=''
            homePhone=''
            ipPhone=''
        }
    
        $otherAttributes.mobile = $_.Mobiltelefon
        $otherAttributes.telephoneNumber = $_.'Telefon geschäftlich'
        $otherAttributes.homePhone = $_.'Telefon geschäftlich 2'
        $otherAttributes.ipPhone = $_.'Weiteres Telefon'
    
        New-ADObject -Name $_.Name -Path 'OU=Contacts,DC=domain,DC=com' -ProtectedFromAccidentalDeletion $false -Type Contact -OtherAttributes @otherAttributes
    
    }

    I've never actually tried to use a splat for OtherAttributes, so I'm not sure if this will actually work or not.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, July 23, 2014 1:00 PM
  • Hi,

    its still not working i get the following Error:

    New-ADObject : Fehlendes Argument für den Parameter "OtherAttributes". Geben Sie einen Parameter vom Typ "System.Collections.Hashtable" an, und
    
    versuchen Sie es erneut.


    Wednesday, July 30, 2014 7:55 AM
  • I just inserted a - into empty Cells so the PowerShell Script could import it correctly
    • Marked as answer by Elton_Ji Tuesday, August 12, 2014 2:36 AM
    Wednesday, July 30, 2014 9:44 AM