On Demand Migration – PowerShell API – Example Scripts

Quest recently announced that the On Demand Migration (ODM) PowerShell API module is now available in the PowerShell gallery. To help get you started, we have compiled a few example scripts that demonstrate how to perform common migration activities using the ODM API.

The examples below assume you have already connected to ODM and selected your Organization, Project, and ProjectWorkload, as described in the ODM API New Feature Spotlight.  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.

Discover Accounts - Create task, start task, monitor task status

This example demonstrates how to create a task to discover all accounts, start the task and monitor the status of the task until it is no longer In Progress.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Discover objects in Accounts Project #
#########################################
$SleepMinute = 1 # Defines the wait time in minutes between querying the task status
$TaskName = "Discover All Objects"
# Defines the tasks settings
$ACCDiscoverParam = @{Name = $Taskname
DiscoverUsingCSV = $false
FilePath = $null
DiscoverUsingGroups = $false
DiscoveryGroupsFilePath = $null
CreateApplicationAccessPolicy = $false
NotifyOnTaskCompletion = $true
NotifyOnlyOnTaskFailure = $false
NotificationRecipientEmail = "example@domain.com"
ScheduledStartTime = $null
}
# Create the task
$Task = New-OdmDiscoveryTask @ACCDiscoverParam
# Run the task
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Migrate Accounts - Create multiple tasks with a defined number of objects

This example demonstrates how to create multiple tasks with the defined number of objects. For example, if you need to migrate 2000 accounts, you can split the migration into 10 tasks with 200 accounts each. Note: This example creates the tasks, but does not run them. 

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Create task(s) for migrating user accounts #
#############################################
# The below script will create tasks based on the setting in NumberOfObjects variable.
# Using Type to filter for User objects only.
$TaskName = "Migrate User Accounts"
$NumberOfObjects = 50 # Defines the number of accounts added to a single task
# Download every object from project
$AllObjects = Get-OdmObject -All
Write-Host "All Objects count:"$AllObjects.Count
# Filter for objects where the Type is User
$Objects = $AllObjects | Where-Object { $_.Type -eq "User" }
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)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Migrate Mailboxes - Create task from collection, schedule task start time

