Bulk capture AD Subnets, then delete them

Welcome Forums General PowerShell Q&A Bulk capture AD Subnets, then delete them

This topic contains 6 replies, has 2 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #167794

    Participant
    Topics: 115
    Replies: 244
    Points: 142
    Rank: Participant

    I want to inventory to a csv all Subnets associated with an AD Site then, later, delete them.

    I cobbled together this code:

    Get-ADReplicationSite -Identity MyADSite -Properties subnets |
        select @{L='Subnets'; E={$_.subnets[0]}} |
            export-csv “c:\All_MyADSites_subnets.csv” -notypeinformation -Encoding UTF8
    
    $Subnets = Get-ADReplicationSite -Identity MyADSite -Properties subnets |
        select @{L='Subnets'; E={$_.subnets[0]}}
    
    Remove-ADReplicationSubnet -Identity $Subnets

    My csv is only getting one value, however.

    Also, will my Remove-ADReplicationSubnet once I get all those DNs' subnets in that variable? The help suggests this will work as it's the expected value, as I understand it.

    Thank you.

  • #167812

    Participant
    Topics: 1
    Replies: 59
    Points: 328
    Helping Hand
    Rank: Contributor

    Do you really want strings with so many commas in a comma delimited file?

    I did something with just using a text file. After the last few days, export-csv is becoming the bane of my existance....

    $subnets=Get-ADReplicationSite -properties subnets |select @{L='subnets'; E={$_.subnets}}
    $subnets.subnets |out-file C:\test\subnet2.txt
    • #167833

      Participant
      Topics: 115
      Replies: 244
      Points: 142
      Rank: Participant

      They ultimately get pumped into an Excel file and sent to management. Management likes Excel

    • #167944

      Participant
      Topics: 1
      Replies: 59
      Points: 328
      Helping Hand
      Rank: Contributor

      The issue you're going to run into, is since a comma is the delimiter, and the subnets are filled with commas each section will be its own field.

      for example, a line will look like:
      CN=0.0.0.0/16,CN=Subnets,CN=Sites,CN=Configuration,DC=myCompany,DC=net

      CN=0.0.0.0/16,
      CN=Subnets,
      CN=Sites,
      CN=Configuration,
      DC=myCompany,
      DC=net

      This will create a file that can be opened in excel:

      $subnets=Get-ADReplicationSite -properties subnets |select @{L='subnets'; E={$_.subnets}}
      foreach ($s in $subnets.subnets){
      $s |add-content C:\test\0726test2.csv}

      That'll be 6 cells for that line.

    • #168031

      Participant
      Topics: 115
      Replies: 244
      Points: 142
      Rank: Participant

      Terrific, thanks.

      Which code then, would be used for the Remove-ADReplicationSubnet then?

      The NoteProperty of $subnets ("subnets") has all the subnets in it. Would that be sufficient to remove those via this one-liner?

      Remove-ADReplicationSubnet -Identity $subnets
    • #168034

      Participant
      Topics: 1
      Replies: 59
      Points: 328
      Helping Hand
      Rank: Contributor

      I haven't worked with removal before, so I can't comment. My /guess/ is that it'll be $subnets.subnets.

      $subnets.count returns NULL
      $subnets.subnets.count returns a value (in my case 75)

      I give no claims of competency around removing the subnets. You might try putting a -whatif at the end to see what it would do.

  • #168037

    Participant
    Topics: 115
    Replies: 244
    Points: 142
    Rank: Participant

    ok thanks I'll post separately (the -WhatIf isn't working with anything, but at least I know it would fail at change time, now)

You must be logged in to reply to this topic.