Created
April 21, 2015 21:18
-
-
Save bill-long/154702cb47cba72474a0 to your computer and use it in GitHub Desktop.
Watch AD for changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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