Dynamics CRM, PowerShell, and Async Operations

Have you used the Dynamics CRM PowerShell commandlets to automate any of your deployment tasks? 

If not, then you should.  There is good stuff in there.

In the CRM SDK sample code folder, you will find a folder called ps, which is where the PowerShell samples are located.

Many of the Deployment operations have an asynchronous component so that you can “fire and forget” your operation, should you wish.

However, there are built-in facilities for monitoring the asynchronous process that was created:

$opstatus = Get-CrmOperationStatus -OperationId $opid
while($opstatus.State -eq "Processing")
    {
        Write-Host [(Get-Date)] Processing…
        Start-Sleep -s 30
        $opstatus = Get-CrmOperationStatus -OperationId $opid
    }

The while loop will continue to execute until the status changes away from Processing.

BUT, and there is always a but, there is another status that you might encounter that needs to be addressed:

while($opstatus.State -eq "Processing" -or $opstatus.State -eq "Queued")

The Queued status will appear if the Deployment service has other processes that it is attempting to complete.  Modifying the while loop will allow your code to wait for either.

Leave a Reply 0 comments