list active directory subnets with powershell

Windows PowerShell

sometimes it’s fun to do things the long way (not) and then do the equivalent in a shortcut fashion.

these are the steps i used to retrieve subnets from active directory.

 

first of all, let’s grab the forest.

$myForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()

 

now, we can get the list of sites names.

$myForest.Sites | Select-Object name

 

if we see a site name that we like, we can retrieve just that site name and the subnets associated with it.

$myForest.Sites | Where-Object { $_.Name -eq 'myCity' } | Select-Object Subnets
 

 

well, that’s probably not what you wanted unless you have such few subnets you can see the whole thing.  let’s pass that through the ExpandProperty feature of select-object.

$myForest.Sites | Where-Object { $_.Name -eq 'myCity' } | Select-Object -ExpandProperty Subnets

 

that’s better!

Comments

  1. Very helpful, just what I needed.

    ReplyDelete
  2. Nice example, I used it for a script which gets site and subnet information from AD an saves it as an XML file:
    http://meanderingmarcus.wordpress.com/2011/08/23/read-ad-site-and-subnet-information-save-to-xml/

    ReplyDelete
  3. thanks for the link back! i'm sure i'll be able to find something to do with xml formatting of that data.

    ReplyDelete
  4. Thanks a lot ! Very usefull.

    ReplyDelete

Post a Comment