How to Call an ESM Plan From a PowerShell Script

Modified on Wed, 28 Aug at 5:12 PM

Introduction

In ManagementStudio PowerShell scripts are useful as they can be run on triggers such as a User Migration having a Migration Slot set. Natively, ESM plans cannot be run on triggers.


A PowerShell script can run an ESM plan with a single line of code, which enables ESM plans to be run on the script trigger. 


Prerequisites

  • Ensure an ESM Plan has been create
  • Make a note of either its name or its ID 

Script Setup

  • Administration -> [Module] -> PowerShell Scripts (select the Module where the ESM Plan has been created)
  • Click Here to Add New Item to create a new PowerShell script 
  • Set the Trigger and Sub Trigger as required:


  • Click Edit and add this text to the script editor:
Invoke-MSESMPlan -Module UserMigrations -Ids $ScriptArgs.Items -PlanName 'My ESM Plan name' -ResetPriorToRun -WaitForComplete 

or 

Invoke-MSESMPlan -Module UserMigrations -Ids $ScriptArgs.Items -PlanId XX -ResetPriorToRun -WaitForComplete 
  • Switches:

-Module should be set to the module where the ESM was created

-PlanName should be set to the name of the ESM plan to be called, enclosed in single quotes

-PlanId should be set to the ID of the ESM plan to be called (no quotes)

-ResetPriorToRun should only be used where the ESM plan needs to be Reset Prior to Run


  • Click Save Changes.


Note that the Data Source section at the top of the ESM plan will be ignored when the ESM is called by the script.



Test

  • Make a change to an asset (e.g. User Migration) which to trigger the script to run. In the example above, the User Migration Deployment Unit Slot should be changed
  • Open the updated asset, change to the Service Plans tab, select the ESM Plan and validate that the plan was run