2013-11-01 135 views
0

我正在做一个脚本,从csv添加一些用户到我的广告,它不起作用,因为我找不到^^'。PowerShell - Win2k8 - 脚本

我正在使用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 
} 
+0

具体什么是你想实现什么?这不仅仅是将用户从CSV添加到AD中。尝试调试(甚至只是回显)来验证存储在每个变量中的值,这些值与您所期望的相符。 –

+0

ty我只是做了你所说的(回声),我只是发现我的错误:) – Maho

回答