On Demand Migration - Powershell API - Example script - Public Folder Migration

The examples below assume you have already installed ODM Powershell module.

How to install ODM API Powershell

Note that these scripts are provided as-is for example purposes only, and you may need to modify them to work for your specific project.

The first script will create tasks. The second script will run them.

Create tasks:

$Region = "EU" # "UK","AU","US","EU","Canada" 
$OrganizationID = "ee8b3eb6-79e3-648d-1b57-4fef624b75c5"
$ProjectID = "9G7DSZABS0YNcR2MxwfL" # Please note, the ProjectID is always the ID of the Accounts/MBX/OD project
$TaskName = "PF Migration"
$NumberOfObjects = 5 # Defines the number of Public Folders will be added to a single task

# Connect and select Public Folder Projectworkload
Connect-OdmService -Region $Region
Select-OdmOrganization -Organizationid $OrganizationID
Get-OdmProject -ProjectId $ProjectID | Get-OdmProjectWorkload -Type PublicFolders | Select-OdmProjectWorkload


# Create task(s) for migrating Public Folders #
#############################################
# The below script will create tasks based on the setting in NumberOfObjects variable.

# Download every object from project
$AllObjects = Get-OdmPfPublicFolder -All
Write-Host "All Objects count:"$AllObjects.Count

# Filter for objects where Status is New
$Objects = $AllObjects | Where-Object { $_.Status -eq "New" }
Write-Host "Available Objects to create task count:"$Objects.Count
$SegmentCount = 1; $SegmentStart = 0
# Calculate how many tasks will be created
$WillCreate = [math]::ceiling($Objects.Count / $NumberOfObjects)

Do {
    $Objects2Add = $Objects | Select-Object -Skip $SegmentStart -First $NumberOfObjects
    $CurrTaskName = ($TaskName + " " + $SegmentCount + " / " + $WillCreate)
    Write-Host $SegmentCount" / "$WillCreate

    # Defines the tasks settings
    $PFMigrationParam = @{
        Name                       = $CurrTaskName
        MigrateContent             = $true
        MigrateMailEnabled         = $true
        MigrateSubfolders          = $false
        MigrateFolderPermissions   = $true
        MigrateMailboxPermissions  = $true
        NotifyOnTaskCompletion     = $false
        NotifyOnlyOnTaskFailure    = $false
        NotificationRecipientEmail = "example@domain.com"
        ScheduledStartTime         = $null
    }

    $CurrTask = New-OdmPfMigrationTask @PFMigrationParam


    # Adding the defined number of accounts to the task
    $CurrTask | Add-OdmObject -object $Objects2Add -SubEntity "publicfolders"
    $SegmentStart = $SegmentStart + $NumberOfObjects
    $SegmentCount = $SegmentCount + 1
}While ($Objects.Count -gt $SegmentStart)
#########################

Run the created task(s):

$Region = "EU" # "UK","AU","US","EU","Canada" 
$OrganizationID = "ee8b3eb6-79e3-648d-1b57-4fef624b75c5"
$ProjectID = "9G7DSZABS0YNcR2MxwfL" # Please note, the ProjectID is always the ID of the Accounts/MBX/OD project
$TaskType2Run = "Public Folder Migration" # Types for example, "Mail Migration", "OneDrive Migration"
$MaxRunningTasks = 5
$SleepMinute = 5 # Wait time before getting task info as it's looped

# Connect and select Public Folder Projectworkload
Connect-OdmService -Region $Region
Select-OdmOrganization -Organizationid $OrganizationID
Get-OdmProject -ProjectId $ProjectID | Get-OdmProjectWorkload -Type PublicFolders | Select-OdmProjectWorkload

# Automatically Start New tasks #
#################################
# When New tasks are created (without scheduled start time), this example will start them based on the variable defined
# For example, you created multiple tasks for Public Folder migration and wish to run 5 tasks in parallel

Do {
    # Get the current running task based on the tasktype
    $TotalRunnning = Get-OdmTask -All -Type $TaskType2Run | Where-Object { $_.Status -eq "In Progress" }

    # Get New tasks based on the tasktype and sort by creation date
    $QueuedTask = Get-OdmTask -All -Type $TaskType2Run | Where-Object { $_.Status -eq “New” } | Sort-Object Created

    # If no more New tasks, then exit
    If ($QueuedTask.Count -eq 0) { Break }

    # Display In Progress task info
    Clear-Host
    $TotalRunnning | Sort-Object Name | Select-Object Id, Name, Type, Progress, LastResult | Format-Table *

    # Start next task based on MaxRunningTasks variable
    If ($TotalRunnning.Count -lt $MaxRunningTasks) {
        Start-OdmTask $QueuedTask[0]
        Write-Host "Started task:"($QueuedTask[0].Name)
    }

    # Display info
    Write-Host ("Running tasks count: " + $TotalRunnning.Count)
    Write-Host ("Queued tasks count: " + $QueuedTask.Count)

    # Inform when last query ran
    Write-Host ("Last query ran at: " + (Get-Date))

    # Wait before getting the next task info
    Write-Host ("Sleeping for " + $SleepMinute + " Minutes")
    Start-Sleep ($SleepMinute * 60)

}While ($true)
##################################