我正在构建一个退役应用程序,该应用程序将允许个人提供计算机名称,该实用程序将出去并从各个位置清除计算机记录。尝试从 Active Directory 中删除计算机帐户时遇到问题。我正在模拟一个仅有权在特定 OU 结构中“删除所有子对象”的服务帐户。如果我使用我的域管理员帐户运行以下代码,则它可以工作;但是,当我使用模拟的服务帐户运行它时,会出现“拒绝访问”失败。我已经验证了 AD 中的权限是正确的,因为我可以使用“runas”启动 Active Directory 用户和计算机并提供服务帐户凭据,并且可以完美地删除计算机对象。
想知道是否有人以前遇到过这种情况,或者在使用我当前的 OU 权限的同时有不同的编码方式。我的直觉告诉我“DeleteTree”方法比删除对象做得更多。
任何援助将不胜感激。
Sub Main()
Dim strAsset As String = "computer9002"
Dim strADUsername As String = "serviceaccount@domain.com"
Dim strADPassword As String = "password"
Dim strADDomainController As String = "domaincontroller.domain.com"
Dim objDirectoryEntry As New System.DirectoryServices.DirectoryEntry
Dim objDirectorySearcher As New System.DirectoryServices.DirectorySearcher(objDirectoryEntry)
Dim Result As System.DirectoryServices.SearchResult
Dim strLDAPPath As String = ""
Try
objDirectoryEntry.Path = "LDAP://" & strADDomainController
objDirectoryEntry.Username = strADUsername
objDirectoryEntry.Password = strADPassword
objDirectorySearcher.SearchScope = DirectoryServices.SearchScope.Subtree
objDirectorySearcher.Filter = "(&(ObjectClass=Computer)(CN=" & strAsset & "))"
Dim intRecords As Integer = 0
For Each Result In objDirectorySearcher.FindAll
Console.WriteLine(Result.Path)
Diagnostics.Debug.WriteLine("DN: " & Result.Path)
Dim objComputer As System.DirectoryServices.DirectoryEntry = Result.GetDirectoryEntry()
objComputer.DeleteTree()
objComputer.CommitChanges()
intRecords += 1
Next
If intRecords = 0 Then
Console.WriteLine("No Hosts Found")
End If
Catch e As System.Exception
Console.WriteLine("RESULT: " & e.Message)
End Try
End Sub