PowerShell scripting for Microsoft Dynamics CRM

PowerShell scripting for Microsoft Dynamics CRM

Introduction

Another PowerShell scripting tutorial  – but Dynamics CRM oriented

The main design of these series of blog posts is to share my experience regarding PowerShell scripting for Microsoft Dynamics CRM. I am pretty confident that PowerShell will be soon available as the major administrative interface of Microsoft products, as it is already available for Dynamics CRM deployment administration. This scripting language trends to become a key skill for IT professionals, as I observed in my current assignment.

More about the future of PowerShell: http://blogs.technet.com/b/heyscriptingguy/archive/2014/06/01/weekend-scripter-predicting-the-future-of-powershell.aspx

Moreover, Dynamics CRM professionals needs to deal with every technology implied in a Dynamics CRM deployment sooner or later. These Microsoft technologies include Windows Registry, Exchange, Active Directory and Internet Information Service. Nowadays, all of them can be managed through standard PowerShell features or dedicated snap-ins. All these usages are well documented on Microsoft MSDN.

More about PowerShell on MSDN: http://msdn.microsoft.com/en-us/library/ms714469%28v=vs.85%29.aspx

The following blog posts are being written mainly for Dynamics CRM developers. These posts aims to cover the following main subjects : setting-up the development environment, implementing overall logic, managing configuration, handling CRM exceptions, performing unit testing and improving log verbosity.

If one of these blog posts is of some value to you, please do not hesitate to comment or criticize any technical aspects, as I am still a learner regarding this scripting language and willing to improve constantly.

A scripting language designed to improve productivity

Windows PowerShell is an object-oriented scripting language. Therefore, it provides the best of two worlds: the scripting world and the Windows .NET Framework.  All the following benefits ensue from this definition.

The purpose of script languages is to write tasks quicker than using a program. In my opinion, this is the most attractive aspect of using PowerShell scripting. In order to take advantage of that characteristic, we choose to develop core, unitary actions in a .NET library. This library implements methods that calls methods exposed by the Dynamics CRM SDK. The only logic allowed should be: input validation, log management and error handling. This library have other benefits, such as the possibility to perform unit testing ; but this is of the topic for now. The point is that the business logic resides in the PowerShell scripts.

The fact that PowerShell scripting allows instantiation of objects, invocation of methods and accessing object’s properties is a huge benefit. A well spread example is the validation of an Uri. The following test is based on a static methods of the Uri object. No need to use complex regular expression here.

# Validates an Uri specified in the $Uri variable
 $isValidUri = [System.Uri]::IsWellFormedUriString($Uri,[System.UriKind]::RelativeOrAbsolute)

We are also losing the compilation process. This is a good point as it allow to gain some time in the delivery process. The script is executed against integration and validation environments before running in production in order to validate the grammar and the logic.

Benefits of Dynamics CRM integration using PowerShell

PowerShell scripting is a great way to make Dynamics CRM integration easier.

The IT department of a company gather a lot of different teams, each specializing on a specific technology or responsible for a dedicated perimeter. For example, the development team is responsible for producing the delivery package that the Operation team deploys in the Production environment. The only technology in common between these two teams may be Windows PowerShell, as a transverse scripting language.

Moreover, as explain upper in the introduction, Microsoft Dynamics CRM deals with multiple technologies. Using PowerShell allows the development team to use the same language and logic to perform operations against all the major Windows services and applications related to this product.

An other benefit of using PowerShell scripting is that the code snippets are always reusable, or at least they should be. The idea is to call methods which perform unitary operation. The data management operations are a good example, as each call should initiate a simple CRUD operation.

Finally, the syntax of PowerShell commands is easy to read and should be self explaining. The verb is followed by the targeted object, as in the following command. The scripts are easily maintained and understood with the help of a few best practices. These best practices are described in the next chapter.

# Gets help on the specific topic by passing the topic name to Get-Help.
 Get-Help Get-Member

People who inspired these blog posts

First, I would like to thank Fabien Dibot and Carlo Mancini for their great training course during the Microsoft  TechDays this year. This presentation was my first insight of the capabilities of the PowerShell language and a very inspired one. The session “Powershell, 45 tips & tricks pour votre utilisation quotidienne” is a great start for French developers.

The session is available on YouTube at this address: http://youtu.be/662BmxO559U

Here is a simple command explained during this session. This command aims to display the Windows events which occurred today in a grid, implementing sorting and search features. This is just awesome.

# Gets a list of Windows applicative logs for the current day, and format the output in a grid
 Get-EventLog -LogName application -After ((date)+-1d) | Out-GridView

Then, I would like to thank Aymeric Mouillé (Microsoft Consulting Services) who demonstrates interesting PowerShell usages for Dynamics CRM data management in his blog on MSDN. These examples highlight the efficiency of PowerShell syntax and why PowerShell is a great way to implement data management tasks for Dynamics CRM.

Aymeric’s post on CRM Data Management With PowerShell: http://blogs.msdn.com/b/aymerics_blog/archive/2014/06/28/crm-data-management-with-powershell.aspx

Finally, I would like to thank my current development team for letting me introduce some PowerShell scripting in their life. Our team now uses PowerShell in order to perform post delivery operations, data management actions and complete CRM environment refresh. PowerShell scripting has already proven its efficiency as we improve our code design and implementation every day.

Next time, I would like to review some requirements and best practices: http://blog.canaple.fr/?p=206

Leave a Reply

Your email address will not be published.