This example script demonstrates how to create a task from a collection and schedule the task to start after a defined number of minutes.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Create task for Migrating mailbox(es) from a Collection #
#######################################################
# Create and start a mailbox migration task based on Collection members and schedule the start
$CollectionName = "Example" # Defines the name of Collection
$TaskName = "MBX Migration of " + $CollectionName + " Collection"
$ScheduleFromNowMinutes = 60 # Defines the task start delay from current time
# Get collection
$Coll = Get-OdmCollection -WildcardFilter @{name = $CollectionName }
# Get the mailboxes from collection
$Objects = Get-OdmCollection -WildcardFilter @{name = $CollectionName } | Get-OdmObject -All
Write-Host "Object(s) in Collection:"$Objects.Count
# Options to migrate
$MBXMigParam = @{ Name = $TaskName
MigrateFromArchive = $false
MigrateToArchive = $false
MigrateMail = $true
MigrateCalendar = $true
MigrateContacts = $true
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Migrate OneDrive - Create task, filter by size, schedule task start time

This example script demonstrates how to create a migration task for OneDrives sized between 1 GB and 10 GB that have not previously been migrated and schedule the task to start at a specific date and time.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# OneDrive Migration #
#######################
# This example creates OneDrive migration task for OneDrives sized between 1 GB and 10 GB which have not been previously migrated
# Start the task at specific scheduled time "01/23/2024 1:30 PM"
$TaskName = "OD Migration"
$MinSizeGB = 1
$MaxSizeGB = 10
$ScheduleTime = "01/23/2024 1:30 PM" # "MM/DD/YYYY HH:MM" - 24 hour, "MM/DD/YYYY HH:MM AM/PM" - 12 hour
# Get every item
$AllObjects = Get-OdmObject -All
Write-Host "All Objects count:"$AllObjects.Count
# Filter for New OneDrives only
$NewObjects = $AllObjects | Where-Object { $_.OneDriveMigrationState -eq "Discovered" } # Discovered the status for New OneDrives
Write-Host "New OneDrives count:"$NewObjects.Count
# Filter minimum size
$MinObjects = $NewObjects | Where-Object { $_.OneDriveSourceTotalSize -gt ($MinSizeGB * 1024 * 1024 * 1024) }
Write-Host ("OneDrives above " + $MinSizeGB + " GB count: " + $MinObjects.Count)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Monitor Tasks - Return status and details for In Progress tasks

This example script demonstrates how to monitor running tasks and return detailed info, utilizing filters such as Wildcardfilter, tasktype filters, and Get-OdmEvent filters.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Monitor In Progress tasks #
#############################
# Define the task type to monitor or set to ALL to monitor every In Progress task
$TaskType = "All" # use ALL to monitor every running task, Types for example, "Mail Migration", "OneDrive Migration"
$SleepMinute = 5 # Wait time in minutes before getting task info as it’s looped
Do {
# Check the tasks to monitor
If ($TaskType -eq "All") {
$MonTasks = Get-OdmTask -All | Where-Object { $_.Status -eq "In Progress" }
}
else {
$MonTasks = Get-OdmTask -All -Type $TaskType | Where-Object { $_.Status -eq "In Progress" }
}
# Check if any running tasks; if not, exit
If ($MonTasks.Count -lt 1) {
Write-Host "No In Progress tasks to monitor"
Break
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Auto Start Tasks - Auto-start tasks, limit number of concurrent parallel tasks

This example script demonstrates how to automatically start tasks based on how many parallel tasks you want to run for a specific task type.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Automatically Start New tasks #
#################################
# When New tasks are created (without scheduled start time), this example will start them based on the variables defined
# For example, you created multiple tasks for mailbox migration and wish to run 10 tasks in parallel
$TaskType2Run = "Mail Migration" # Types for example, "Mail Migration", "OneDrive Migration"
$MaxRunningTasks = 10
$SleepMinute = 5 # Wait time before getting task info as it's looped
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 -eqNew” } | Sort-Object Created
# If no more New tasks, then exit
If ($QueuedTask.Count -eq 0) { Break }
# Display In Progress task info
Clear-Host
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Further Information

For more information on this and many other features within On Demand Migration, check out the ODM User Guide Appendix A: Using PowerShell. We welcome your feedback and suggestions below and invite you to visit us at Quest.com.

  • Hi, when trying to create a OneDrive job with  New-OdmOneDriveMigrationTask  I get an error. I have a custom RBAC role defined. This all works fine for similar Mailbox task creation

    The task did not created. HTTP Error code: Forbidden
    Description: {"message":"Access is denied. The 'write' permission is required to access the 'tasks' entity.","data":null}
    At C:\Users\neil.adams4\OneDrive - National Grid\Documents\WindowsPowerShell\Modules\OdmApi\2.0.123\OdmAPI.WriteFunctions.ps1:114 char:12
    + catch {throw $($($MessagesDictionary['EntityNotCreated'] -f $Enti ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (The task did no....","data":null}:String) [], RuntimeException
    + FullyQualifiedErrorId : The task did not created. HTTP Error code: Forbidden
    Description: {"message":"Access is denied. The 'write' permission is required to access the 'tasks' entity.","data":null}

    • Hello Neil,

      What is the customer RBAC settings? Let me try to reproduce the issue.

      • Thanks Jozsef!

        Interestingly, when I am added to the Migration Administrator role, the command runs fine. However, only other Migration Administrators can see the resultant task, or view the generated events. The taks is not shown for other people and even sharing a link to the events generated by the task gives an Access Denied message

        RBAC permissions

        On Demand Organization:

        • Can Export Data (Migration Only)

        Migration:

        • View Projects and Manage Selected Services: All Selected
        • View Projects: All Selected
        • Run A full discovery: Accounts, Teams, SharePoint, Public Folders
        • Run a scoped Discovery with CSV file: Accounts, Teams, SharePoint
        • Run Content discovery tasks: Accounts, Mailboxes, OneDrive, SharePoint
        • Run match and map tasks: Accounts, Teams, Sharepoint
        • Run Provision and migration tasks: all
        • Manage Collections: all
        • Update and delete migration objects: all
        • Ack and clear Task events: all
        • Manage DUA: Yes
        • View Migration Reports: All
        • View Reporting dashboard: All

    • Thanks for these great examples! Are there any plans to add the ability to use this OdmApi module with Quest ODM for Active Directory (or any way to use the BinaryTree module still)?

    • Hello, we are trying to run New-OdmAddressRewritingTask but we're not able to do it because we always get:

      Incorrect workload type.
      At C:\Program Files\WindowsPowerShell\Modules\OdmApi\2.1.30\OdmAPI.CommonFunctions.ps1:172 char:17
      +                 throw $MessagesDictionary['IncorrectWorkLoadType']
      +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : OperationStopped: (Incorrect workload type.:String) [], RuntimeException
          + FullyQualifiedErrorId : Incorrect workload type.

      When we run:
      $task = New-OdmAddressRewritingTask -Name "Address Rewriting Task (From PowerShell)" -Action Deploy


      It doesn't matter what Action we set, always same error, can you please help?

      • Hello Asola,

        This is a feature of Domain Coexistence, do you have license for it? ODMAD Domain Rewrite has no yet Powershell support.

        If it is Domain Coexistence:

        How did you select Account project workload?

        The format is like:

        Get-OdmProject -ProjectId xxxxxxxx| Get-OdmProjectWorkload -Type Accounts | Select-OdmProjectWorkload