【问题标题】:Setting 'UserAccountControl' using Powershell and .NET使用 Powershell 和 .NET 设置“UserAccountControl”
【发布时间】: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


【解决方案1】:

我找到了一种将凭据放入 powershell ADSI 包装器的简单方法。

$objADSI = [ADSI]$LDAPPath
$objADSI.PsBase.Username = $UserName
$objADSI.PsBase.Password = $Password

使用 psbase 暴露 System.DirectoryServices.DirectoryEntry .NET 对象的隐藏属性

然后您可以返回到通常的 powershell ADSI 包装器方法,并且一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-07
    • 2011-07-18
    • 2013-09-14
    • 2019-09-15
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多