Active Directory对象的导入CSV已经存在错误 [英] Import-CSV for Active Directory Object Already Exists Error

查看:89
本文介绍了Active Directory对象的导入CSV已经存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个PowerShell脚本,用于从CSV文件导入新的AD用户.代码是:

I created a PowerShell script to import new AD users from a CSV file. The code is:

Import-Module ActiveDirectory
$Users = Import-Csv -Delimiter "," -Path "C:\temp\kindergarten.csv"
$Password = 000
foreach ($User in $Users) {
    $Password = $Password + 1
    $OU = "OU=KCenter,OU=Students,OU=District Users New,DC=,DC=k12,DC=ny,DC=us"
    $UserFirstname = $User.FirstName
    $UserLastname = $User.LastName
    $DetailedName = $UserFirstname + " " + $UserLastname
    $FirstLetterFirstname = $UserFirstname.substring(0,1)
    $SAMName = $FirstLetterFirstname + $UserLastname
    $UserPrincipalName = $SAMName + "@student.pobschools.org"
    $Description = "Kindergarteners K-Center"


    New-ADUser -Name $DetailedName -SamAccountName $SAMName -UserPrincipalName $UserPrincipalName -DisplayName     
    $SAMName -GivenName $UserFirstname -Surname $UserLastname -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) 
    -Enabled $false -Description $Description -EmailAddress $UserPrincipalName -CannotChangePassword $true -ChangePasswordAtLogon $false -Path $OU
    }

该脚本可处理CSV中大约一半的条目.对于其他人,我会得到错误:

The script worked for about half of the entries in the CSV. For the others I get the error:

New-ADUser : The object already exists
At C:\users\jbaruch\desktop\getADUsers.ps1:16 char:12
+     New-ADUser <<<<  -Name $DetailedName -SamAccountName $SAMName -UserPrincipalName $UserPrincipalName -DisplayName
$SAMName -GivenName $UserFirstname -Surname $UserLastname -AccountPassword (ConvertTo-SecureString $Password -AsPlainTe
xt -Force) -Enabled $false -Description $Description -EmailAddress $UserPrincipalName -CannotChangePassword $true -Chan
gePasswordAtLogon $false -Path $OU
    + CategoryInfo          : NotSpecified: (CN=Shradha Sang...k12,DC=ny,DC=us:String) [New-ADUser], ADException
    + FullyQualifiedErrorId : The object already exists,Microsoft.ActiveDirectory.Management.Commands.NewADUser

如果我搜索用户,则会收到错误消息,因为没有结果.我不确定为什么它又回来了.任何帮助将不胜感激,谢谢.

If I search for the users I get an error for there are no results. I am not sure why it is coming back as already existing. Any help would be appreciated, Thanks.

推荐答案

阅读您的评论,我认为这可能对您有帮助:

Reading your comment I think this might help you:

$VerbosePreference = 'Continue'

$Users = Import-Csv -Delimiter "," -Path "C:\temp\kindergarten.csv"
$Password = 000
foreach ($User in $Users) {

    $Password = $Password + 1
    $SamAccountName = $User.FirstName[0] + $User.LastName

    if ($U = Get-ADUser -Filter {SamAccountName -eq $SamAccountName}) {
        Write-Verbose "SamAccountName $($SamAccountName) already present"

        if ($U.GivenName -eq $User.FirstName) {
            Write-Verbose "User firstname $($User.FirstName) already present in AD"
            Continue # to the next user
        }
        $SamAccountName = $User.FirstName[1] + $User.LastName
        Write-Verbose "New SamAccountName generated $($SamAccountName)"
    }

    $ADParams = @{
        Name                  = $UserFirstname + ' ' + $UserLastname
        SamAccountName        = $SamAccountName
        UserPrincipalName     = $SamAccountName + '@student.pobschools.org'
        DisplayName           = $SamAccountName
        GivenName             = $User.FirstName
        Surname               = $User.LastName
        AccountPassword       = (ConvertTo-SecureString $Password -AsPlainText -Force)
        Enabled               = $false
        Description           = 'Kindergarteners K-Center'
        EmailAddress          = $SamAccountName + '@student.pobschools.org'
        CannotChangePassword  = $true
        ChangePasswordAtLogon = $false
        Path                  = 'OU=KCenter,OU=Students,OU=District Users New,DC=,DC=k12,DC=ny,DC=us'
    }
    Write-Verbose "Create user $($SamAccountName)"
    New-ADUser @ADParams
}

Splatting是一种很好的技巧,可以使事情更具可读性.

Splatting is a nice technique for this to make things more readable.

这篇关于Active Directory对象的导入CSV已经存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