Technique: Determining Replication Schedules

Doctor Scripto

Summary: Windows PowerShell MVP, Richard Siddaway, shares another excerpt from his book PowerShell in Practice.

Microsoft Scripting Guy, Ed Wilson, is here. This week we will not have our usual PowerTip. Instead we have excerpts from seven books from Manning Press. In addition, each blog will have a special code for 50% off the book being excerpted that day. Remember that the code is valid only for the day the excerpt is posted. The coupon code is also valid for a second book from the Manning collection.

Today, the excerpt is from PowerShell in Practice
  By Richard Siddaway

Photo of book cover

It’s a fair assumption to say that Microsoft Office applications will be found on almost every computer desktop in work environments. It’s possible to work with most of the Office applications by using Windows PowerShell. There are COM objects representing most of them. In this technique from PowerShell in Practice, author Richard Siddaway explains how to see replication schedules of a site link with more detail than with GUI tools.

When a site link is created, a replication interval (default 180 minutes) and a schedule (default 24 × 7) are created. The schedule controls when replication can start, not when replication can happen. If the schedule is set for only 1:00 to 2:00 A.M., replication can start during that period; but once started, it will continue until finished even if that goes beyond 2:00 A.M.

Accessing the schedule in the GUI is awkward in that Active Directory Sites and Services has to be opened, then we have to drill down into the transport mechanisms to find the site link, open its properties, and finally click the Schedule button. This will show the schedule on an hourly basis for each day of the week.

Additionally, we can’t use the InterSiteReplicationSchedule property, because if a schedule is set as 24 × 7, nothing shows when you list the InterSiteReplicationSchedule property. If it’s set to anything else, we get System.DirectoryServices.ActiveDirectory.ActiveDirectorySchedule returned instead of the actual schedule. Let’s write a script that will sort this out for us.

Problem

We want an easy way to see the replication schedules of our site link. Ideally we want the display to show more detail than the GUI tools.

Solution

We need to unravel the way Active Directory stores the schedule information to get to that display. The script to do so is shown in Listing 1.

Listing 1: Display replication schedule

$sched = @()    #1

$days = “Sunday”, “Monday”, “Tuesday”,

“Wednesday”, “Thursday”, “Friday”, “Saturday”    #1

hours = ” “*11

 

for ($j=0; $j -le 23; $j++){$hours += “{0,-4}” -f $j}

$sched += $hours      #2

 

$for =

[System.DirectoryServices.ActiveDirectory.Forest]::

GetCurrentForest()    #3

 

$fortyp =

[System.DirectoryServices.ActiveDirectory.`

DirectoryContexttype]”forest”   #3

 

$forcntxt = New-Object

System.DirectoryServices.ActiveDirectory.`

DirectoryContext($fortyp, $for)       #3

 

$link =

[System.DirectoryServices.ActiveDirectory.`

ActiveDirectorySiteLink]::

FindByName($forcntxt, “MyNewSite3-MyNewSite4”)     #4

 

for ($i=0; $i -le 6; $i++) { #days    #5

    $out = “”

    $out += $days[$i].PadRight(11)

    for ($j=0; $j -le 23; $j++) { #hours     #6

        for ($k=0; $k -le 3; $k++) { #15 minutes      #7

            if ($link.InterSiteReplicationSchedule.     #8

                        RawSchedule.psbase.GetValue($i,$j,$k)){$out += “Y”}

            else {$out += “n”}      #9

        }

    }

    $sched += $out    #10

}

$sched     #11

Discussion

I like this script because it gives me more information than the GUI and makes that information easier to access. The following display shows the replication schedule for 15-minute intervals through the whole week.

Image of command output

The numbers across the top row are the hours of the day (24-hour clock). I chose to show when replication is allowed with a capital Y; and when it isn’t, I use with a lowercase n. This makes the replication schedule easier to understand.

It’s time to see how we get to this display. We start by creating a couple of arrays (#1). The first is empty and will hold the schedule data, whereas the second holds the days of the week. If you don’t want to type the days of the week into a script like this, you can generate them this way:

$days = 0..6 | foreach{([System.DayofWeek]$_).ToString()}

Use the range operator to pipe the numbers 0 through 6 into ForEach. The System.DayofWeek enumeration is used to generate the name of the week day.

The next job is to create the top row of the table that holds the hours. Our starting point is the $hours variable, which has 11 spaces. This is padding to allow for the column of day names in the table. The values are simply numbers, so we can use a loop to put each integer value into a four-character field by using the f operator and the .NET string formatting functionality. It’s then appended to the $hours variable. After completed, the $hours variable is appended to the array holding the schedule (#2). We then need to generate a forest context (#3) by going through the usual steps to create it.

The ActiveDirectorySiteLink class has a FindByName() method that uses the forest context and the name of the link (#4). A site link has an InterSite-ReplicationSchedule.RawSchedule property consisting of 672 Boolean entries in a three-dimensional array. Each value represents a period of 15 minutes counted sequentially from 00:00 on Sunday. We can use a set of nested loops to unravel these entries.

The outer loop (#5) counts through the seven days of the week. The processing for each day initializes an empty string as an output variable and adds the day of the week name to it. We pad the name to 11 characters to make everything line up.

It’s much easier to read that way. The middle loop counts through the hours of the day (#6), and the inner loop counts the 15-minute blocks of each hour (#7). The correct value is retrieved from the schedule by using the loop counters as indices (#8). If set to True, the output variable has a Y appended, and if it’s False, an n is appended (#9).

At the end of the loop representing the days, the output variable is appended to our schedule array (#10).

When all 672 values have been processed, we can display the schedule (#11) to produce the display shown previously.

~Richard 

Here is the code for the discount offer today at www.manning.com: scriptw3
Valid for 50% off PowerShell in Practice and Learn Windows PowerShell in a Month of Lunches
Offer valid from April 3, 2013 12:01 AM until April 4 midnight (EST)

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

 

 

0 comments

Discussion is closed.

Feedback usabilla icon