Using customattributes

This topic contains 1 reply, has 2 voices, and was last updated by  Matt Bloomfield 4 months, 3 weeks ago.

  • Author
    Posts
  • #67420

    Johnathan Tenoever
    Participant

    I have a simple user creation script that works great except for extensionattributes. I'm having issues with the autopopulate/translation of the extenstion attributes. I'm familiar with set-aduser and the process of adding extension attributes but with New-aduser it needs "-otherattributes" to define the custom attribute. Which works fine if I manually add it, but wont work in my translation.(or I can't get the syntax correct) How do I properly incorporate the ability to pull custom/extension attributes straight from the csv?

    import-csv ".\newusers.csv" | Select-Object Title, Department, City, State, Office, EmployeeID,DBRMember,Manager?,Contractor,VIP,`
    @{name='name';e={($_.'First Name'+' '+$_.'Last Name')}},
    @{name='samaccountname';e={($_.'First Name'.substring(0,1)+$_.'Last Name').tolower()}},
    @{name='UserPrincipalName';e={($_.'First Name'.substring(0,1)+$_.'Last Name').toLower()+"@company.com"}},
    @{name='displayname';e={$_.'First Name'+' '+$_.'Last Name'}},
    @{name='givenname';e={$_.'First Name'}},
    @{name='surname';e={$_.'Last Name'}},
    @{name='path';e={'Ou=Test,OU=IT,OU=company,DC=company,DC=com'}},
    @{name='extensionAttribute1';e={$_.'DBRMember'}},
    @{name='extensionAttribute2';e={$_.'Manager?'}},
    @{name='extensionAttribute3';e={$_.'Contractor'}},
    @{name='extensionAttribute4';e={$_.'VIP'}}|
    Out-GridView

    ———————————————————————————————

    For example, this works when manually specified

    New-Aduser -ChangePasswordAtLogon $true -Enabled $true -AccountPassword $(ConvertTo-Securestring 'P@$$word' -AsPlainText -force)`
    -Description "Test User Creation By Script"`
    -HomeDrive 'h:' `
    -HomeDirectory "\\company\dfs\users\%username%"`
    -Company "Company, Inc" `
    -StreetAddress "1234 Innovation Way" `
    -PostalCode "12345"`
    #-OtherAttributes @{extensionAttribute1="TEST";extensionAttribute2= "TEST2"; extensionattribute3="Test3"; extensionattribute4="Test4"}

  • #67431

    Matt Bloomfield
    Participant

    Build a hashtable from your CSV columns and pass that as the value to the OtherAttributes parameter. Tested on 2012 R2:

    #Sample CSV saved as csv1
    sAMAccountName,attr1,attr2
    test998,value1,value2
    test999,value3,value4
    
    
    $users = Import-Csv c:\csv1.csv
    
    foreach ($user in $users) {
    
        $sa = $user.sAMAccountName
        $oa = @{
           adminDescription = $user.attr1
           adminDisplayName = $user.attr2
        }
    
        New-ADUser -Name $sa -OtherAttributes $oa
    
    }

You must be logged in to reply to this topic.