Thumbnail image

PowerShell for Automated Administration of Dynamics AX 2012 (PS-II)

!
Warning: This post is over 365 days old. The information may be out of date.

Table of contents

In the first entry of this PowerShell and DevOps series for Microsoft Dynamics AX 2012 we discussed the most basic PowerShell principles and syntax. We also saw in previous post entries how PowerShell can be used in very creative ways.

This time we are going to review cmdlets specifically for Microsoft Dynamics AX 2012 administration, which we can split into several groups:

Model and Model Store Management

These cmdlets are very important and will be the foundation of our DevOps strategy, combined with the automated build processes configured in TFS, if you use them.

The combination of these components allows us to design processes that automatically update our environments, pushing our operations toward continuous integration or continuous deployment, especially in development and test environments. We will cover this in the next posts in this series.

These are the ones we normally use for these tasks, although it is worth checking the full reference because all of them can be useful for specific tasks or broader automation scripts:

Models

CommandDescription
New-AXModelCreates a new model in the Model Store
Uninstall-AXModelDeletes a model from the Model Store
Install-AXModelImports a model (from a file) into the Model Store
Export-AXModelExports a model from the Model Store to an .axmodel file
Move-AXModelMoves objects from one model to another, combining all objects in the target model.

Model Store

CommandDescription
Import-AXModelStoreImports a Model Store (from a file)
Export-AXModelStoreExports a Model Store to an .axmodelstore file
Initialize-AXModelStoreInitializes a model store to be used. It is recommended to run this command after certain maintenance operations.
Optimize-AXModelStoreOptimizes and reindexes the model store to improve performance. It is recommended to run this command after certain maintenance operations.
Set-AXModelStoreAssigns flags in the Model Store that indicate operations performed on it. These flags affect, for example, whether the model upgrade cockpit is shown at client startup: -InstallMode (show cockpit) or -NoInstallMode (do not show cockpit)

Report and Reporting Services Management and Deployment

Report management is another task we can automate in our builds or deployment scripts. Here are some of the most commonly used commands, but there are many more; check the full reference:

CommandDescription
Publish-AXReportPublishes a report from AOS to the SSRS Report Server. It is recommended to review this command documentation because it includes very useful parameters for optimized report deployment.
Test-AXReportServerConfigurationValidates configuration and connectivity between AOS and Report Server. This command is useful when you have deployment/printing issues, to detect configuration problems or failing components.
Get-AXReportServerConfigurationShows current configuration, including settings visible in the corresponding form in Dynamics AX 2012 and additional parameters.
Get-AXReportShows report details such as creation/modification date, modified-by user, design list, related Visual Studio AOT project, etc. This command is very useful in deployment scripts, and often directly in console sessions when troubleshooting report issues.

User Management and Security Artifacts

Another area where PowerShell is very useful both in console and in automation scripts is user management and, in AX, security role management as well.

As we will see, PowerShell is not limited to AX. It includes all Windows capabilities, so these commands can be combined with Active Directory cmdlets to automatically create, maintain, and monitor AX users based on domain changes.

Users

CommandDescription
New-AXUserCreates a new User
Disable-AXUserDisables a User
Get-AXUserReturns a list of all Users. This command also returns useful data such as each user’s Active Directory SID, making it an ideal integration point between Dynamics AX security and domain users.

Roles

CommandDescription
New-AXSecurityRoleCreates a security Role
Remove-AXSecurityRoleDeletes a security Role
Add-AXSecurityRoleMemberAdds a User to a Role
Get-AXSecurityRoleReturns the list of Roles assigned to a User
Get-AXSecurityRoleMemberReturns the list of Users assigned to a Role
Get-AXSecurityRoleInfoReturns information about all Roles in the system

I must insist on reviewing the full reference. There is no point in reproducing the complete command list here, so I only included the most relevant ones, but there are many more and they are worth knowing:

AX Is Not the Whole Story

So far I have focused only on cmdlets available in Dynamics AX 2012 administration tools. But we should not stop there. For advanced automation tasks we need to integrate with the rest of the system and use not only generic PowerShell commands but also cmdlets from products such as Analysis Services, Sharepoint, Active Directory, SQL Server, Azure services, … any Microsoft product includes a PowerShell module to interact with its components.

For example, some time ago I showed how to retrieve user information from Active Directory using X++; the same can be done from PowerShell and quickly reused by admins without opening AX. I also showed how to use PowerShell to assign our active user to an AX database restored from another domain to regain admin access, as an example using the SQL Server module. And we could keep going…

Examples of modules we may use:

Using these modules is fairly intuitive. After importing a module with Import-Module, we can use the cmdlets introduced in our PowerShell introduction, such as Get-Command and Get-Help, to discover available commands and usage.

PowerShell - Import-Module

Reuse Is the Key

As with any console or scripting language, we should avoid reinventing the wheel by writing scripts that someone else has likely already built. Searching online, you can find scripts for nearly any administration task using PowerShell.

Also, there are several initiatives publishing scripts specific to Microsoft Dynamics AX 2012. The most remarkable, in my opinion, is the script package published by Martin Dráb on CodePlex, which includes actions for almost every stage of automated deployment. You can practically build a complete build + environment update system using PowerShell only.

This package may or may not fit your needs, but in any case it is a great codebase to learn from and a solid starting point for your own scripts, including good AX-specific patterns to connect to AX servers. You can download it here:

Extra

As a final note, I want to mention an article that is worth checking more for its possibilities than for immediate practical use. Remember that PowerShell can use .NET assemblies, including the AX .NET Business Connector. That article shows an example of this usage, whether it is practical in your case or not ;)

And this is the end of part two. After the initial introduction, this is the starting point to build our own DevOps scripts for AX 2012!

Posts in this series

Related Posts