AD Administration Scripts

Creates an Active Directory site and sets the site link for the new site.

strSiteRDN      = "cn=ON-TOR-Marketing"
strSiteLinkRDN  = "cn=DEFAULTIPSITELINK"
strSiteLinkType = "IP"                      
 const ADS_PROPERTY_APPEND = 3

Set objRootDSE = GetObject("LDAP://RootDSE")

strConfigurationNC = objRootDSE.Get("configurationNamingContext")
strSitesContainer = "LDAP://cn=Sites," & strConfigurationNC

Set objSitesContainer = GetObject(strSitesContainer)

Set objSite = objSitesContainer.Create("site", strSiteRDN)

objSite.SetInfo

Set objLicensingSiteSettings = objSite.Create("licensingSiteSettings", _

    "cn=Licensing Site Settings")
objLicensingSiteSettings.SetInfo

Set objNtdsSiteSettings = objSite.Create("nTDSSiteSettings", _

     "cn=NTDS Site Settings")
objNtdsSiteSettings.SetInfo

Set objServersContainer = objSite.Create("serversContainer", "cn=Servers")

objServersContainer.SetInfo

strSiteLinkPath = "LDAP://" & strSiteLinkRDN & ",cn=" & strSiteLinkType & _

    ",cn=Inter-Site Transports,cn=Sites," & strConfigurationNC
 set objSiteLink = GetObject(strSiteLinkPath)
objSiteLink.PutEx ADS_PROPERTY_APPEND, "siteList", _
                  Array(objSite.Get("distinguishedName"))
objSiteLink.SetInfo

Create an Active Directory Site Link

Creates an Active Directory site link.

strSite1Name    = "ON-TOR-Sales"
strSite2Name    = "Wa-Red-Sales"
strSiteLinkRDN  = "cn=[" & strSite1Name & "][" & strSite2Name & "]"
intCost         = 100
intReplInterval = 60
strDescription  = "[" & strSite1Name & "][" & strSite2Name & "]"

Const ADS_PROPERTY_UPDATE = 2

Set objRootDSE = GetObject("LDAP://RootDSE")

strConfigurationNC = objRootDSE.Get("configurationNamingContext")

strSite1DN = "cn=" & strSite1Name & ",cn=Sites," & strConfigurationNC

strSite2DN = "cn=" & strSite2Name & ",cn=Sites," & strConfigurationNC

Set objInterSiteTransports = GetObject("LDAP://" & _

    "cn=IP,cn=Inter-Site Transports,cn=Sites," & strConfigurationNC)

Set objSiteLink = objInterSiteTransports.Create("siteLink", strSiteLinkRDN)

objSiteLink.Put "cost",         intCost
objSiteLink.Put "replInterval", intReplInterval
objSiteLink.Put "description",  strDescription
 objSiteLink.PutEx ADS_PROPERTY_UPDATE, "siteList", _
                  Array(strSite1DN, strSite2DN)
objSiteLink.SetInfo

Create an Active Directory Subnet

Creates an Active Directory subnet.

strSubnetRDN     = "cn=192.168.1.0/26"
strSiteObjectRDN = "cn=ON-TOR-Sales-Marketing"
strDescription   = "192.168.1.0/255.255.255.192"
strLocation      = "USA/GA/Atlanta"

Set objRootDSE = GetObject("LDAP://RootDSE")

strConfigurationNC = objRootDSE.Get("configurationNamingContext")

strSiteObjectDN = strSiteObjectRDN & ",cn=Sites," & strConfigurationNC

strSubnetsContainer = "LDAP://cn=Subnets,cn=Sites," & strConfigurationNC

Set objSubnetsContainer = GetObject(strSubnetsContainer)

Set objSubnet = objSubnetsContainer.Create("subnet", strSubnetRDN)

objSubnet.Put "siteObject", strSiteObjectDN
objSubnet.Put "description", strDescription
 objSubnet.Put "location", strLocation
objSubnet.SetInfo

Delete an Active Directory Subnet

Deletes an Active Directory subnet.

strSubnetCN = "cn=192.168.1.0/26"

Set objRootDSE = GetObject("LDAP://RootDSE")

strConfigurationNC = objRootDSE.Get("configurationNamingContext")
strSubnetsContainer = "LDAP://cn=Subnets,cn=Sites," & strConfigurationNC

