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.

3 comments:

  1. Now add this into your backup PowerShell and you can get a snapshot of what models changed over x period. My dream (if only I had more time) is to get a fingerprint mechanism into the models, hash key the data in the models to have a more reliable difference indicator.
    Like your blog.

    ReplyDelete
    Replies
    1. Thanks, Lurker! That is a good idea. I haven't checked, but I wonder if the RecVersion in SysModelManifest-table gets touched when a model is changed or exported. :-)

      Delete
  2. Negative on recVersion (there is no such field in db).
    There is a Dirty and State field, but these seem to be only used for Microsoft layers (isDirty) and models (SYS to USP, none of mine change it)

    Superficial checking, didn't trace with test cases. Just took a look at applications that I know are in different states.

    ReplyDelete