【问题标题】:Powershell - Unable to retrieve active directory attribute after clearing itPowershell - 清除后无法检索活动目录属性
【发布时间】:2014-11-20 06:13:26
【问题描述】:

我正在清除计算机的 AD 属性。 然后我尝试将该属性更改为某个值。但是,当我查看该 AD 对象的属性时,该属性似乎不再存在:

function clearAttribute
{
    $directorySearcher = New-Object System.DirectoryServices.DirectorySearcher
    $directorySearcher.PageSize = 100
    $directorySearcher.SearchScope = [System.DirectoryServices.SearchScope]::Subtree
    $directorySearcher.Filter = "(&(objectCategory=computer)(cn=computerName1))"
    $result = $directorySearcher.FindOne()
    if ($result.Properties.Contains("netbootmachinefilepath"))
    {
        $directoryEntry = $result.GetDirectoryEntry()
        $directoryEntry.Properties["netbootmachinefilepath"].Clear()
        $directoryEntry.CommitChanges()
    }
}

function setAttribute
{
    $directorySearcher = New-Object System.DirectoryServices.DirectorySearcher
    $directorySearcher.PageSize = 100
    $directorySearcher.SearchScope = [System.DirectoryServices.SearchScope]::Subtree
    $directorySearcher.Filter = "(&(objectCategory=computer)(cn=computerName1))"
    $result = $directorySearcher.FindOne()
    if ($result.Properties.Contains("netbootmachinefilepath")) ###THIS IS FALSE!###
    {
        $directoryEntry = $result.GetDirectoryEntry()
        $directoryEntry.Properties["netbootmachinefilepath"].Value = "someValue"
        $directoryEntry.CommitChanges()
    }
}
clearAttribute
setAttribute

编辑:原来这个属性可以是非空白或删除(它不能是空白)。在“清除”它之后,如果要更新值,就必须重新创建它。

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    原来我错误地认为如果 $result.Properties.Contains("netbootmachinefilepath") = FALSE 则无法设置属性的值。 不是这种情况。 $result.Properties.Contains("netbootmachinefilepath") = FALSE 只是意味着该值为空(或者也可能该属性不存在?)。

    如果你只是删除 if 语句,如下所示,代码可以工作:

    function setAttribute
    {
        $directorySearcher = New-Object System.DirectoryServices.DirectorySearcher
        $directorySearcher.PageSize = 100
        $directorySearcher.SearchScope = [System.DirectoryServices.SearchScope]::Subtree
        $directorySearcher.Filter = "(&(objectCategory=computer)(cn=computerName1))"
        $result = $directorySearcher.FindOne()
    
        $directoryEntry = $result.GetDirectoryEntry()
        $directoryEntry.Properties["netbootmachinefilepath"].Value = "someValue"
        $directoryEntry.CommitChanges()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      相关资源
      最近更新 更多