创建Active Directory用户PowerShell脚本 [英] Create Active Directory Users PowerShell Script

查看:211
本文介绍了创建Active Directory用户PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建五个Active Directory用户(Manager1,Manager2等),并将它们放在一个称为Managers的OU中。我首先在Active Directory用户和计算机中手动创建OU。然后,我尝试运行脚本,但出现此错误:

I am trying to create five Active Directory Users (Manager1, Manager2, etc) and put them in an OU called Managers. I started by creating the OU manually in Active Directory Users and Computers. Then, I tried running the script but got this error:

Add-ADGroupMember:指定的用户帐户已经是指定组的成员,或者是指定组的成员无法删除,因为它包含成员At(.ps1文件的路径)

"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)

这是我的脚本:

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;
}


推荐答案

新的活动目录用户会自动添加到e域用户组。您可以在下面看到一个简单的示例:

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          : {}

等等?我以为我说他们是自动添加到群组的。那么为什么 memberof 为空?用户 PrimaryGroup 包含答案。

Wait? I thought I said they were automatically added to the group. So why is the memberof empty? Well the users PrimaryGroup contains the answer.

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

这篇关于创建Active Directory用户PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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