Run Microsoft.Xrm.Data.PowerShell on Azure Automation


Hi all,

When we announced Microsoft.Xrm.Data.PowerShell in this article, we got some feedback that you want to run the module on Azure Automation. Yes we hear you and here comes the latest module which supports Azure Automation!

Please get the latest module from GitHub (Please download version 2.x)

What’s Azure Automation?

Before showing “how to” part, I will explain what is Azure Automation in case you do not know about it. You can consider Azure Automation as PaaS. It provides a place to run PowerShell scripts. You don’t have to worry about Operating Systems nor clustering for High Availability, nor Infrastructure. Azure Automation also provides various capabilities.

- You can upload your own modules.
- You can store variables such as Credential, String, etc.
- You can schedule the task.
- You can draft and run test session, before “publish” the script.
- Integration with services such as GitHub, your own services, etc.

Please find more detail about Azure Automation here.

Step by Step instructions

It’s time to play with it. Please follow the steps below to create your first script running on the cloud, which disables “Display welcome screen to users when they sign in” System Settings.

Create Azure Automation Account

1. Login to Azure Portal. If you do not have any Azure Subscription, please sign up for 30 days trial. http://portal.azure.com

2. Click Browse | Automation Accounts from the list and click “Add” in the list.

image

image

3. Check if correct Subscription is selected, then enter name. Then click “Create” button.

4. Go back to Automation Accounts list to confirm the account has been created.

Add Assets

Next step is to add Assets. Asset is a place where you store your own stuff, such as PowerShell modules and variables.

1. Open added Automation Account and click Assets.

image

2. Click Modules.

image

3. Click “Add a module” button.

4. Click folder icon to browse module.

5. Select Microsoft.Xrm.Data.PowerShell.zip which you download from GitHub, and click “OK”.

image

6. Next, add credential. Click Credentials.

image

7. Click “Add a credential” button.

8. Enter credential detail and click “Create”.

image

9. Lastly, add CRM Server URL. Click “Variables”.

10. Click “Add a variable” and enter CRM server URL as “string” variable. Click “Create”.

image

Create Runbook

Now you are ready to write first script! To do so, you need to create a runbook, which will contain your script.

1. Go back to your automation account blade, and click Runbooks.

image

2. Click “Add a runbook” button, and select “Create a new runbook”.

3. Enter a name as “SetNavigationTourOff” and select “PowerShell” from Runbook type, then click “Create”.

image

4. Once the runbook created, select the runbook and click Edit.

image

5. Firstly, you get credential and CRM Server URL from variables. Expand “ASSETS” | “Variables” and right click “CRM Server” variable. Then, select Add “Get Variables” to canvas menu, which inserts a script to get the variable.

image

6. Modify the script to store the result to $crmserver variable.

image

7. Expand “Credentials” and right click “CrmCred”, then select Add to canvas.

8. Change the script to store the result to $cred variable.

image

9. Next, you connect to your CRM organization. Expand “CMDLETS” | “Microsoft,Xrm.Data.PowerShell” and right click “Connect-CrmOnline”, then click “Add to canvas”. Modify the script to store the connection to $conn variable, and pass parameters you obtained above.

image

10. Lastly, put the following script to the canvas either by typing or from left menu. The entire script looks like this.

image

11. Click “Save” to save the change.

Test the runbook

Once you crafted the script, you need to test it.

1. Click “Test Pane” button.

image

2. Click “Start” to run the test, and wait until it’s completed.

image

image

3. Once completed, login to your CRM to see if “Display welcome screen to users when they sign in” System Settings has been disabled.

image

Publish the runbook

Once you confirmed it works as expected, you publish the script.

1. Click “Publish” button. Click “Yes” for confirmation dialog.

image

2. You can also set scheduling. Click “Schedule” button.

image

3. Create your schedule as you need.

Conclusion

Microsoft.Xrm.Data.PowerShell is compatible with cloud now, and you can bring everything to the cloud. You may still need VM or local computer if you need to keep running the script less than every hour though, as minimum scheduling period is an hour for Azure Automation as of now.

Ken
Premier Mission Critical/Premier Field Engineer
Microsoft Japan

Comments (3)

  1. Dragan says:

    Hi Kenichiro,

    Thanks for the module and this great article. It works great with the CRM online. I’m struggling to connect to the CRM on-premises in my Azure Automation runbook.

    Get-CrmConnection : The type initializer for ‘System.ComponentModel.DesignerProperties’ threw an exception.
    At line:49 char:13
    + $crmConn = Get-CrmConnection -Credential $crmAutomationCredential -O …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : SyntaxError: (:) [Get-CrmConnection], TypeInitializationException
    + FullyQualifiedErrorId : -9,Microsoft.Xrm.Tooling.CrmConnector.Powershell.Commands.GetCrmConnectionCommand

    Do you have any tip (or trick or tweak or whatever you’d like to call it) that can help?

    1. Though it says Connect-CrmOnline, it actually connects to OnPrem as well (sorry for naming). Try changing the connection string to test?

  2. J5 says:

    None of the images are loading for Chrome or IE

Skip to main content