Home > Microsoft > PowerShell script to create AD Subnets and Sites

PowerShell script to create AD Subnets and Sites

September 1, 2010 Leave a comment Go to comments
Powershell scripts are really usefull. So, here is on to create automate the AD Sites creation :
How to use :
Save the script below to the notepad and save as Create-ADSubnet.ps1
To execute
Open the Administrator command prompt and type
powershell .create-adsubnet.ps1 SITENAME  ""site location""
You can create a .bat file with all subnets/sites.
Note: use "" if the site location or site name has spaces in between.
#Script to create Subnets and Sites
param ($Subnet, $SiteName, [String] $Location, [String] $Description, [switch]$Help)
function Help
Write-Host "Usage: .Create-ADSubnet.ps1 -Help"
Write-Host "Usage: .Create-ADSubnet.ps1 <Subnet> <SiteName> <Location> <Description>"
Write-Host "Ex: .Create-ADSubnet.ps1 Default-First-Site-Name Sydney FrenchsForest"
if ($Help) {Help}
if ($Subnet -eq $Null) {Write-Host "Provide a Subnet!" -fore Red; Help}
if ($Location -eq $Null) {Write-Host "Provide a Location!" -fore Red; Help}
if ($Description -eq $Null) {Write-Host "Provide a Description!" -fore Red; Help}
if ($SiteName -eq $Null) {Write-Host "Provide a Site Name!" -fore Red; Help}
if ($SiteName -like "CN=*")
 $SiteNameRDN = $SiteName
 $SiteNameRDN = "CN=$($SiteName)"
#$Description = $Subnet
$RootDSE = [ADSI]"LDAP://RootDSE"
$ConfigurationNC = $RootDSE.configurationNamingContext
$SubnetRDN = "CN=$($Subnet)"
#$Description = $Subnet
$SiteDN = "$($SiteNameRDN),CN=Sites,$($ConfigurationNC)"
$SubnetsContainer = [ADSI]"LDAP://CN=Subnets,CN=Sites,$($ConfigurationNC)"
$NewSubnet = $SubnetsContainer.Create("subnet",$SubnetRDN)
$NewSubnet.Put("siteObject", $SiteDN)
$NewSubnet.Put("description", $Description)
$NewSubnet.Put("location", $Location)
trap {Continue}
if (!$?)
Write-Host "Error! Please check your input."
Write-Host "Error Message:"
Write-Host "Subnet Created Successfully."
Categories: Microsoft

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: