【问题标题】:why can't I add a member to an ADobject为什么我不能将成员添加到 ADobject
【发布时间】:2016-11-16 21:35:42
【问题描述】:

如果我有一个 Microsoft.ActiveDirectory.Management.ADObject 类型的对象,我不能使用 Add-Member 添加注释属性,除非我使用 -force。如果我不使用 force 指令,我会收到如下错误:

添加成员:无法添加名称为“SAMAccountName”的成员 因为同名的成员已经存在。覆盖 无论如何,将 Force 参数添加到您的命令中。

但是,它还不存在。任何属性名称都会发生这种情况。例如:

$domainAccount | Add-Member -NotePropertyName SAMAccountName -NotePropertyValue $account.name

但是,这行得通:

$domainAccount | Add-Member -NotePropertyName SAMAccountName -NotePropertyValue $account.name -force

我找不到解释这一点的文档。你能解释一下吗?而且,使用 -force 指令执行此操作有什么危险吗?

【问题讨论】:

  • "但是,它还不存在。" - 我相信 PowerShell 运行时,还是您?你有什么证据表明它不存在?

标签: powershell active-directory


【解决方案1】:

ADObject 类的行为有点笨拙,因为只需询问一个属性是否存在,如果不存在,就会创建该属性。

Add-Member 检查SAMAccountName 属性是否已经存在时,它偶然会导致它被创建。

只需使用-Force 参数开关即可。


您可以自己重现此行为:

Import-Module ActiveDirectory
$ADObject = New-Object Microsoft.ActiveDirectory.Management.ADObject

# No SamAccountName property will be listed
$ADObject | Get-Member 

现在,尝试引用一个不存在的属性,例如“SamAccountName”(ADObject 扩展了 ADPropertyValueCollection 类,它基本上是一个字典,因此索引到它的属性是完全有效的):

$ADObject["SamAccountName"]
# SamAccountName property will now be listed even though we haven't set it
$ADObject | Get-Member 

这不仅限于 AD 属性名称,什么都可以:

"1 This","2 Is","3 Quite","4 Funky","5 Isn't","6 It?" |ForEach-Object {
    [void]$ADObject[$_]
}

$ADObject |Get-Member

【讨论】:

  • 我觉得Add-Member 有点奇怪,尤其是在循环中使用时,所以即使你只为每个项目添加一个属性,你最终还是不得不使用-Force。结果,我现在总是用-Force 来称呼它。
【解决方案2】:

Microsoft.ActiveDirectory.Management.ADObject 已有 SamAccountName 属性。您无需重新创建它。只需为其定义值,如下所示:

$obj = New-Object Microsoft.ActiveDirectory.Management.ADObject
$obj.SamAccountName = 'AccountName'
$obj | Get-Member

输出:

TypeName: Microsoft.ActiveDirectory.Management.ADObject

Name           MemberType            Definition                                                                             
----           ----------            ----------                                                                             
Contains       Method                bool Contains(string propertyName)                                                     
Equals         Method                bool Equals(System.Object obj)                                                         
GetEnumerator  Method                System.Collections.IDictionaryEnumerator GetEnumerator()                               
GetHashCode    Method                int GetHashCode()                                                                      
GetType        Method                type GetType()                                                                         
ToString       Method                string ToString()                                                                      
Item           ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(string propertyN...
SamAccountName Property              Microsoft.ActiveDirectory.Management.ADPropertyValueCollection SamAccountName {get;s...

【讨论】:

  • 如果你将$obj 传递给Get-Member 之前 设置SamAccountName,你会发现它并不存在
猜你喜欢
  • 2014-05-19
  • 2011-01-30
  • 2018-12-02
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多