Skip to main content

Notifications

Dynamics 365 Community / Blogs / Goshoom. NET Dev Blog / AX management shell – multi...

AX management shell – multiple models

Martin Dráb Profile Picture Martin Dráb 230,842 Most Valuable Professional

Even if you’re not familiar with PowerShell, you probably don’t have much trouble with using AX management cmdlets. You just call the right command, set some parameters and it’s done.

# Just for example
Export-AXModel -Model "MyModel" -File "c:\my.axmodel"

If you don’t know the parameters, you’ll simply use Get-Help or some of its aliases, such as man:

man Export-AXModel -Detailed

But if you don’t know PowerShell well, you may not know how, say, to work with multiple models at once. This is often useful, therefore it’s worth showing how to do it.

Before we look at any code, ensure yourself that you have a development environment such as PowerShell ISE or PowerGUI. These applications will help you saving your scripts, discovering commands and parameters, setting breakpoints, reviewing variables and many other things. No need to do everything directly in console!

But there is a problem – if you try to use AX cmdlets outside AX management shell, they won’t be recognized. Fortunately the fix is easy – add the following line to your script (including the dot at the beginning):

. 'C:\Program Files\Microsoft Dynamics AX\60\ManagementUtilities\Microsoft.Dynamics.ManagementUtilities.ps1'

You can also run it directly in console or even to add it to your profile, so it’s loaded automatically every time you run PowerShell.

Let’s warm up with a few simple commands (using PowerShell 3.0 syntax):

# Lists all AX modules - nothing new here
Get-AXModel
 
# Lists just model names and layers, to make things more readable
Get-AXModel | select Name, Layer
 
# Lists models in a specific layer
Get-AXModel | ? Layer -eq USR

The “| ” symbol is a pipe and it passes the output of one command to the input of another command. It would be great if could use it with AX cmdlets. e.g. for passing a list of models to Export-AXModel. Unfortunately it’s not supported and AX cmdlets also don’t accept arrays as arguments, therefore we have to call each command for a single model at a time. It’s not that bad as it might sound, because we can use a loop to sequentially execute a command for every element in an array.

# Let's set some parameters first 
$toDir = 'C:\Models'
$layer = 'USR'
 
# Export all models in a given layer to file
Get-AXModel | ? Layer -eq $layer | % { Export-AXModel -Model $_.Name -File "$toDir\$($_.Name).axmodel" }
 
# Uninstall all modules from a given layer
Get-AXModel | ? Layer -eq $layer | % { Uninstall-AXModel -Model $_.Name -NoPrompt }

It might look a little bit cryptic at first, but you see it’s rather simple and you shouldn’t have problems to apply the same approach in your own scripts. It’s much faster to write (or copy) something like this than typing, running and waiting for several individual commands.


This was originally posted here.

Comments

*This post is locked for comments