I have been working on a new version of my PowerShell script I use to export models to disk. I wanted to increment the build number each time a model was exported, so I started with my export script and added a single method and hooking things up.
The PowerShell method looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
| Function Update -Version ([string] $model , [string] $server , [string] $database , [int] $incrementBuildBy ) { $manifest = Get -AXModelManifest -Server $server -Database $database -Model $model $manifestVersion = $manifest .Version $manifestVersionSplit = $manifestVersion .Split( '.' ) $manifestVersionSplit [3] = $incrementBuildBy + $manifestVersionSplit [3] $ofs = "." $versionStr = [string] $manifestVersionSplit $manifestVersion = "Version=" + $versionStr Edit -AXModelManifest -Server $server -Database $database -Model $model -ManifestProperty $manifestVersion return $manifest } |
Given this method I changed my export to look more like this:
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
| $backupFilePath = New -BackupFolder ( $axBackupFolder ) $models = Get -AXModel -Server $server -Database $database foreach ( $model in $models ) { $backupFileName = "" $elementCount = "" $nameToBackup = $model .Name.ToString() $layer = $model .Layer.ToString() $elementCount = $model .ElementCount.ToString() $versionStr = $model .Version.ToString() $backupFileName = $backupFilePath + "\" + $nameToBackup + " .axmodel" if ( $layer .ToUpper().Contains( $layerFilter .ToUpper()) -and $nameToBackup .ToUpper().Contains( $modelFilter .ToUpper())) { Update -Version $nameToBackup $server $database $incrementBuildBy $backupFileName = $backupFilePath + "\" + $nameToBackup + " _ " + $versionStr + " .axmodel" "Exporting " + $elementCount + " elements from " + $nameToBackup + "..." File -Backup $nameToBackup $backupFileName $server $database } else { "Skipping " + $backupFileName + " in layer " + $layer } } "Completed!" |
The two other utility methods are simply like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
| Function New -BackUpFolder ([string] $destinationFolder ) { $dte = get-date $dte = $dte .tostring() -replace "[:\s/]" , "." $backUpPath = "$destinationFolder" + $dte $null = New-Item -path $backUpPath -itemType directory return $backUpPath } Function File -Backup ([string] $model , [string] $fileName , [string] $server , [string] $database ) { Export -AXModel -Model $model -File $fileName -Server $server -Database $database } |
So in order to wrap things up, all you need is to declare at the top a few variables:
1
2
3
4
5
6
| $server = "mydbserver" $database = "MicrosoftDynamicsAx" $axBackupFolder = "c:\axModelBackup\" $modelFilter = "AwesomeApp" $layerFilter = "ISV" $incrementBuildBy = 1 |
No comments:
Post a Comment