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

Welcome Forums General PowerShell Q&A Remove-ADsubnet, not seeing all my Subnets contained in variable

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

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #168040

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

    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?

  • #168061

    Participant
    Topics: 1
    Replies: 17
    Points: 59
    Rank: Member

    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.

    • #168382

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

      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.
  • #168403

    Participant
    Topics: 1
    Replies: 17
    Points: 59
    Rank: Member

    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}
    • #168415

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

      Works perfectly now, thanks Robert.

You must be logged in to reply to this topic.