通过.csv文件批量加载时,新ADUser无法正常工作

时间:2016-06-23 23:01:51

标签: csv powershell active-directory bulk-load

我遇到了一个根本没有拿起密码字段的错误...我手动运行导入命令以确保它抓取了所有正确的数据,但是它出错了密码和组信息......

$Users = Import-Csv -Path "C:\NewUsers.csv"            
foreach ($User in $Users)            
    {            
    $Displayname = $User.'Firstname' + " " + $User.'Lastname'            
    $UserFirstname = $User.'Firstname'            
    $UserLastname = $User.'Lastname'            
    $OU = $User.'OU'            
    $SAM = $User.'SAM'            
    $UPN = $User.'Firstname' + "." + $User.'Lastname' + "@" + $User.'Maildomain'            
    $Password = $User.'Password'   
    $Description = $User.'Description'            
    $Group = $User.'Group'  

    $Account = New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false -PasswordNeverExpires $true -server esg.intl -PassThru   

    Add-ADGroupMember -Identity $Group -Members $Account
    }

即使我知道密码没问题,我也会遇到错误:

New-ADUser : The password does not meet the length, complexity, or history requirement of the domain.
At C:\Users\A-Shane.Johnson\Desktop\Bulk Add Domain Users.ps1:24 char:13
+ ...  $Account = New-ADUser -Name "$Displayname" -DisplayName "$Displaynam ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (CN=ESGAP PMOInt...,DC=esg,DC=intl:String) [New-ADUser], ADPasswordComplexi
   tyException
    + FullyQualifiedErrorId : ActiveDirectoryServer:1325,Microsoft.ActiveDirectory.Management.Commands.NewADUser

Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\Users\A-Shane.Johnson\Desktop\Bulk Add Domain Users.ps1:26 char:46
+     Add-ADGroupMember -Identity $Group -Members $Account
+                                                 ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
   pMember

2 个答案:

答案 0 :(得分:0)

根据CSV的生成方式,您可能会尝试仔细检查密码列标题中是否有额外的空格?即'密码'。有时会让我感到高兴,因为它不会轻易地显示在excel或cmd线视图中。

答案 1 :(得分:0)

我发现了问题和答案。显然,当部分用户名在密码中时,我们的域名存在问题。我误解了错误,认为转换是造成问题的原因......

我修复了密码,现在脚本运行得很漂亮!