Bulk capture AD Subnets, then delete them

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.

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

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

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.

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

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.

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)