使用PowerShell将用户从CSV导入AD

时间:2015-05-28 09:02:22

标签: powershell csv active-directory powershell-v3.0

我使用以下脚本将CSV文件中的100,000个用户导入Active Directory。但是,我遇到了很多错误,需要你的帮助来消除它们。

脚本:

Import-Csv C:\csv\100Kusers.csv | ForEach-Object {
 $userPrinc = $_."Logon Username" + "@mydomain.com"
 New-QADUser -Name $_.Name `
 -ParentContainer $_."Container" `
 -SamAccountName $_."Logon Username" `
 -UserPassword "passw0rd123" `
 -FirstName $_.FirstName `
 -LastName $_."LastName" `
 -Description $_."Department" `
 -UserPrincipalName $userPrinc `
 -DisplayName $_."Name" `
 -StreetAddress $_."StreetAddress" `
 -City $_."City" `
 -State $_."State" `
 -PostalCode $_."PostalCode" `
 -Email $_."Email" `
 -Company $_."Company" `
 -Department $_."Department" `
 -HomePhone $_."HomePhone" `
 -Title $_."Title" `
 -Manager $_."Manager" ;`
 Add-QADGroupMember -identity $_."Group" -Member $_."Logon Username" ;`
 }

以下是我遇到的错误:

Add-QADGroupMember : Cannot resolve directory object for the given identity: 'jm?lmhx4'.
At C:\csv\PS_import_script.ps1:22 char:2
+  Add-QADGroupMember -identity $_."Group" -Member $_."Logon Username" ;`
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-QADGroupMember], ObjectNotFoundException
    + FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.ObjectNotFoundException,Qu 
   est.ActiveRoles.ArsPowerShellSnapIn.Commands.AddGroupMemberCmdlet2

    New-QADUser : A device attached to the system is not functioning.
At C:\csv\PS_import_script.ps1:3 char:2
+  New-QADUser -Name $_.Name `
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-QADUser], DirectoryServicesCOMException
    + FullyQualifiedErrorId : System.DirectoryServices.DirectoryServicesCOMException,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.NewUserCmdlet

CSV如下所示:

FirstName   Surname Name    StreetAddress   City    State   PostalCode  EmailAddress    Company Department  Group   HomePhone   Title   Manager Logon Username  Container
Jose    Mullane Jose Mul    Rue de Li?ge 493    Lompret WLX 6463    JoseLMullane@superrito.com  rmit    QA  QA-FL   0495 94 79 62   Laboratory animal technologist  tgatesjr    jmuljhk2    dcui.mydomain.com/dcui_OU/Finland/Departments/QA

抱歉,无法添加更多内容 - 当我将纯CSV复制到帖子窗口时,格式化会变得很疯狂。

1 个答案:

答案 0 :(得分:0)

非常感谢大家。我同意这可能是一个编码问题,是的,CSV本身有所有这些“?”标记,我从xlsx格式保存后。我们最终使用了Pyhthon,与Pshell相比,它也快得多。

此致,