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

Welcome Forums General PowerShell Q&A Get-adobject remove for bulk servers & associate security groups as well

Viewing 1 reply thread
  • Author
    Posts
    • #33806
      Participant
      Topics: 23
      Replies: 26
      Points: 0
      Rank: Member

      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
      Participant
      Topics: 4
      Replies: 262
      Points: 61
      Rank: Member

      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.

Viewing 1 reply thread
  • The topic ‘Get-adobject remove for bulk servers & associate security groups as well’ is closed to new replies.