# Get all replication groups
$replicationgroups = dfsradmin rg list;
# Reduce loop by 3 lines to filter out junk from dfsradmin
$i = 0;
$imax = ($replicationgroups.count -3);
# Loop through each replication group
foreach ($replicationgroup in $replicationgroups) {
# Exclude first and last two lines as junk, and exclude the domain system volume
if (($i -ge 1) -and ($i -le $imax) -and ($replicationgroup -notlike "*domain system volume*")) {
# Format replication group name
$replicationgroup = $replicationgroup.split(" ");
$replicationgroup[-1] = "";
$replicationgroup = ($replicationgroup.trim() -join " ").trim();
# Get and format replication folder name
$replicationfolder = & cmd /c ("dfsradmin rf list /rgname:`"{0}`"" -f $replicationgroup);
$replicationfolder = (($replicationfolder[1].split("\"))[0]).trim();
# Get servers for the current replication group
$replicationservers = & cmd /c ("dfsradmin conn list /rgname:`"{0}`"" -f $replicationgroup);
# Reduce loop by 3 lines to filter out junk from dfsradmin
$j = 0;
$jmax = ($replicationservers.count -3);
# Loop through each replication member server
foreach ($replicationserver in $replicationservers) {
# Exclude first and last two lines as junk
if (($j -ge 1) -and ($j -le $jmax)) {
# Format server names
$sendingserver = ($replicationserver.split(" "))[0].trim();
$receivingserver = ($replicationserver.split(" "))[2].trim();
# Get backlog count with dfsrdiag
$backlog = & cmd /c ("dfsrdiag backlog /rgname:`"{0}`" /rfname:`"{1}`" /smem:{2} /rmem:{3}" -f $replicationgroup, $replicationfolder, $sendingserver, $receivingserver);
$backlogcount = ($backlog[1]).split(":")[1];
# Format backlog count
if ($backlogcount -ne $null) {
$backlogcount = $backlogcount.trim();
}
else {
$backlogcount = 0;
}
# Create output string to <replication group> <sending server> <receiving server> <backlog count>;
$outline = $replicationgroup + " From: " + $sendingserver + " To: " + $receivingserver + " Backlog: " + $backlogcount;
$outline;
}
$j = $j + 1;
}
}
$i = $i + 1;
}
Very helpful. Thanks for the script.
Hi Kamal,
I found that in some cases (and I think this occurred with longer server names) that the Trim whitespace function was not yeilding the correct results.
I found that in some cases there were numerous additional whitespaces so I changed the code to (on line 37 and 38):
$sendingserver = ($replicationserver.split()| where {$_})[0].trim();
$receivingserver = ($replicationserver.split()| where {$_})[1].trim();
I found if all of my servers had the same length names, your original code wouldn’t be a problem; but if some had different length names then this issue would occur because on a few lines of code there would be 2 spaces between sending and receiving server and then on other lines of code there would be 4 spaces between sending and receiving server due to being shorter length names on that column.
Thanks
Tawney
Excellent pick-up.
I can’t seem to get this working on 2008 R2 servers, only 2012 R2. For example, the output below. I tried Tawney’s tweak but that didn’t help either. Perhaps a Powershell version issue?
Replication group with name Support cannot be found.
Cannot index into a null array.
At C:\Users\administrator\Desktop\dfsr_replication.ps1:21 char:51
+ $replicationfolder = (($replicationfolder[ <<<< 1].split("\"))[0]).trim();
+ CategoryInfo : InvalidOperation: (1:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Looks like the returned object from the dfsradmin command isn’t working correctly. I’d start troubleshooting this by running the command separately (and determining where to go from there) – ie; dfsradmin rf list /rgname:
This script is exactly what I was looking for. Is there an easy way to take that output and email it through PS? Creating a scheduled task to send a daily email would be beneficial.
Thanks in advance.
Creating a schedule task is pretty straightforward, just need to save the script in a .ps1 file, then call powershell.exe -f filename.ps1 within the scheduled task. The command to send emails is send-mailmessage (https://technet.microsoft.com/en-us/library/hh849925.aspx) – and I like using the -BodayAsHTML option.
I don’t like any of the automated conversions to HTML, so I often build the HTML file as a variable, as I go. EG:
– and then start appending to that variable as you loop through the data in the regular script (usually adding tables, and tr elements inside the loops). This variable then becomes the body of your email.
A couple of things to be aware of, as well:
– you need the change the Remote Execution Policy to allow the script to run from a scheduled task (https://technet.microsoft.com/en-us/library/hh849812.aspx). Something like “set-executionpolicy unresitricted -scope LocalMachine”.
– You need to set the scheduled task to allow it to run when no one is logged in with elevated privileges (checkboxes in Task Scheduler).
– The account you’re running the scheduled task as, needs “log on as a batch job” rights (Local Admin, or set for a specific account with these rights through Local Policy or Group Policy). (https://technet.microsoft.com/en-us/library/cc957131.aspx)
I know you mention the send-mailmessage to send the results via email but where in the script do I enter that command? Sorry still new PS scripting.