简体   繁体   English

创建Active Directory用户PowerShell脚本

[英]Create Active Directory Users PowerShell Script

I am trying to create five Active Directory Users (Manager1, Manager2, etc) and put them in an OU called Managers. 我试图创建五个Active Directory用户(Manager1,Manager2等),并将它们放在一个称为Managers的OU中。 I started by creating the OU manually in Active Directory Users and Computers. 我首先在Active Directory用户和计算机中手动创建OU。 Then, I tried running the script but got this error: 然后,我尝试运行脚本,但收到此错误:

"Add-ADGroupMember : Either the specified user account is already a member of the specified group, or the specified group cannot be deleted because it contains a member At (the path to the .ps1 file) “ Add-ADGroupMember:指定的用户帐户已经是指定组的成员,或者指定的组由于包含成员At(.ps1文件的路径)而无法删除。

Here is my script: 这是我的脚本:

Import-Module ActiveDirectory
$totalusers = 5
for ($i=0; $i -lt $totalusers; $i++) 
 { 
 $userID = "{0:00}" -f ($i + 1)
 $userName = "Manager$userID"

 Write-Host "Creating AD user" ($i + 1) "of" $totalusers ":" $userName

New-ADUser `
 -Name $userName  `
 -Path  "OU=Managers,DC=dsu,DC=com" `
 -SamAccountName $userName `
 -AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) `
 -Enabled $true
 Add-ADGroupMember "Domain Users" $userName;
}

New active directory users are automatically added into the "Domain Users" group. 新的活动目录用户将自动添加到“域用户”组中。 You can see a simple example below: 您可以在下面看到一个简单的示例:

PS C:\temp> New-ADUser webejammin 

PS C:\temp> Get-ADUser webejammin -Properties memberof


DistinguishedName : CN=webejammin,CN=Users,DC=BA,DC=NET
Enabled           : False
GivenName         : 
MemberOf          : {}

Wait? 等待? I thought I said they were automatically added to the group. 我以为我说他们是自动添加到群组的。 So why is the memberof empty? 那么为什么memberof空的? Well the users PrimaryGroup contains the answer. 那么用户PrimaryGroup包含答案。

PS C:\temp> (Get-ADUser webejammin -Properties primarygroup).PrimaryGroup
CN=Domain Users,CN=Users,DC=BA,DC=NET

That is why you get the error you see. 这就是为什么您看到错误的原因。 The group is there at user creation 该组在创建用户时就在那

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM