Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

UpToDateness Vector (UTDV) PowerShell exercise

I did see this post at joeware.com : UpToDateness Vector (UTDV) that was a follow up, at the PowerShell examples Brandon posted . As needed a break from my activities at that moment, To get my head clear, I translated it to PowerShell.

Note that this was just meant as a PowerShell exercise for me, I did not look into the original Problem that much For a detailed background see Brandon and joe's posts and/or the discussion thread.

I started with translating the first part of joe's command :

adfind -default

This I did translate to :

$ds = new-object DirectoryServices.DirectorySearcher

Note : I did not pick the type adapter ... :

[AdsiSearcher]''

... here, as this defaults to the Filter parameter, hence overwriting the default filter, an effect I did not want here.

As in this case as we pick "my default domain on my default domain controller…" this covers the -default switch also.

next this was to cover the -Base switch in the Adfind command As this parameter does set the SearchScope to Base, we do the same in PowerShell by setting the SearchScope property on our DirectorySearcher object :

$ds.SearchScope = 'base'

Note : this is not strictly needed in our case, as the first object returned from our query is the object itself anyway, so we could leave it away but in this case it is good to be clear about it

So we go on to complete the first ADfind command :

adfind -default -s base msDS-NCReplCursors

the last argument here does specify the property to look for, let's do this in PowerShell also :

$ds.PropertiesToLoad.add('msDS-NCReplCursors')

Note here that this step is mandatory, as this attribute is "Lazy" and not retrieved by default by the searcher.

so new we have prepared our DirectorySeacher object, let's invoke it :

$r = $ds.findone().properties.'msds-ncreplcursors'

Note that, I select the property directly on the result of the method here from the resulting properties collection here (adspath is always returned).

As joe mentioned : "That will work from any LDAP query tool you want to use…", but this is not really friendly to work with.

Now did Adfind  have a nice ;Binary option joe shows in his post :

adfind -default -s base msDS-NCReplCursors;binary

I'm not sure how this works exactly bust this gives a much cleaner output, that I also wanted to get from PowerShell.

As you might have see the output returned looks a lot like XML, so I first tried to directly cast it into a XML object by using [XML]

this did complain that there was no root node, hence I added one by embedding the variable into a string like this :

$xml = [xml]"<root>$r</root>"

And yes, this did the trick ! the result looks like this :

image

So far so good, and as we have an XML object now the result is not text but properties.

for in this case the given date is not in a handy format, hence I wanted to translate the Date into a Datetime object, so I used select and calculated properties to convert this also :

$xml.root.DS_REPL_CURSOR | select usnAttributeFilter,@{n='Date';e={get-date $_.ftimeLastSyncSuccess}},pszSourceDsaDN

and now I can do things like this with the data :

image

I sort the result's sorted on the Date and select the first 3 with the following command.

$xml.root.DS_REPL_CURSOR | select usnAttributeFilter,@{n='Date';e={get-date $_.ftimeLastSyncSuccess}},pszSourceDsaDN | sort date -des | select -first 3

this is as far as my 10 minute PowerShell exercise did go, I was loaded up again enough to get to my real work again ;-) but you can see that having created structured data from the returned "XML blob", can be very useful , and it is easy to improve this console exercise, by doing a bit more processing and building it into a function.

For reference the complete code used :

# Get info

$ds = new-object DirectoryServices.DirectorySearcher
$ds.SearchScope = 'base'
$ds.PropertiesToLoad.add('msDS-NCReplCursors')

# Convert to XML

$r = $ds.findone().properties.'msds-ncreplcursors'
$xml = [xml]"<root>$r</root>"

# Usage
$xml.root.DS_REPL_CURSOR

# selecting and converting properties, sorting and filtering

$xml.root.DS_REPL_CURSOR | select usnAttributeFilter,@{n='Date';e={get-date $_.ftimeLastSyncSuccess}},pszSourceDsaDN | sort date -des | select -first 3

Ok a bit more as :

adfind -default -s base msDS-NCReplCursors;binary

but we did create structured data in the PowerShell example, that we can easy work with the data in PowerShell, as I showed selecting, sorting, filtering the data is a trivial task from here.

Enjoy,

Greetings /\/\o\/\/

Published Tuesday, August 12, 2008 1:30 PM by MoW

Comments

# PowerTab V2 Examples

&#160; Did not find much time for improvements to the Alpha Build this week, but some examples working

Tuesday, August 12, 2008 4:03 PM by The PowerShell Guy
Anonymous comments are disabled