six demon bag

Wind, fire, all that kind of thing!

2015-03-12

Transfer Active Directory Site Subnets

A while ago I had to take a couple hundred subnets (don't ask) of an Active Directory site in one domain and re-create them in another domain. Doing that manually would've been a real pain. But if you export the subnets to a file the list can easily be transferred to the other domain and imported there.


The export can be done for instance from Active Directory Sites and Services (dssite.msc):

Subnet export in "Active Directory Sites and Services".

or with PowerShell:

$list = 'C:\subnets.txt'
$nc   = ([adsi]'LDAP://RootDSE').ConfigurationNamingContext
$base = "cn=subnets,cn=sites,$nc"
$fltr = '(objectClass=subnet)'

Get-ADObject -LDAPFilter $fltr -SearchBase $base -SearchScope Subtree -Properties * |
  Select-Object Name, Location, Description, @{n='Type';e={$_.objectClass}},
                @{n='Site';e={(Get-ADObject $_.siteObject -Properties *).Name}} |
  Export-Csv $list -Delimiter "`t" -Encoding UTF8

I'd recommend exporting the list as tab-separated values, because the GUI export doesn't properly quote exported fields, so commas in the fields Description or Location may become an issue when using CSV format.

Copy the list (subnets.txt) to a domain controller of the other domain and import it with a PowerShell script like this (inspiration taken from here):

$list = 'C:\subnets.txt'
$nc   = ([adsi]'LDAP://RootDSE').configurationNamingContext
$base = "cn=subnets,cn=sites,$nc"

Import-Csv $list -Delimiter "`t" | % {
  $subnet = ([adsi]"LDAP://$base").Create('subnet', "cn=$($_.Name)")
  $subnet.Put('siteObject', "cn=$($_.Site),cn=sites,$nc")
  if ($_.Description) { $subnet.Put('description', $_.Description) }
  if ($_.Location) { $subnet.Put('location', $_.Location) }
  $subnet.SetInfo()
}

Posted 17:42 [permalink]