Skip to content

Instantly share code, notes, and snippets.

@davegreen
Last active February 2, 2016 08:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save davegreen/b4b376e50b24c6a1561c to your computer and use it in GitHub Desktop.
A function that will return the AD site the local computer is currently connected to. Useful for PowerShell login scripts that need to target specific sites.
Function Get-ComputerADSite()
{
<#
.Synopsis
Get the computers current AD site from the computer Netlogon information.
.Example
Get-ComputerADSite
.Notes
Author: David Green
#>
[CmdletBinding()]
param()
Write-Verbose 'Getting the computers current AD site from HKLM:\SYSTEM\CurrentControlSet\services\Netlogon\Parameters.'
[string]$adsite = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\services\Netlogon\Parameters' -name DynamicSiteName).DynamicSiteName
#Trim the last 2 characters from the site as this registry value seems to have " R" appended.
$adsite.Substring(0, ($adsite.Length -2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment