Skip to content

Instantly share code, notes, and snippets.

@bill-long
Created April 21, 2015 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bill-long/154702cb47cba72474a0 to your computer and use it in GitHub Desktop.
Save bill-long/154702cb47cba72474a0 to your computer and use it in GitHub Desktop.
Watch AD for changes
$lastHighestUSN = 0
$rootDSE = [ADSI]("LDAP://RootDSE")
$lastHighestUSN = $rootDSE.highestCommittedUSN
"Starting highest USN: " + $lastHighestUSN
$configNC = [ADSI]("LDAP://" + $rootDSE.configurationNamingContext)
while ($true)
{
$rootDSE = [ADSI]("LDAP://RootDSE")
$highestUSN = $rootDSE.highestCommittedUSN
if ($lastHighestUSN -lt 1) { $lastHighestUSN = $highestUSN }
if ($highestUSN -gt $lastHighestUSN)
{
"New highest USN: " + $highestUSN
$filter = "(&(USNChanged >= " + $lastHighestUSN + ")(USNChanged <= " + $highestUSN + "))"
$finder = new-object System.DirectoryServices.DirectorySearcher($configNC, $filter)
$finder.PageSize = 100
$results = $finder.FindAll()
if ($results.Count -gt 0)
{
"Objects changed:"
foreach ($result in $results)
{
$result.Path
}
}
else
{
"No objects changed in Config context."
}
}
$lastHighestUSN = $highestUSN
Start-Sleep 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment