0

我正在制作一个脚本来将 csv 中的一些用户添加到我的 AD 中,但由于某种我无法找到的原因它不起作用 ^^'。

我正在使用文件 ADlog 来查看我的代码在哪里,它在“else(Woot?)”中,所以它可能无法访问我的 AD thx,因为我的代码有错误,或者......不知道

#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
}
4

1 回答 1

1

查看这篇脚本专家文章,非常简单

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

于 2013-11-01T18:53:49.960 回答