Skip to content

Instantly share code, notes, and snippets.

@bill-long
Last active January 11, 2021 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bill-long/6aeaffa3f7bee7c78efbe17c0445862f to your computer and use it in GitHub Desktop.
Save bill-long/6aeaffa3f7bee7c78efbe17c0445862f to your computer and use it in GitHub Desktop.
# Move-ResidentPublicFolders
#
# This script can be used to move all public folders from one mailbox
# to another in batches. Simply run the script, wait for the move to
# complete, then run the script again. Repeat until all folders are
# moved.
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]
$SourceMailbox,
[Parameter(Mandatory=$true)]
[string]
$TargetMailbox,
[Parameter(Mandatory=$false)]
[uint32]
$BatchSize=500
)
# Make sure source and destination PF mailboxes exit
$source = Get-Mailbox -PublicFolder $SourceMailbox
if ($null -eq $source -or $source.Count -gt 1) {
Write-Error "Source mailbox is invalid."
return
}
$target = Get-Mailbox -PublicFolder $TargetMailbox
if ($null -eq $target -or $target.Count -gt 1) {
Write-Error "Destination mailbox is invalid."
return
}
# Make sure there are no existing moves for these
$existingMoveRequests = Get-PublicFolderMoveRequest -ResultSize Unlimited
$blockingMoveRequests = @()
$completedMoveRequests = @()
$badMoveRequests = @()
foreach ($request in $existingMoveRequests) {
if ($request.SourceMailbox.ToString() -eq $source.Id.ToString() -or
$request.SourceMailbox.ToString() -eq $target.Id.ToString() -or
$request.TargetMailbox.ToString() -eq $source.Id.ToString() -or
$request.TargetMailbox.ToString() -eq $target.Id.ToString()) {
if ($request.Status -ne "Completed") {
$blockingMoveRequests += $request
} else {
$completedMoveRequests += $request
}
}
if ($null -eq $request.SourceMailbox -or $null -eq $request.TargetMailbox) {
$badMoveRequests += $request
}
}
if ($blockingMoveRequests.Count -gt 0) {
Write-Warning "The source or target mailbox is already in use by the following
move requests, which are not yet completed. Another request
cannot be created."
Start-Sleep -Milliseconds 200
$blockingMoveRequests
return
}
if ($badMoveRequests.Count -gt 0) {
Write-Warning "The following move requests have a null source or target mailbox.
This can cause the New-PublicFolderMoveRequest cmdlet to fail, so these
should be removed before proceeding."
$badMoveRequests | Format-Table | Out-String | ForEach-Object { Write-Host $_ }
# This will prompt
$badMoveRequests | Remove-PublicFolderMoveRequest
}
if ($completedMoveRequests.Count -gt 0) {
Write-Warning "The source or target mailbox is already in use by the following
move requests. These requests are completed and can be removed."
$completedMoveRequests | Format-Table | Out-String | ForEach-Object { Write-Host $_ }
# This will prompt
$completedMoveRequests | Remove-PublicFolderMoveRequest
}
$foldersToMove = Get-PublicFolder -Mailbox $source -ResidentFolders -Recurse -ResultSize ($BatchSize + 1) -WarningAction SilentlyContinue |
Select-Object -Skip 1 |
ForEach-Object { $_.Identity.ToString() } |
Select-Object -Unique
if ($foldersToMove.Count -lt 1) {
Write-Host "There are no resident folders in the source mailbox."
return
}
Write-Host "Moving $($foldersToMove.Count) folders from $source to $target."
New-PublicFolderMoveRequest -Folders $foldersToMove -TargetMailbox $target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment