0

I'm making a script to add some user from a csv to my AD and it doesn't work for some reason that i can't find out ^^'.

I'm using the file ADlog to see where my code goes or not and it goes in the "else (Woot?)" so maybe it can't access to my AD thx to a mistake in my code or ... dunno

#connection to the Active Directory

 $objOU=[ADSI]"LDAP://localhost:389/DC=maho,DC=lan"

if($objOU.Children -ne $null) {

# import data from the csv file

$dataSource=import-csv ("\user.csv")
ForEach($dataRecord in $dataSource) {
    $ou=$dataRecord.service

    #checking the existance of the UO

    if(($objOU.Children | where {$_.Path -match "OU=$ou"}) -eq $null){

    #if it doesn't, we creat it

        $objOU = $objOU.create("organizationalUnit", "ou="+$ou)
        $objOU.SetInfo()
    "UO not there" | Add-Content C:\ADlog.txt

    }

    else {

    #if it does exist we point on it to creat the new user

        $objOU = $objOU.Children.Find("OU=$ou")
    "WOOT ?" | Add-Content C:\ADlog.txt
    }

    $SamAccountName=$dataRecord.login
    $GivenName=$dataRecord.fname
    $sn=$dataRecord.lname
    $cn=$GivenName + " " + $sn
    $displayName=$cn
    $description=$dataRecord.description
    $UserPrincipalname=$SamAccountName +"@"+$DNS_DomainName


    #we create the obj user in the AD

    $objUser=$objOU.Create("User","CN="+$cn)
    $objUser.Put("SamAccountName",$SamAccountName)
    $objUser.Put("UserPrincipalName",$UserPrincipalName)
    $objUser.Put("DisplayName",$Displayname)
    $objUser.Put("Description",$description)
    $objUser.Put("GivenName",$GivenName)
    $objUser.Put("sn",$sn)

    $objUser.SetInfo()

    #$objUser.setPassword("")
    #empty to make the user choise his own passwd

    #we activate the account
    $objUser.psbase.InvokeSet("AccountDisabled",$false)
    $objUser.SetInfo()

    #we check that the acc is created

    if(($objOU.Children | where {$_.Path -match "CN=$cn"}) -ne $null) {
        "User : "+$UserPrincipalName+" Ok" | Add-Content C:\ADlog.txt
    }

    $objOU=[ADSI]"LDAP://localhost:389/DC=maho,DC=lan"

}
Write-Host "Sucess!"

#Delete the reg key

Remove-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run"-Name "Unattend*"

}
 else {
"Failure" | Add-Content C:\ADlog.txt
}
2
  • What specifically are you trying to accomplish? This isn't just adding users to AD from a CSV. Try debugging (or even just echoing) to verify the values stored in each variable that they match what you are expecting. Commented Nov 1, 2013 at 18:40
  • ty i just did what u said (echoing) and i just find my mistake :) Commented Nov 2, 2013 at 12:51

1 Answer 1

1

Check out This Scripting Guy article, pretty straight forward

http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/22/use-powershell-to-read-a-csv-file-and-create-active-directory-user-accounts.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.