Remove-ADsubnet, not seeing all my Subnets contained in variable

I have these two lines of code:

$subnets=Get-ADReplicationSite -Identity MySITE -properties subnets | select @{L='subnets'; E={$_.subnets}}
Remove-ADReplicationSubnet -Identity $Subnets -WhatIf

…but I get the following error:

Remove-ADReplicationSubnet : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.

My Variable has values though. GM says they are

subnets     NoteProperty System.Object

and Remove-ADReplicationSubnet says it can handle:

This example shows how to set this parameter to an ADObject object instance named "ADObjectInstance".
        
        -Identity   $ADObjectInstance

Are these two different Objects? How do I en masse delete the Subnets associated with this Site object?

Hi Jeff,

I think the problem is that you have an array or collection of ADObjectInstance returned from Get-ADReplicationSite and the Remove-AdReplicationSite cmdlet only accepts a single instance for the identity parameter.

You’ll need to wrap it in a foreach/foreach-object loop.

ie

$subnets | foreach-object {Remove-ADReplicationSubnet -Identity $_ -WhatIf}

Hope that helps.

Robert, I get this error when attempting to run your suggestion:

Remove-ADReplicationSubnet : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.

Hi Jeff

I didn’t look at your code correctly. The subnets variable has a property called subnets. and the subnets property is a collection.

You’ll need to change it to

$subnets.subnets | foreach-object {Remove-ADReplicationSubnet -Identity $_ -WhatIf}

Works perfectly now, thanks Robert.