Set objSubnetsContainer = GetObject(strSubnetsContainer)

objSubnetsContainer.Delete "subnet", strSubnetCN

List Active Directory Connections

Lists Active Directory connections (nTDSConnection objects) for a specified domain controller.

strDcRDN   = "cn=ont-dc-01"
strSiteRDN = "cn=ON-TOR-Marketing"

Set objRootDSE = GetObject("LDAP://RootDSE")

strConfigurationNC = objRootDSE.Get("configurationNamingContext")

strNtdsSettingsPath = "LDAP://cn=NTDS Settings," & strDcRDN & _

    ",cn=Servers," & strSiteRDN & ",cn=Sites," & strConfigurationNC

Set objNtdsSettings = GetObject(strNtdsSettingsPath)

objNtdsSettings.Filter = Array("nTDSConnection")

WScript.Echo strDcRDN & " NTDS Connection Objects" & vbCrLf & _
    String(Len(strDcRDN) + 24, "=")

For Each objConnection In objNtdsSettings

    WScript.Echo "Name:      " & objConnection.Name
    WScript.Echo "Enabled:   " & objConnection.enabledConnection
    WScript.Echo "From:      " & Split(objConnection.fromServer, ",")(1)
    WScript.Echo "Options:   " & objConnection.Options
    WScript.Echo "Transport: " & Split(objConnection.transportType, ",")(0)
    WScript.Echo "Naming Contexts"
    WScript.Echo "---------------"
    For Each objDNWithBin In objConnection.GetEx("ms-DS-ReplicatesNCReason")
        Wscript.Echo objDNWithBin.DNString
    Next
    WScript.Echo
Next

List All Domain Controllers

Returns a list of all the domain controllers in the fabrikam.com domain.

Const ADS_SCOPE_SUBTREE = 2
 Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection

objCommand.CommandText = _

    "Select distinguishedName from " & _
        "'LDAP://cn=Configuration,DC=fabrikam,DC=com' " _
            & "where objectClass='nTDSDSA'" 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF

    Wscript.Echo "Computer Name: " & _
        objRecordSet.Fields("distinguishedName").Value
    objRecordSet.MoveNext
Loop

List Active Directory Sites

Lists Active Directory sites.

Set objRootDSE = GetObject("LDAP://RootDSE")
strConfigurationNC = objRootDSE.Get("configurationNamingContext")

strSitesContainer = "LDAP://cn=Sites," & strConfigurationNC

Set objSitesContainer = GetObject(strSitesContainer)
objSitesContainer.Filter = Array("site")

For Each objSite In objSitesContainer

    WScript.Echo "Name: " & objSite.Name
Next

List Your Domain Controller

Returns the name of the domain controller used to authenticate the logged-on user of a computer.

Set objDomain = GetObject("LDAP://rootDse")
 objDC = objDomain.Get("dnsHostName")
Wscript.Echo "Authenticating domain controller:" & objDC

List the Protocols Over Which a Bridgehead Server Replicates

Reads the bridgehead transport list from a domain controller in a site.

On Error Resume Next
   Set objServer = GetObject _
    ("LDAP://CN=SEA-DC-01,CN=Servers,CN=Default-First-Site-Name,"  & _
        " CN=Sites,CN=Configuration,DC=fabrikam,DC=com")

dnBHTList = objServer.GetEx("bridgeheadTransportList")

WScript.Echo "Bridge Head Transport List:"

WScript.Echo "This multi-valued attribute lists the protocol" & _
    "transports over which this BridgeHead Server replicates"
For Each dnValue in dnBHTList
    WScript.Echo "Value: " & dnValue
Next

List the Subnets in an Active Directory Site

Lists subnets in a specified Active Directory site.

strSiteRDN = "cn=ON-TOR-Marketing"

Set objRootDSE = GetObject("LDAP://RootDSE")

strConfigurationNC = objRootDSE.Get("configurationNamingContext")

strSitePath = "LDAP://" & strSiteRDN & ",cn=Sites," & strConfigurationNC

Set objSite = GetObject(strSitePath)

objSite.GetInfoEx Array("siteObjectBL"), 0

