Skip to content

Instantly share code, notes, and snippets.

@JaekelEDV
Last active February 14, 2020 10:53
Show Gist options
  • Save JaekelEDV/d37068f211e383d6fa5de2db0066c908 to your computer and use it in GitHub Desktop.
Save JaekelEDV/d37068f211e383d6fa5de2db0066c908 to your computer and use it in GitHub Desktop.
#https://superuser.com/questions/758372/query-site-for-given-ip-from-ad-sites-and-services
#Found this one - and it's working. But... I don't know why it does. Let's see...
#This is the function:
Function Get-AdSiteAndSubnetFromIP {
<#
.Synopsis
Get the matching AD Site and Subnet for a given IP Address
.DESCRIPTION
Get the matching AD Site and Subnet for a given IP Address. The results will be returned as a Hash.
.EXAMPLE
Get-AdSiteAndSubnetFromIP -ip 172.28.68.53
ADSite ADSubnet
------ --------
SiteA 10.1.0.0/16
.EXAMPLE
(Get-AdSiteAndSubnetFromIP -ip 172.28.68.53).ADSite
SiteA
#>
param([string]$ip
)
$site = nltest /DSADDRESSTOSITE:$ip /dsgetsite 2>$null
if ($LASTEXITCODE -eq 0) {
$split = $site[3] -split "\s+"
# validate result is for an IPv4 address before continuing
if ($split[1] -match [regex]"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$") {
"" | select @{l="ADSite";e={$split[2]}}, @{l="ADSubnet";e={$split[3]}}
}
}
}
#Ran it from within the ISE and it shows this nice output:
PS C:\>Get-AdSiteAndSubnetFromIP -ip 10.0.0.1
ADSite ADSubnet
------ --------
Default-First-Site-Name 10.0.0.0/24
#Let's dissect it a bit:
PS C:\> $site = nltest /DSADDRESSTOSITE:$ip /dsgetsite 2>$null
PS C:\> $site
Default-First-Site-Name
Ruft Standort-/Subnetzzuordnung fr lokalen Computer von '\\DC1.test.local' ab.
fe80::12d:a39c:abe9:a071%6 Default-First-Site-Name (null)
10.0.0.1 Default-First-Site-Name 10.0.0.0/24
Der Befehl wurde ausgefhrt.
#So far, so good. Let's dig deeper. Insert a closing brace, run this, and ask for the var:
if ($LASTEXITCODE -eq 0) {
$split = $site[3] -split "\s+"}
PS C:\> $split
fe80::12d:a39c:abe9:a071%6
Default-First-Site-Name
(null)
#I understand the regex \s+ removes all the whitespace
#Question 1: Index [3] returns the fe80 because in line 49 here it is row 4 ([0], [1]...[3])
#Right?
PS C:\> $site[3]
fe80::12d:a39c:abe9:a071%6 Default-First-Site-Name (null)
#But now - I'd expected the following result if it would have been $split[0, $split[1]] and $split[2]
#What am I missing here?
PS C:\> $split[1]
fe80::12d:a39c:abe9:a071%6
PS C:\> $split[2]
Default-First-Site-Name
PS C:\> $split[3]
(null)
#And: the fe80 doesn't match the regex from line 29 here. This is for an v4 address...
#Also I don't understand the "" left of the pipe:
{"" | select @{l="ADSite";e={$split[2]}}, @{l="ADSubnet";e={$split[3]}}
#And with all seen, why is the (obviously desired) output when running the function this:
PS C:\> Get-AdSiteAndSubnetFromIP -ip 10.0.0.1
ADSite ADSubnet
------ --------
Default-First-Site-Name 10.0.0.0/24
#I mean, it's the desired result, but $split doesn't show the v4 address, nor does it split the v4-site.
#Lost...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment