Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7873775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:36:40+00:00 2026-06-03T02:36:40+00:00

I’m building out a decommissioning application that will allow an individual to provide an

  • 0

I’m building out a decommissioning application that will allow an individual to provide an computer name and the utility will go out and purge the computer record from various locations. I’m running into a problem when attempting to delete a computer account from Active Directory. I’m impersonating a service account that only has rights to “Delete All Child Objects” within a particular OU structure. The code below works if I run it with my domain admin account; however fails with an “Access Denied” when I run it with the impersonated service account. I have verified that the permissions are correct within AD as I can launch Active Directory Users and Computers using a “runas” and providing the service account credentials and I can delete computer objects perfectly fine.

Wondering if anyone has run into this before or has a different way to code this while still utilizing my current OU permissions. My gut tells me the “DeleteTree” method is doing more then just deleting the object.

Any assistance would be appreciated.

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:36:42+00:00Added an answer on June 3, 2026 at 2:36 am

    If you’re on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

    • Managing Directory Security Principals in the .NET Framework 3.5
    • MSDN docs on System.DirectoryServices.AccountManagement

    Basically, you can define a domain context and easily find users and/or groups in AD:

    ' set up domain context
    Dim ctx As New PrincipalContext(ContextType.Domain, "DOMAIN", strADUsername, strADPassword)
    
    ' find a computer
    Dim computerToDelete As ComputerPrincipal = ComputerPrincipal.FindByIdentity(ctx, strAsset)
    
    If computerToDelete IsNot Nothing Then
        ' delete the computer, if found
        computerToDelete.Delete()
    End If
    

    The new S.DS.AM makes it really easy to play around with users and groups in AD!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.