“创建AD组时,服务器不愿意处理请求”

时间:2011-07-18 20:00:37

标签: powershell active-directory

我正在尝试创建一个Powershell脚本,以便在我的域控制器的Users容器下创建一个新组(“TestUsers”)。域控制器在2008 Server R2 64位VM上运行。

我的代码是这样的:

#  Group Types in AD
#
# -2147483646 Global security group
# -2147483644 Domain local security group
# -2147483640 Universal security group

$groupName = "TestUsers"
$groupType = -2147483646

$root = [ADSI]""
$rootdn = $root.distinguishedName
$UsersNode = [ADSI]("LDAP://localhost:389/cn=Users,"+$rootdn)
$UsersNode.Create("group", "cn=" + $groupName)
$usersNode.Put("groupType", $groupType)    
$UsersNode.Put("sAMAccountName", $groupName)
$UsersNode.SetInfo()

执行$UsersNode.SetInfo()时,脚本会引发以下错误:

Exception calling "SetInfo" with "0" argument(s): "The server is unwilling to process the request.
"
At line:1 char:19
+ $UsersNode.SetInfo <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

我正在域控制器上运行脚本,以域管理员帐户登录,即mydomain \ Administrator

尝试了不同的群体类型而没有任何运气。

我是AD脚本的新手,所以我几乎跟着下面的文章。

http://geekseat.wordpress.com/2011/02/10/script-of-the-day-creating-ad-groups-without-qad-cmdlets/

如上文所述,我不想安装第三方cmdlet。

感谢。

1 个答案:

答案 0 :(得分:2)

您忘记了用户节点中的创建返回了组对象(此处为$CreatedGroup)。你必须在groupe对象上添加属性。

以下是解决方案:

#  Group Types in AD
#
# -2147483646 Global security group
# -2147483644 Domain local security group
# -2147483640 Universal security group

$groupName = "TestUsers"
$groupType = -2147483646

$root = [ADSI]""
$rootdn = $root.distinguishedName
$UsersNode = [ADSI]("LDAP://localhost:389/cn=Users,"+$rootdn)
$CreatedGrp = $UsersNode.Create("group", "cn=" + $groupName)
$CreatedGrp.Put("groupType", $groupType)  
$CreatedGrp.Put("sAMAccountName", $groupName)
$CreatedGrp.SetInfo()

小心以管理员身份运行它。


如果您使用的是Windows Server 2008 R2,则可以使用ActiveDirectory模块中的Cmdlet(最短,更易读)

Import-Module ActiveDirectory
New-ADGroup -Name $groupName -SamAccountName $groupName -GroupCategory Security -GroupScope Global -DisplayName $groupName -Path "CN=Users" + $rootdn