{"id":244,"date":"2014-09-25T15:17:34","date_gmt":"2014-09-25T19:17:34","guid":{"rendered":"http:\/\/adsecurity.org\/?p=244"},"modified":"2014-10-01T21:00:52","modified_gmt":"2014-10-02T01:00:52","slug":"powershell-code-find-active-directory-site-containing-ad-subnet","status":"publish","type":"post","link":"https:\/\/adsecurity.org\/?p=244","title":{"rendered":"PowerShell Code: Find Active Directory Site Containing AD Subnet"},"content":{"rendered":"<p>Here&#8217;s a quick script that returns the site in the Active Directory forest given a subnet (ex. 10.20.30.0).<\/p>\n<p><a href=\"https:\/\/adsecurity.org\/wp-content\/uploads\/2014\/09\/Match-Subnet2Site.ps1_.txt\">Match-Subnet2Site.ps1<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><code><br \/>\nParam<br \/>\n(<br \/>\n[string]$Subnet<br \/>\n)<\/p>\n<p>$IPSubnetRegEx = '\\b((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0)\\b'<br \/>\n# $IPRegEx = '\\b((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\b'<\/p>\n<p>IF ($Subnet -match $IPSubnetRegEx)<br \/>\n{ Write-Output \"Searching the AD forest for subnet: $Subnet \" }<br \/>\nELSE<br \/>\n{ Write-Error \"The provided subnet ($Subnet) is not valid. Please enter as follows #.#.#.0 (ex. 10.22.33.0)\" }<\/p>\n<p>$ADForestName = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Name<br \/>\n$DomainDNS = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name<\/p>\n<p>$ADSites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites<br \/>\n$ADSites = $ADSites | sort-Object Name<br \/>\n[int]$ADSitesCount = $ADSites.Count<br \/>\nWrite-output \"Searching $ADSitesCount AD Sites in $ADForestName `r\"<\/p>\n<p>[string]$SearchResults = \"Subnet $Subnet could not be found in the current Active Directory forest ($ADForestName)\"<br \/>\nForEach ($ADSitesItem in $ADSites)<br \/>\n{ ## OPEN ForEach ($ADSitesItem in $ADSites)<br \/>\n$ADSitesItemName = $ADSitesItem.Name<br \/>\n$ADSitesItemSubnetsCount = $ADSitesItem.Subnets.Count<br \/>\nIF ($ADSitesItem.Subnets.Count -gt 1)<br \/>\n{ ## OPEN IF ($ADSitesItem.Subnets.Count -gt 1)<br \/>\n$ADSitesItemSubnetsArray = $ADSitesItem.Subnets<br \/>\nWrite-Verbose \"The site $ADSitesItemName has $ADSitesItemSubnetsCount subnets \"<br \/>\nForEach ($ADSitesItemSubnetsItem in $ADSitesItemSubnetsArray)<br \/>\n{ ## OPEN ForEach ($ADSitesItemSubnetsItem in $ADSitesItemSubnets)<br \/>\n$ADSitesItemSubnets = $ADSitesItemSubnetsItem.Name<br \/>\n$ADSitesItemSubnetSite = $ADSitesItemSubnetsItem.Site<br \/>\n$ADSitesItemSubnetLocation = $ADSitesItemSubnetsItem.Location<br \/>\nWrite-Verbose \"Checking Site $ADSitesItemName subnet $ADSitesItemSubnets\"<br \/>\nIF ($ADSitesItemSubnets -like \"*$Subnet*\")<br \/>\n{ [string]$SearchResults = \"The subnet $Subnet is configured as part of the AD site $ADSitesItemName ($ADSitesItemSubnetLocation)\" }<br \/>\n} ## CLOSE ForEach ($ADSitesItemSubnetsItem in $ADSitesItemSubnets)<br \/>\n} ## CLOSE IF ($ADSitesItem.Subnets.Count -gt 1)<br \/>\nELSE<br \/>\n{ ## OPEN ELSE ($ADSitesItem.Subnets.Count -lt 1)<br \/>\n$ADSitesItemSubnets = $ADSitesItem.Subnets[0].Name<br \/>\n$ADSitesItemSubnetSite = $ADSitesItem.Subnets[0].Site<br \/>\n$ADSitesItemSubnetLocation = $ADSitesItem.Subnets[0].Location<\/p>\n<p>Write-Verbose \"Checking Site $ADSitesItemName single subnet $ADSitesItemSubnets\"<br \/>\nIF ($ADSitesItemSubnets -like \"*$Subnet*\")<br \/>\n{ [string]$SearchResults = \"The subnet $Subnet is configured as part of the AD site $ADSitesItemName ($ADSitesItemSubnetLocation)\" }<br \/>\n} ## CLOSE ELSE ($ADSitesItem.Subnets.Count -lt 1)<\/p>\n<p>[array]$ADSitesItemSubnetsArray = $ADSitesItemSubnets -Split(\", \")<\/p>\n<p>} ## CLOSE ForEach ($ADSitesItem in $ADSites)<\/p>\n<p>return $SearchResults<\/p>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a quick script that returns the site in the Active Directory forest given a subnet (ex. 10.20.30.0). Match-Subnet2Site.ps1 &nbsp; Param ( [string]$Subnet ) $IPSubnetRegEx = &#8216;\\b((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0)\\b&#8217; # $IPRegEx = &#8216;\\b((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\b&#8217; IF ($Subnet -match $IPSubnetRegEx) { Write-Output &#8220;Searching the AD forest for subnet: $Subnet &#8221; } ELSE { Write-Error &#8220;The provided subnet ($Subnet) is not &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/adsecurity.org\/?p=244\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[20,88,89,22],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-activedirectory","tag-adsite","tag-adsubnet","tag-powershellcode","item-wrap"],"_links":{"self":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=244"}],"version-history":[{"count":5,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/244\/revisions\/253"}],"wp:attachment":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}