PowerShell advanced function (cmdlet) to get all Active Directory Sites and Subnets


I wrote a simple function to get all AD sites and subnets. Using this function it is possible to easily report all sites and for example find sites without subnets. If there is more then on subnet in a site then the output is multiple objects with the same site and different subnet.

Every object (every line in CSV) contains following properties (columns in CSV):

  1. Site Name
  2. Site Distinguished Name
  3. Site Description
  4. Subnet Count
  5. Subnet Name
  6. Subnet Distinguished Name
  7. Subnet Description
Function Get-RvADSiteAndSubnet
{
    <#
    .SYNOPSIS
        Get custom objects of all sites and subnets in Active Directory. If there is more then on subnet in a site then the output is multiple objects with the same site and different subnet.

    .DESCRIPTION
        Developer
            Developer: Rudolf Vesely, http://rudolfvesely.com/
            Copyright (c) Rudolf Vesely. All rights reserved
            License: Free for private use only

            "RV" are initials of the developer's name Rudolf Vesely and distingue names of Rudolf Vesely's cmdlets from the other cmdlets.

        Description
            Get custom objects of all sites and subnets in Active Directory. If there is more then on subnet in a site then the output is multiple objects with the same site and different subnet.

        Requirements
            Developed and tested using PowerShell 4.0.

    .PARAMETER Server
        Specifies the Active Directory Domain Services instance to connect to.

    .EXAMPLE
        'List all'
        Get-RvADSiteAndSubnet

    .EXAMPLE
        'List all and specify Domain Controller to get data'
        Get-RvADSiteAndSubnet -Server mydc4

    .EXAMPLE
        'Export to CSV'
        Get-RvADSiteAndSubnet | Export-Csv `
            -Path 'D:\Sites and subnets.csv' `
            -Delimiter "`t" `
            -Encoding UTF8 `
            -NoTypeInformation

    .INPUTS

    .OUTPUTS

    .LINK
        https://techstronghold.com/
    #>

    [CmdletBinding(
        DefaultParametersetName = 'Server',
        SupportsShouldProcess = $true,
        PositionalBinding = $false,
        HelpURI = 'https://techstronghold.com/',
        ConfirmImpact = 'Medium'
    )]

    Param
    (
        [Parameter(
            Mandatory = $false,
            Position = 0,
            ParameterSetName = 'Server'
        )]
        [ValidateLength(1, 255)]
        [string]$Server
    )

    Begin
    {
        $ErrorActionPreference = 'Stop'
        if ($PSBoundParameters['Debug']) { $DebugPreference = 'Continue' }
        Set-PSDebug -Strict
        Set-StrictMode -Version Latest

        $configNCDN = (Get-ADRootDSE).ConfigurationNamingContext
        $siteContainerDN = 'CN=Sites,{0}' -f $configNCDN

        $commonParametersAndArguments = @{}
        if ($Server) { $commonParametersAndArguments.Add('Server', $Server) }
    }

    Process
    {
        $siteItems = Get-ADObject `
            -SearchBase $siteContainerDN `
            -Filter { objectClass -eq 'site' } `
            -Properties Name, DistinguishedName, Description, siteObjectBL `
            @commonParametersAndArguments

        foreach ($siteItem in $siteItems)
        {
            $outputSite = [PsCustomObject]@{
                SiteName = $siteItem.Name
                SiteDistinguishedName = $siteItem.DistinguishedName
                SiteDescription = $siteItem.Description
                SubnetCount = $siteItem.siteObjectBL.Count
                SubnetName = ''
                SubnetDistinguishedName = ''
                SubnetDescription = ''
            }

            if ($outputSite.SubnetCount -gt 0)
            {
                foreach ($siteSubnetItemDistinguishedName in $siteItem.siteObjectBL)
                {
                    $outputSiteAndSubnet = $outputSite

                    $siteSubnetItem = Get-ADObject `
                        -Identity $siteSubnetItemDistinguishedName `
                        -Properties Name, DistinguishedName, Description `
                        @commonParametersAndArguments

                    $outputSiteAndSubnet.SubnetName = $siteSubnetItem.Name
                    $outputSiteAndSubnet.SubnetDistinguishedName = $siteSubnetItem.DistinguishedName
                    $outputSiteAndSubnet.SubnetDescription = $siteSubnetItem.Description

                    # Return
                    $outputSiteAndSubnet
                }
            }
            else
            {
                # Return
                $outputSite
            }
        }
    }

    End
    {
    }
}

2 responses to “PowerShell advanced function (cmdlet) to get all Active Directory Sites and Subnets”

  1. Rudolf, sorry I don’t see the option for creating excel output. Where is the path for the output file. Thx. Michael.

  2. Excellent, Thanks! I just modified for my use: snip —– foreach ($siteItem in $siteItems) { $outputSite = [PsCustomObject]@{ SiteName = $siteItem.Name SiteDescription = $siteItem.Description SubnetCount = $siteItem.siteObjectBL.Count SubnetName = ” SubnetLocation = ” SubnetDescription = ” SubnetWhenCreated = ” SubnetWhenChanged = ” } if ($outputSite.SubnetCount -gt 0) { foreach ($siteSubnetItemDistinguishedName in $siteItem.siteObjectBL) { $outputSiteAndSubnet = $outputSite $siteSubnetItem = Get-ADObject ` -Identity $siteSubnetItemDistinguishedName ` -Properties Name, DistinguishedName, Description, Location, WhenCreated, WhenChanged ` @commonParametersAndArguments $outputSiteAndSubnet.SubnetName = $siteSubnetItem.Name $outputSiteAndSubnet.SubnetLocation = $siteSubnetItem.Location $outputSiteAndSubnet.SubnetDescription = $siteSubnetItem.Description $outputSiteAndSubnet.SubnetWhenCreated = $siteSubnetItem.WhenCreated $outputSiteAndSubnet.SubnetWhenChanged = $siteSubnetItem.WhenChanged # Return $outputSiteAndSubnet } } else { # Return $outputSite } } } End { } } snip —–

Leave a Reply

Your email address will not be published. Required fields are marked *

Active Directory Advanced function AlwaysOn Availability Groups AlwaysOn Failover Cluster Instances Building Cloud Cloud Cluster Cmdlet Database Deployment Design DFS Domain Controller DSC Fabric Failover Clustering File Server Group Policy Hardware Profile Host Hyper-V Installation Library Library Asset Library Server Network Operations Manager Orchestrator PowerShell PowerShell User Group PowerShell Workflow Security Service Manager SQL Server Storage System Center Template Time Time Synchronization Tips Virtual Machine Virtual Machine Manager VM Network VM Template Windows Server 2012 R2