Freigeben über


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
.csharpcode, .csharpcode pre
{
    font-size: small;
   color: black;
   font-family: consolas, "Courier New", courier, monospace;
   background-color: #ffffff;
  /*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
   background-color: #f4f4f4;
  width: 100%;
    margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Hope you find them useful.

Cheers,

Swami

--

Swaminathan Pattabiraman

Developer – Active Directory Powershell Team

Comments

  • Anonymous
    April 08, 2011
    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
  • Anonymous
    November 28, 2011
    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
  • Anonymous
    February 22, 2012
    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
  • Anonymous
    May 02, 2012
    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
  • Anonymous
    June 20, 2013
    The comment has been removed
  • Anonymous
    June 29, 2013
    HiI put together an AD sites and services module a while back.  Here's a link to my blog which has all the infoadadmin.blogspot.com.au/.../aadsites-update-1.htmland a link to the module on codeplexhttp://aadsites.codeplex.comhope it helpsAdam
  • Anonymous
    December 26, 2013
    any command to add site description
  • Anonymous
    January 04, 2015
    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}}
  • Anonymous
    August 26, 2015
    It looks like the formatting truncated multiple lines of the script.