Get-adobject remove for bulk servers & associate security groups as well

Tagged: 

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Tim Pringle Tim Pringle 10 months, 4 weeks ago.

  • Author
    Posts
  • #33806
    Profile photo of Biswajit
    Biswajit
    Participant

    For a single server it is working fine. It is removing the server ANA-SRV002 & few security groups those are contain “ANA-SRV002” like “Printer_GRP_ ANA-SRV002_DP” but below script is not giving any error neither removing any object.
    #It is working fine
    Get-ADObject -Filter { CN -like "*ANA-SRV002*" } | Remove-ADObject -Confirm:$true

    #Problemetic one for bulk servers
    $Servers = Get-Content D:\Biswajit-Temp\Scripts\Decom_SRVs.txt
    $ErrorActionPreference = "Stop"
    foreach ($Server in $Servers)
    {

    Try {
    Get-ADObject -Filter { CN -like "*$Server*" } | Remove-ADObject -Confirm:$true
    }

    Catch
    {
    Add-Content "$Server Object not found" -path $env:USERPROFILE\Desktop\Decom_UnknownHosts.txt
    }
    }

  • #33809
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hi Biswajit,

    I'd suggest this is a perfect example for using breakpoints in the ISE with your script. If you put a breakpoint in your Get-ADObject..... line and run the code.

    You want to check the value of $Server (and $servers). Also whilst the script is temporarily halted, enter the line

    Get-ADObject -Filter { CN -like "*$Server*" }

    Into the console window, and see what's being returned.

    That's going to give you a better idea of what's happening.

You must be logged in to reply to this topic.