【问题标题】:Issue bulk creation of users in AD在 AD 中批量创建用户
【发布时间】:2021-02-18 18:52:31
【问题描述】:

我正在尝试创建一种解决方案,以在 AD 中创建数千个帐户,将它们添加到特定组或将服务帐户添加到特定 OU。记录所做的事情和错误是什么。

脚本提取具有以下标头的 csv 文件。

SamAccountName,name,password,ou,domain,isAdded

$Domain  = [system.directoryservices.activedirectory.domain]::GetCurrentDomain().Name
$NewUserADGroup = 'Print Operators'
$NewUsersList = Import-Csv .\bulk_user1.csv | Where-Object{$_.domain -like "$Domain"}
$NewUsersList | ForEach-Object{
              $NewUserAttributes = @{
              SamAccountName    = $_.SamAccountName
              name              = $_.name
              #path              = $_.parentou
              #UserPrincipalName = $_."samAccountName" + "@lovely.Local"
              AccountPassword   = (convertto-securestring "$NewUsersList.password" -AsPlainText -Force)
              Enabled           = $true
              #Server            = $dcname
              #isAdded = $Issue
              }
              try{
                 #Create new User and add to specific group
                 New-ADUser $NewUserAttributes
                 Add-ADGroupMember -Identity $NewUserADGroup -Members $_.SamAccountName
                 #Delete Specific User
                 #Remove-ADUser -Identity $_.SamAccountName
                 }catch{
                        Write-Warning $_
                        $Issue  = $_.ToString()   
                        }
                        $count = $count + 1 
                        Write-Host  $_.SamAccountName " " $_.Name " " $_.SamAccountName.Enabled " Total:" $NewUsersList.Count + "Processed:" $count
                        $NewUserAttributes| Select-Object -Property SamAccountName,name,AccountPassword,Enabled,isAdded | Export-Csv ".\$Domain.NewAccountsCreatedStatus.csv"

}

我收到以下错误:

WARNING: The name provided is not a properly formed account name

当我查看变量时

$NewUserAttributes

我确实看到了名称和值:

Name                           Value                                                                                                                                                  
----                           -----                                                                                                                                                  
Enabled                        True                                                                                                                                                   
name                           bfmbsngfilexfer2                                                                                                                                       
AccountPassword                System.Security.SecureString                                                                                                                           
SamAccountName                 bfmbsngfilexfer2  

          

【问题讨论】:

  • New-ADUser $NewUserAttributes --> New-ADUser @NewUserAttributes。散布哈希表使用语法@params,而不是$params
  • 感谢 Theo,这项工作帐户已创建,但现在它不会将它们添加到组中,并且在输出文件中所有变量都是空的。
  • 我认为它在尝试将用户添加到组时失败警告:在“DC=Testcct,DC=cct”下找不到身份为“bfmbsngfilexfer1”的对象。
  • $rookie = New-ADUser @NewUserAttributes -PassThru捕获新用户,然后用Add-ADGroupMember -Identity $NewUserADGroup -Members $rookie添加到组中
  • 另外,在New-ADUserAdd-ADGroupMember cmdlet 上使用-ErrorAction Stop,这样代码也会进入非终止错误的catch 块。

标签: powershell powershell-3.0 powershell-4.0


【解决方案1】:

正如所承诺的,下面重写你的代码。
我插入了 cmets,希望能解释代码的作用:

$Domain         = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name
$NewUserADGroup = 'Print Operators'
$successCount   = 0
$NewUsersList   = Import-Csv .\bulk_user1.csv | Where-Object { $_.domain -eq $Domain } | ForEach-Object {
    # capture the human readable password for output use
    $password = $_.password
    $userParams = @{
        SamAccountName    = $_.SamAccountName
        Name              = $_.name
        Path              = $_.parentou
        UserPrincipalName = '{0}@lovely.Local' -f $_.SamAccountName
        AccountPassword   = ConvertTo-SecureString $_.password -AsPlainText -Force
        Enabled           = $true
        #Server           = $dcname
    }
    try{
        # Create new User and add to specific group
        $user = New-ADUser @userParams -PassThru -ErrorAction Stop
        Add-ADGroupMember -Identity $NewUserADGroup -Members $user -ErrorAction Stop
        # add the 'isAdded' element to the $userParams hashtable
        $userParams['isAdded'] = $true
        $successCount++
    }
    catch{
        Write-Warning $_.Exception.Message
        $userParams['isAdded'] = $false  
    }
    # output a PsCustomObject with values taken from the Hashtable
    # AccountPassword is a SecureString, which will be of no use to you..
    # Output the human readable password instead so you can inform the new users.
    [PsCustomObject]$userParams | Select-Object SamAccountName, Name,
                                                @{Name = 'Password'; Expression = {$password}},
                                                Enabled, isAdded
}

# output
# use '@($NewUsersList)' to force it as array, so the Count property is accurate
if (@($NewUsersList).Count) { 
    Write-Host ('Processed: {0} Succeeded: {1}' -f $NewUsersList.Count, $successCount) -ForegroundColor Green
    $NewUsersList | Export-Csv ".\$Domain.NewAccountsCreatedStatus.csv" -NoTypeInformation
}
else {
    Write-Host 'No users successfully processed!' -ForegroundColor Red
}

【讨论】:

  • 感谢 Theo,这太棒了。我最终放弃了哈希表,因为以漂亮的表格格式捕获结果需要做很多工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多