【发布时间】:2015-04-01 18:32:13
【问题描述】:
我正在编写一个 Powershell 脚本来在 Active Directory 中创建一个用户帐户,并且我想使用凭据来执行此操作,所以我使用的是 .NET
$objDirectoryEntry = New-Object System.DirectoryServices.DirectoryEntry ($OU,$($Credential.UserName),$($Credential.GetNetworkCredential().password))
$Account = $objDirectoryEntry.psbase.get_children().add("CN="+$AccountName,"User")
$Account.psbase.InvokeSet("sAMAccountName",$sAMAccountName)
$Account.psbase.invokeset("DisplayName", $Displayname)
$Account.psbase.invokeset("Description", $Description)
$Account.psbase.CommitChanges()
设置臭名昭著的“UserAccountControl”参数似乎是不可能的
$Account.psbase.invokeset(“userAccountControl”, 66048) #fails
$Account.psbase.invokeset(“userAccountControl”, 0x10200) #fails
$Account.psbase.invokeset(“userAccountControl”, 0x2) #fails
另一方面,使用 ADSI 包装器工作正常。
$objADSI = [ADSI]$AdminOU
$objAccount = $objADSI.create("User","CN="+$AccountName)
# Create the account
$objAccount.put("SamAccountName", $AccountName)
$objAccount.put("DisplayName", $Displayname)
$objAccount.put("Description", $Description)
$objAccount.SetInfo()
# set password
$objAccount.SetPassword($AdminAccountPassword)
$objAccount.SetInfo()
# set the userAccountControl
$objAccount.put(“userAccountControl”, 66048)
$objAccount.SetInfo()
但无法让 ADSI 包装器方法在不同的凭据下运行。
花太多时间在这个问题上,我能想到的唯一其他方法是开始将 ADSI 方法保存到外部脚本并使用凭据调用它,当然有办法
【问题讨论】:
-
当它失败时它会给你一个错误吗?您是否厌倦了将其设置为实际的 int 值:
512 (active) 514 (disabled) etc. -
嗨 Dane,感谢您的回复
-
是的,这是我尝试过的命令,并且收到的错误低于 $ServerAccount.psbase.invokeset(“userAccountControl”, 512) $ServerAccount.psbase.CommitChanges() 调用“CommitChanges”时出现“0”的异常" argument(s): "服务器不愿意处理请求
-
$ServerAccount.psbase.invokeset(“userAccountControl”, 0x512) $ServerAccount.psbase.CommitChanges() 使用“0”参数调用“CommitChanges”的异常:“连接到系统的设备无法正常工作。
-
奇怪的是,这实际上运行并没有错误'但'它似乎没有改变 Active Directory 中的任何内容? $ServerAccount.psbase.invokeset(“userAccountControl”, 0x2 ) $ServerAccount.psbase.CommitChanges()
标签: .net powershell adsi