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 192.168.1.0/24 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 10.150.0.0/16 Default-First-Site-Name Sydney FrenchsForest"
""
Break
}
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
}
else
{
 $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}
$NewSubnet.SetInfo()
if (!$?)
{
""
Write-Host "Error! Please check your input."
Write-Host "Error Message:"
$Error[0].Exception
""
}
else
{
""
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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

%d bloggers like this: