Thursday, April 19, 2012

PowerShell Script to Set Public Folder Replication Schedule Recursively in Exchange 2007

I've recently been tasked to replicate a lot of Exchange 2007 Public Folder data to another server.  I'll blog the steps here shortly, but first, I had to write a quick power-shell script to do it, as there were over 100 public folders in the root public folder, and they were all set to not replicate.

I had already used the MS script AddReplicaToPFrecursive.ps1, but it doesn't set the replication schedule if it wasn't set before.

This script will walk through all of your public folders and set the replication  schedule to "always"

It logs to C:\set_pf_log.txt and you can see two work files it also makes in C:\ - modify as you wish.

It's my first PowerShell script, so be gentle. :-) I don't know how to handle wrapping long folder names from the Get-PublicFolders command, so it won't process a long PF name.

# Set Public Folder Replication Schedules Recursively
#
# NOTE: If you have very long public folder names, they may wrap, and will not be handled correctly here.
#
# Define Varables
#

$filename_log = "C:\set_pf_log.txt"
$filename_pf_long = "C:\set_pf_long.txt"
$filename_pf_short = "C:\set_pf_short.txt"

#
# First, get a list of all the public folders and write them to $filename_pf_ong
#
write-host "Fetching Public Folders and writing to $filename_pf_long"
write-host "Fetching Public Folders and writing to $filename_pf_long" | out-file $filename_log -append

get-publicfolder "\" -recurse | fl identity > $filename_pf_long

#
# Now we need to trim the file, as it also outputs 'Identity : ' which we don't want
#

write-host "Trimming $filename_pf_long into $filename_pf_short"
write-host "Trimming $filename_pf_long into $filename_pf_short" | out-file $filename_log -append


get-content $filename_pf_long | foreach-object {$_ -replace 'Identity : ',''} > $filename_pf_short

# Now loop through the file, executing our action for each public folder
#
# NOTE - The action is commented out, using write-host to output to the console instead. If you want to make this actually run, remove the # in front of set-publicfolder
#

foreach($line in get-content $filename_pf_short)
{
    if ($line -ne "")   
        {
        write-output "Processing $line"
        write-output "Processing $line" | out-file $filename_log -append
#         set-publicfolder $line -ReplicationSchedule Always
        }
}

write-host "Complete!"


1 comment:

  1. You could allso use:
    get-publicfolder -identity "folderpath" -Recurse -resultsize unlimited | set-publicfolder -ReplicationSchedule Always

    ReplyDelete