Active Directory Powershell to manage Sites and Subnets – Part 3 (Getting Site and Subnets)


Hello folks! Here are few Active Directory Powershell script snippets that you will find useful while writing scripts. They deal with fetching sites, subnets and servers. Most of the snippets are simple and self-explanatory and can be simply copy-pasted in your existing script.

## Get a specified Active Directory Site.    
$siteName = “Default-First-Site-Name”
$configNCDN = (Get-ADRootDSE).ConfigurationNamingContext
$siteContainerDN = (“CN=Sites,” + $configNCDN)
$siteDN = “CN=” + $siteName + “,” + $siteContainerDN
Get-ADObject -Identity $siteDN -properties *

## Get all Active Directory Sites (and fetch relevant properties)

$configNCDN = (Get-ADRootDSE).ConfigurationNamingContext
$siteContainerDN = (“CN=Sites,” + $configNCDN)
Get-ADObject -SearchBase $siteContainerDN –filter { objectClass -eq “site” } -properties “siteObjectBL”, “location”, “description” | select Name, Location, Description

## Get all Servers in a specified Active Directory site.

$siteName = “Default-First-Site-Name”
$configNCDN = (Get-ADRootDSE).ConfigurationNamingContext
$siteContainerDN = (“CN=Sites,” + $configNCDN)
$serverContainerDN = “CN=Servers,CN=” + $siteName + “,” + $siteContainerDN
Get-ADObject -SearchBase $serverContainerDN -SearchScope OneLevel –filter { objectClass -eq “Server” } -Properties “DNSHostName”, “Description” | Select Name, DNSHostName, Description

## Get all Subnets in a specified Active Directory site.

$siteName = “Default-First-Site-Name”
$configNCDN = (Get-ADRootDSE).ConfigurationNamingContext
$siteContainerDN = (“CN=Sites,” + $configNCDN)
$siteDN = “CN=” + $siteName + “,” + $siteContainerDN
$siteObj = Get-ADObject -Identity $siteDN -properties “siteObjectBL”, “description”, “location”
foreach ($subnetDN in $siteObj.siteObjectBL) {
Get-ADObject -Identity $subnetDN -properties “siteObject”, “description”, “location”
}

## Print a list of site and their subnets

$configNCDN = (Get-ADRootDSE).ConfigurationNamingContext
$siteContainerDN = (“CN=Sites,” + $configNCDN)
$siteObjs = Get-ADObject -SearchBase $siteContainerDN –filter { objectClass -eq “site” } -properties “siteObjectBL”, name
foreach ($siteObj in $siteObjs) {
$subnetArray = New-Object -Type string[] -ArgumentList $siteObj.siteObjectBL.Count
$i = 0
foreach ($subnetDN in $siteObj.siteObjectBL) {
$subnetName = $subnetDN.SubString(3, $subnetDN.IndexOf(“,CN=Subnets,CN=Sites,”) – 3)
$subnetArray[$i] = $subnetName
$i++
}
$siteSubnetObj = New-Object PSCustomObject | Select SiteName, Subnets
$siteSubnetObj.SiteName = $siteObj.Name
$siteSubnetObj.Subnets = $subnetArray
$siteSubnetObj
}

## Print the site name of a Domain Controller

$dcName = (Get-ADRootDSE).DNSHostName ## Gets the name of DC to which this cmdlet is connected
(Get-ADDomainController $dcName).Site


Hope you find them useful.


Cheers,


Swami



Swaminathan Pattabiraman


Developer – Active Directory Powershell Team

Comments (9)

  1. Daniel says:

    Hey!!

    If I have an IP range, like 10.x.x.x and want to find out what location that is connected to, how can I do that?

    Thanks

  2. Patris_70 says:

    Hello Daniel,

    Did you mean, get Site Name from Subnet IP Address, if yes, you can use Dsquery.

    Here is excample:

    Get Site Name from Subnet IP Address in Active Directory (For example, Site Name for Subnet 192.168.2.0/24)

    Dsquery Subnet -Name 192.168.2.0/24 | Dsget Subnet -Site

  3. rpark says:

    This has proven to be incredibly usefull, is there a way to prevent the output from being truncated? example I get outpul like:

    192.168.138.128/26, 192.168.138.0/26, 192.168.129.128/25, 19….

    How can I get the full output?

    Thanks

  4. Dominique says:

    Hello,

    This is really helpful and I wonder if it could be extended to the list of subnet not active? not used by any computers?

    Thanks,

    Dom

  5. yeah says:

    Get-ADObject -SearchBase $siteContainerDN -filter { objectClass -eq "site" } -proper

    I don't know I can't read the rest of the line.  

  6. Adam Stone says:

    Hi

    I put together an AD sites and services module a while back.  Here's a link to my blog which has all the info

    adadmin.blogspot.com.au/…/aadsites-update-1.html

    and a link to the module on codeplex

    http://aadsites.codeplex.com

    hope it helps

    Adam

  7. vijay sakpal says:

    any command to add site description

  8. Rauno Mägi says:

    I created a onliner for the command:

    Get-ADObject -SearchB (Get-ADRootDSE).ConfigurationNamingContext -f {objectClass -eq "site"} -Pr siteObjectBL | %{'';$.Name;foreach($s in $.siteObjectBL){$s}}

  9. same as yeah says:

    It looks like the formatting truncated multiple lines of the script.