Managing an Active Directory (AD) environment requires a clear understanding of its configuration, including its network subnets. Knowing when specific subnets were added can provide valuable insights for audits, troubleshooting, and change tracking. However, this information is not readily visible in the default Active Directory tools. To address this, I created a script that retrieves and displays the creation dates of subnets in Active Directory.
In this article, I’ll share the purpose of this script, explain how it works, and provide usage instructions. If you’ve been looking for a way to streamline your subnet management tasks, this solution might be just what you need.
Subnet creation dates can be important for:
- Auditing: Keeping a record of when changes were made.
- Troubleshooting: Identifying if a recent subnet addition aligns with network changes.
- Change Management: Ensuring compliance with organizational policies.
While the default AD tools allow you to manage subnets, they lack detailed tracking capabilities. This script bridges that gap by retrieving and displaying the creation timestamps.
The script leverages PowerShell and Active Directory cmdlets to query the CN=Subnets
container. By parsing the whenCreated
attribute of each subnet object, it provides a clear list of subnets along with their creation dates. Below is a summary of its functionality:
- Connects to the Active Directory environment.
- Queries the
CN=Subnets
container. - Retrieves the
whenCreated
attribute for each subnet. - Outputs the data in a readable format (e.g., table or CSV).
Before running the script, ensure the following:
- You have administrative privileges to access the
CN=Subnets
container. - PowerShell 5.1 or later is installed on your machine.
- The Active Directory module for Windows PowerShell is installed.
When you execute the script, you’ll see
Active Directory Subnet History ;
# Import the Active Directory module
Import-Module ActiveDirectory
try {
# Retrieve the Configuration container
$configNC = (Get-ADRootDSE).configurationNamingContext
# Retrieve subnet information
$subnets = Get-ADObject -Filter 'objectClass -eq "subnet"' `
-SearchBase "CN=Subnets,CN=Sites,$configNC" `
-Properties Name, Description, Location, whenCreated, whenChanged, siteObject
# Process the results
$subnetInfo = $subnets | Select-Object @{
Name = "Subnet"
Expression = { $_.Name }
},
@{
Name = "Created Date"
Expression = { $_.whenCreated }
},
@{
Name = "Last Modified"
Expression = { $_.whenChanged }
},
@{
Name = "Location"
Expression = { if ($_.Location) { $_.Location } else { "Not Specified" } }
},
@{
Name = "Description"
Expression = { if ($_.Description) { $_.Description } else { "No Description" } }
},
@{
Name = "Associated Site"
Expression = {
if ($_.siteObject) {
($_.siteObject -split ',')[0] -replace 'CN='
} else {
"No Site Associated"
}
}
}
# Display the total count
Write-Host "Total Number of Subnets:" $subnets.Count -ForegroundColor Green
Write-Host "`nDetailed Subnet Information:" -ForegroundColor Yellow
Write-Host "------------------------`n"
# Print to screen
$subnetInfo | Format-Table -AutoSize
# Export to CSV
$exportPath = "AD_Subnets_Export_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv"
$subnetInfo | Export-Csv -Path $exportPath -NoTypeInformation -Encoding UTF8
Write-Host "`nExported to:" $exportPath -ForegroundColor Cyan
} catch {
Write-Host "Error occurred: $_" -ForegroundColor Red
Write-Host "`nCheck if you have the following prerequisites:" -ForegroundColor Yellow
Write-Host "1. Running PowerShell as Administrator" -ForegroundColor Yellow
Write-Host "2. Domain Admin or appropriate permissions" -ForegroundColor Yellow
Write-Host "3. Active Directory PowerShell module is installed" -ForegroundColor Yellow
Write-Host "4. Running on a domain-joined machine" -ForegroundColor Yellow
}
While this script fulfills its primary purpose, there’s always room for improvement. Some potential enhancements include:
Adding filtering options to display subnets created within a specific timeframe.
Incorporating logging functionality for audit purposes.
Automating the script to run periodically and generate reports.
This script provides a straightforward way to retrieve subnet creation dates in Active Directory, making it easier to manage and audit your network environment. Feel free to try it out.
You can also check this article to check for missing subnets in your environment.
Have a nice day !