Add Subnets to 2003 Sites and Services

Posted: September 7, 2012 in Active Directory, Networking, Scripts

Adds in a bunch of new subnets, details read from a CSV, into AD Sites and Services, into pre-existing sites. Requires Quest AD tools.
I can’t take any credit for this one. I pinched the bulk of the code from IT Hassle. All I did was add in an import from CSV to loop the functions. I also didn’t need location where I was using this, so took it out.

function Test-XADObject() {
[CmdletBinding(ConfirmImpact="Low")]
Param (
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
HelpMessage="Identity of the AD object to verify if exists or not."
)]
[Object] $Identity
)
$auxObject = Get-QADObject -Identity $Identity
return ($auxObject -ne $Null)
}

function Add-VADSite {
param(
$SiteName, # Name of the site the subnet should be added to
$Subnet, # Subnet that should be added. Ex: 192.168.1.0/24
$Description # Description of the subnet
)

# Get the Subnet contailer
$ConfigurationDN = (Get-QADRootDSE).ConfigurationNamingContext
$SubnetDN = (“CN=Subnets,CN=Sites,” + $ConfigurationDN)

# Create the new subnet container name
$NewSubnetDN = (“CN=” + $Subnet +”,” + $SubnetDN)
#Test if the new site doesn’t already exist
If (!(Test-XADObject -Identity $newSubnetDN)) {
# Getting the Site
$SiteDN = (Get-QADObject $SiteName -Type “site” -IncludedProperties DistinguishedName).DistinguishedName

#Creating the new subnet
$SubnetName = $Subnet
[Hashtable]$SubnetAttributes = new-object -type hashtable
$SubnetAttributes.Add(“siteObject”, $SiteDN)
$SubnetAttributes.Add(“description”, $Description)
New-QADObject -Name $SubnetName -ParentContainer $SubnetDN -Type subnet -ObjectAttributes $SubnetAttributes
} Else {
Write-Host “Subnet $Subnet already exists. Skipping this subnet” -ForegroundColor Red
}

}

ForEach ($Item in Import-CSV C:\Scripts\ADSubnets\SubnetDetails.csv){
$Site = $Item.Site
$NewSubNet = $Item.NewSubNet
$Description = $Item.Description

Add-VADSite -SiteName $Site -Subnet $NewSubNet -Description $Description
}

Advertisements

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 )

Google+ photo

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

Connecting to %s