Query site for given ip from AD sites and services

6,996

nltest /server:servername /dsgetsite will give you the site name by querying AD.

You could wrap it in Powershell to use it programmatically more easily:

function Get-SystemSite($SystemName)
{
    $site = nltest /server:$SystemName /dsgetsite 2>$null
    if($LASTEXITCODE -eq 0) { $site[0] }
}
Share:
6,996

Related videos on Youtube

Hagen von Eitzen
Author by

Hagen von Eitzen

Updated on September 18, 2022

Comments

  • Hagen von Eitzen
    Hagen von Eitzen over 1 year

    Windows Active Directory maintains a list of subnet to sites assignments (in "Active Directory - Sites and Services" -> Sites -> Subnets). The subnet-to-site associationas ar used, obviously, by workstations in order to select a suitable DC to authenticate with. But how can this information be used programmatically for other purposes? For example: Given an IP, I would like to obtain things like the Site name as found in the properties in the best matching subnet. With what have found so far, I could list all subnets and do the subnet matching with all of them myself - but as this task is in one way or other performed with every workstation reboot, I am sure that there is some direct method for this ...

  • Hagen von Eitzen
    Hagen von Eitzen almost 10 years
    Thanks. This was not precisely what I was looking for (or my question was not clear enough), but with this as a starting point I now found nltest /DSADDRESSTOSITE: ipaddress and DsAddressToSiteNames()
  • phoebus
    phoebus almost 10 years
    @HagenvonEitzen Sorry bout that, for some reason I had hostname on the brain rather than IP.