none
Exception calling "setInfo" with "0" argument(s): "An invalid dn syntax has been specified." RRS feed

  • Question

  • Hi Guys, 

    I am trying to do 3 things:

    1. Create AD users in bulk from CSV, i am running a script but getting following error. I want to fix this:

    Exception calling "setInfo" with "0" argument(s): "An invalid dn syntax has been specified.
    "
    At C:\Users\hsiddiqui-admin\Desktop\Migration\WJCL-TV\Temp-AD Creation.ps1:48 char:17
    + $objUser.setInfo <<<< ()
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

    2. Enable mailboxes for those users and with that i have add 4 email addresses each account. how to do that.

    3. Make one the primary SMTP out of 4. How to do that?  


    Hasan

    Thursday, November 6, 2014 8:35 PM

Answers

  • In code I always check for blank or missing values before assigning values to attributes (to avoid errors). Also, if I cannot find the problem, it helps to add more SetInfo or CommitChanges method to help narrow down which attribute assignment is the problem. Before a new user object can be saved to AD it must have a value assigned to the sAMAccountName attribute, so that must be assigned before the first CommitChanges. And the account must have been saved in AD before invoking the SetPassword method.

    As long as none of the values is missing, I don't see a problem in the code. However, this Wiki outlines the possible errors when you create a new user:

    Active Directory: Requirements For Creating Objects

    In the Catch clause you might output sAMAccountName so you know which row of the csv has the problem.


    Richard Mueller - MVP Directory Services

    Friday, November 7, 2014 3:51 PM
    Moderator