arrSiteObjectBL = objSite.GetEx("siteObjectBL")

WScript.Echo strSiteRDN & " Subnets" & vbCrLf & _

    String(Len(strSiteRDN) + 8, "-")

For Each strSiteObjectBL In arrSiteObjectBL

    WScript.Echo Split(Split(strSiteObjectBL, ",")(0), "=")(1)
Next

List the Subnets in all Active Directory Sites

Lists subnets in all Active Directory sites.

Set objRootDSE = GetObject("LDAP://RootDSE")
strConfigurationNC = objRootDSE.Get("configurationNamingContext")

strSubnetsContainer = "LDAP://cn=Subnets,cn=Sites," & strConfigurationNC

Set objSubnetsContainer = GetObject(strSubnetsContainer)

objSubnetsContainer.Filter = Array("subnet")

Set objHash = CreateObject("Scripting.Dictionary")

For Each objSubnet In objSubnetsContainer

    objSubnet.GetInfoEx Array("siteObject"), 0
    strSiteObjectDN = objSubnet.Get("siteObject")
    strSiteObjectName = Split(Split(strSiteObjectDN, ",")(0), "=")(1)

If objHash.Exists(strSiteObjectName) Then

        objHash(strSiteObjectName) = objHash(strSiteObjectName) & "," & _
            Split(objSubnet.Name, "=")(1)
    Else
        objHash.Add strSiteObjectName, Split(objSubnet.Name, "=")(1)
    End If
Next

For Each strKey In objHash.Keys

    WScript.Echo strKey & "," & objHash(strKey)
Next

List the Site Name for a Domain Controller

Reports the site name for a specified computer.

strDcName = "ONT-dc-01"
Set objADSysInfo = CreateObject("ADSystemInfo")
 strDcSiteName = objADSysInfo.GetDCSiteName(strDcName)
WScript.Echo "DC Site Name: " & strDcSiteName

List the Site Name for the Local Computer

Reports the site name for the local computer.

Set objADSysInfo = CreateObject("ADSystemInfo")
 WScript.Echo "Current site name: " & objADSysInfo.SiteName

Move a Domain Controller to a New Active Directory Site

Moves a domain controller from one Active Directory site (strSourceSiteRDN) to another Active Directory site (strTargetSiteRDN).

strSourceSiteRDN = "cn=Default-First-Site-Name"
strTargetSiteRDN = "cn=ON-TOR-Marketing"
strDcRDN         = "cn=atl-dc-01"

Set objRootDSE = GetObject("LDAP://RootDSE")

strConfigurationNC = objRootDSE.Get("configurationNamingContext")

strDcPath = "LDAP://" & strDcRDN & ",cn=Servers," & strSourceSiteRDN & _

    ",cn=Sites," & strConfigurationNC

strTargetSitePath = "LDAP://cn=Servers," & strTargetSiteRDN & _

    ",cn=Sites," & strConfigurationNC

Set objTargetSite = GetObject(strTargetSitePath)

objTargetSite.MoveHere strDcPath, strDcRDN

Rename an Active Directory Site

Renames an Active Directory site.

strOldSiteRDN = "cn=Default-First-Site-Name"
strNewSiteRDN = "cn=ON-TOR-Marketing"

Set objRootDSE = GetObject("LDAP://RootDSE")

strConfigurationNC = objRootDSE.Get("configurationNamingContext")

strSitesContainer = "LDAP://cn=Sites," & strConfigurationNC

strOldSitePath = "LDAP://" & strOldSiteRDN & ",cn=Sites," & strConfigurationNC

Set objSitesContainer = GetObject(strSitesContainer)

objSitesContainer.MoveHere strOldSitePath, strNewSiteRDN

Verify that a Domain Controller is in a Site

Checks to see if a domain controller is in a specific Active Directory site

strDcName = "ont-dc-01"
strSiteName = "ga-atl-sales"

Set objADSysInfo = CreateObject("ADSystemInfo")

strDcSiteName = objADSysInfo.GetDCSiteName(strDcName)

If UCase(strSiteName) = UCase(strDcSiteName) Then

    WScript.Echo "TRUE: " & strDcName & " is in site " & strSiteName
Else
    WScript.Echo "FALSE: " & strDcName & " is NOT in site " & strSiteName
End If