Tuesday, March 5, 2013

Compare models in two modelstores using Powershell

Imagine you have two modelstores, and inside these two modelstores there are several models installed.

Then imagine you want to compare those two modelstores and list out what models are installed in both or just one of them. You are also interested to see what models are installed in both but have a different number of elements or version number. Basically, you could compare any of the properties of a AX Model (Publisher, Layer, Signed, Description, etc). The only thing you would not be able to compare is the metadata of the elements within the model.

PowerShell to the rescue
Meet the Compare-Object command, or simply "compare". You know how you can get all the models in a modelstore, so lets get one collection from Modelstore A and compare it to a collection from Modelstore B.

Here is the PowerShell script:

import-module "C:\Program Files\Microsoft Dynamics AX\60\ManagementUtilities\Microsoft.Dynamics.ManagementUtilities.ps1"

compare `
 -ReferenceObject (Get-AXModel -Config AX2012_Test | select name, version, elementcount) `
 -DifferenceObject (Get-AXModel -Config AX2012_Dev | select name, version, elementcount) `
 -PassThru -Property name, version, elementcount | where {$_.SideIndicator -eq "=>"} | sort name

Now you can change the code to fit your needs. You can remove the where clause (and the pipe in front) and just get a complete list. Play with it and compare modelstores. Each query is quick and painless.

Hope this helps someone.