All replies

  • Here is the complete script:

    #Change N0.1
    #Change the name of OU containing Company name name
    $objADSI = [ADSI]“LDAP://OU=Charlotte,ou=useraccountsandgroups,DC=resource,DC=abccorp,dc=com”
    # Connecting to Company OU
    #Change N0.2
    #Change the name of csv file accordingly
    $UserDetails=Import-Csv “C:\Users\temp-csv.csv” #—–importing bulkusers data
    foreach($UD in $UserDetails) { #——–looping into csv file and Passing all data into variables

    #Define Attributes

    $givenName=$UD.givenName
    $CN = (“CN=” +$UD.cn)
    $userPrincipalName=$UD.userPrincipalName
    $sAMAccountName=$UD.sAMAccountName
    $title=$UD.title
    $sn=$UD.sn
    $displayName=$UD.displayName
    $department=$UD.department
    $telephoneNumber=$UD.telephoneNumber
    $mobile=$UD.mobile
    $streetAddress=$UD.streetAddress
    $l=$UD.l
    #$extensionAttribute8=$UD.extensionAttribute8
    $company=$UD.company


    $Class = “User”
    $strUserName = $CN
    $objUser = $objADSI.Create($Class, $strUserName)



    $objUser.Put(“givenName”,$givenName)
    $objUser.Put(“sAMAccountName”,$sAMAccountName)
    $objUser.Put(“userPrincipalName”,$userPrincipalName)
    $objUser.Put(“title”,$title)
    $objUser.Put(“sn”,$sn)
    $objUser.Put(“displayName”,$displayName)
    $objUser.Put(“department”,$department)
    $objUser.Put(“telephoneNumber”,$telephoneNumber)
    $objUser.Put(“mobile”,$mobile)
    $objUser.Put("streetAddress”,$streetAddress)
    $objUser.Put(“l”,$l)
    #$objUser.Put(“extensionAttribute8”,$extensionAttribute8)
    $objUser.Put("company",$company)

    $objUser.setInfo()

    }

    #change No. 3
    #Change the OU containing university name
    $bulkuserOU=new-object directoryservices.directoryentry(“OU=Charlotte,ou=useraccountsandgroups,DC=resource,DC=abccorp,dc=com”)
    #change No. 4
    #Change the name of CSV file
    $UserDetails=Import-Csv “C:\Users\temp-csv.csv” #—–importing bulkusers data
    $userdetails=$bulkuserOU.psbase.Children 
    foreach($UD in $UserDetails) {
    $cn=$UD.cn
    $accts=$bulkuserOU.psbase.Children.Find(“cn=$cn”)
    $accts.psbase.Invoke(“SetPassword”,”P@ssW0Rd”)
    $accts.psbase.InvokeSet(‘Accountdisabled’,$false)
    $accts.psbase.CommitChanges()


    Hasan

    Thursday, November 6, 2014 10:47 PM
  • Some of your values are null.  You cannot assign null or blank values.


    ¯\_(ツ)_/¯

    Thursday, November 6, 2014 11:24 PM
  • Try this and be sure all of your columns in the CSV are correct and are not blank.

    Try to not write three times as much code as is necessary.  YOu will find it is easier to debug and has less errors. Try also formatting your script so that it is readable.

    $ou=[ADSI]'LDAP://OU=Charlotte,ou=useraccountsandgroups,DC=resource,DC=abccorp,dc=com' $UserDetails=Import-Csv 'C:\Users\temp-csv.csv' #—–importing bulkusers data foreach($UD in $UserDetails){ $user = $ou.Create('User', 'CN=' + $UD.cn) $user.Put('givenName',$UD.givenName) $user.Put('sAMAccountName',$UD.sAMAccountName) $user.Put('userPrincipalName',$UD.userPrincipalName) $user.Put('title',$UD.title) $user.Put('sn',$UD.sn) $user.Put('displayName',$UD.displayName) $user.Put('department',$UD.department) $user.Put('telephoneNumber',$UD.telephoneNumber) $user.Put('mobile',$UD.mobile) $user.Put('streetAddress',$UD.streetAddress) $user.Put('l',$UD.l) $user.Put('company',$UD.company)     $user.psbase.Accountdisabled=$false
    Try{
        $user.CommitChanges()
        $user.SetPassword('P@ssW0Rd')
    }
    Catch{
    # re-throw exception
    Throw $_
    }
    }



    ¯\_(ツ)_/¯





    • Edited by jrv Friday, November 7, 2014 12:46 AM
    Thursday, November 6, 2014 11:35 PM
  • Sorry - I had an error in the above code. I fixed it.


    ¯\_(ツ)_/¯

    Friday, November 7, 2014 12:43 AM
  • Try this and be sure all of your columns in the CSV are correct and are not blank.

    Try to not write three times as much code as is necessary.  YOu will find it is easier to debug and has less errors. Try also formatting your script so that it is readable.

    $ou=[ADSI]'LDAP://OU=Charlotte,ou=useraccountsandgroups,DC=resource,DC=abccorp,dc=com' $UserDetails=Import-Csv 'C:\Users\temp-csv.csv' #—–importing bulkusers data foreach($UD in $UserDetails){ $user = $ou.Create('User', 'CN=' + $UD.cn) $user.Put('givenName',$UD.givenName) $user.Put('sAMAccountName',$UD.sAMAccountName) $user.Put('userPrincipalName',$UD.userPrincipalName) $user.Put('title',$UD.title) $user.Put('sn',$UD.sn) $user.Put('displayName',$UD.displayName) $user.Put('department',$UD.department) $user.Put('telephoneNumber',$UD.telephoneNumber) $user.Put('mobile',$UD.mobile) $user.Put('streetAddress',$UD.streetAddress) $user.Put('l',$UD.l) $user.Put('company',$UD.company)     $user.psbase.Accountdisabled=$false
    Try{
        $user.CommitChanges()
        $user.SetPassword('P@ssW0Rd')
    }
    Catch{
    # re-throw exception
    Throw $_
    }
    }



    ¯\_(ツ)_/¯





    This is the error

    Exception calling "CommitChanges" with "0" argument(s): "An invalid dn syntax has been specified.


    Hasan

    Friday, November 7, 2014 3:22 PM
  • In code I always check for blank or missing values before assigning values to attributes (to avoid errors). Also, if I cannot find the problem, it helps to add more SetInfo or CommitChanges method to help narrow down which attribute assignment is the problem. Before a new user object can be saved to AD it must have a value assigned to the sAMAccountName attribute, so that must be assigned before the first CommitChanges. And the account must have been saved in AD before invoking the SetPassword method.

    As long as none of the values is missing, I don't see a problem in the code. However, this Wiki outlines the possible errors when you create a new user:

    Active Directory: Requirements For Creating Objects

    In the Catch clause you might output sAMAccountName so you know which row of the csv has the problem.


    Richard Mueller - MVP Directory Services

    Friday, November 7, 2014 3:51 PM
    Moderator
  • This is error when using with try / catch

    Property 'Accountdisabled' cannot be found on this object; make sure it exists and is settable.
    At C:\Users\hsiddiqui-admin\Desktop\Migration\Temp-AD Creation-2.ps1:20 char:17
    +    $user.psbase. <<<< Accountdisabled=$false
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyAssignmentException

    Exception calling "CommitChanges" with "0" argument(s): "An invalid dn syntax has been specified.
    "
    At C:\Users\hsiddiqui-admin\Desktop\Migration\Temp-AD Creation-2.ps1:22 char:29
    +          $user.CommitChanges <<<< ()
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException


    Hasan

    Friday, November 7, 2014 4:15 PM
  • This is the CSV. 


    Hasan


    Friday, November 7, 2014 4:30 PM
  • Mannnnnnnnnnnnnnnnnnnnnn...

    That was COMMA in CN and DN... such a smalll thinggggggg... Thanks Richard your article helped. 

    THanks JRV. 

    Now i want 

    2. Enable mailboxes for those users and with that i have add 4 email addresses each account. how to do that.

    3. Make one the primary SMTP out of 4. How to do that?  


    Hasan

    Friday, November 7, 2014 4:59 PM
  • Well i got that working 2 and 3. I created another column in CSV with name emailaddresses and mail for primary smtp. This works:

    After creating AD accounts in above script:

    $email=$ud.emailaddresses -split ';'

    Start-Sleep -Second 20 
    Enable-Mailbox -identity $ud.displayName –Database DM01DB1 -primarysmtpaddress $ud.mail | get-mailbox | select name,windowsemailaddress,database,alias
    Start-Sleep -Seconds 10
    Set-Mailbox $ud.displayName -EmailAddressPolicyEnabled $false
    Start-Sleep -Seconds 10
    set-mailbox $UD.displayName -emailaddresses @{add=$email} 


    Hasan

    Friday, November 7, 2014 10:22 PM
  • Sleeping is totally unnecessary except if you are a bear.  Are you a bear?


    ¯\_(ツ)_/¯

    Friday, November 7, 2014 11:27 PM
  • Sleeping is totally unnecessary except if you are a bear.  Are you a bear?


    ¯\_(ツ)_/¯

    Jrv, 

    :)). Its just because i am doing all at the same time i.e. creating AD account then enabling mailboxes. It is taking 10-15 sec. I was getting user not found while enabling quickly, so i had to insert delay. Thanks for your help like always. 


    Hasan

    Monday, November 10, 2014 7:42 PM