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
    {
    }
}
« Previous Next »