2014-11-20 4 views
0

컴퓨터의 AD 속성을 지우고 있습니다. 그런 다음 속성을 일부 값으로 변경하려고합니다. 그러나 속성은 내가 그 AD 개체의 속성을 볼 때 더 이상 존재하지 않는 것 같습니다 :Powershell - 지운 후에 활성 디렉토리 속성을 검색 할 수 없습니다.

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 

편집 :은 (는 비워 둘 수 없습니다)이 속성이 아닌 빈 또는 삭제 될 수 있습니다 밝혀졌습니다. 값을 "지우려면"값을 갱신하려면 다시 작성해야합니다.

답변

0

나는 $ result.Properties.Contains ("netbootmachinefilepath") = FALSE 인 경우 속성의 값을 설정할 수 없다고 잘못 추측했다. 그렇지 않습니다. $ result.Properties.Contains ("netbootmachinefilepath") = FALSE는 값이 null임을 의미합니다 (또는 속성이 존재하지 않을 수도 있음). 아래 그림과 같이 그냥 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() 
} 
관련 문제