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

Viewing 2 reply threads
  • Author
    Posts
    • #168040
      Participant
      Topics: 120
      Replies: 260
      Points: 238
      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: 20
      Points: 77
      Helping Hand
      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: 120
        Replies: 260
        Points: 238
        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: 20
      Points: 77
      Helping Hand
      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: 120
        Replies: 260
        Points: 238
        Rank: Participant

        Works perfectly now, thanks Robert.

Viewing 2 reply threads
  • The topic ‘Remove-ADsubnet, not seeing all my Subnets contained in variable’ is closed to new replies.