One of the repetitive tasks when working with Dynamics AX 2012 is getting out the models out of an environment. Lucky for us this is pretty easy to do using PowerShell. Inspired by the great blog post by Gary Holsopple where he shares a script he made to get the models out, I made this script covering my needs. In my version I iterate over the layers instead, and I prefer to display some feedback while doing so. The export will work each time, since the model-files will get unique names each time. Here's my version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
| import -module "C:\Program Files\Microsoft Dynamics AX\60\ManagementUtilities\Microsoft.Dynamics.ManagementUtilities.ps1" #region Variables $server = "myserver" $database = "AX2012_Dev" $axBackupFolder = "c:\axModelBackup\AX2012_Dev\" [System.Collections.ArrayList] $layerList = New-Object System.Collections.ArrayList $layerList = @( "USR" ; "USP" ; "CUS" ; "CUP" ; "VAR" ; "VAP" ;) [System.Xml.XmlDocument] $xml [System.Xml.XmlNode] $obj [System.Xml.XmlNode] $property #endregion #region Functions Function New -BackUpFolder ( $destinationFolder ) { $dte = get-date $dte = $dte .tostring() -replace "[:\s/]" , "." $backUpPath = "$destinationFolder" + $dte $null = New-Item -path $backUpPath -itemType directory return $backUpPath } Function fBackup([string] $model , [string] $fileName , [string] $server , [string] $database ) { Export -AXModel -Model $model -File $fileName -Server $server -Database $database } #endregion #region do the work $backupFilePath = New -BackupFolder ( $axBackupFolder ) $xml = (Get -AXModel -Server $server -Database $database | convertto -xml ) $nodes = $xml .SelectNodes( "Objects/Object" ) foreach ( $obj in $nodes ) { $backupFileName = "" $elementCount = "" # Loop all properties foreach ( $property in $obj .SelectNodes( "Property" )) { if ( $property .GetAttribute( "Name" ).Equals( "Name" )) { $nameToBackup = $property .InnerText $backupFileName = $backupFilePath + "\" + $nameToBackup + " .axmodel" } if ( $property .GetAttribute( "Name" ).Equals( "Layer" )) { $layer = $property .InnerText } if ( $property .GetAttribute( "Name" ).Equals( "ElementCount" )) { $elementCount = $property .InnerText } } if ( $layerList .contains( $layer .ToUpper()) -and $backupFileName -ne "" ) { "Exporting " + $elementCount + " elements from " + $nameToBackup + "..." fBackup $nameToBackup $backupFileName $server $database } else { "Skipping " + $backupFileName + " in layer " + $layer } } "Completed!" #endregion |
1
2
3
4
5
6
7
8
9
10
11
12
13
| import -module "C:\Program Files\Microsoft Dynamics AX\60\ManagementUtilities\Microsoft.Dynamics.ManagementUtilities.ps1" $server = "myserver" # Format: "server\instance" $db = "ax2012_dev" $folder = "c:\axModelBackup\" # Remember trailing slash (\) #region Do Work $dte = get-date $dte = $dte .tostring() -replace "[:\s/]" , "." $file = $folder + $db + "_" + $dte Export -AXModelStore -Server $server -Database $db -File $file #endregion |
No comments:
Post a Comment