functions/invoke-axexportmodelstore.ps1

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<#
.SYNOPSIS
Export an AX 2012 modelstore file
 
.DESCRIPTION
Export an AX 2012 modelstore file from the modelstore database
 
.PARAMETER DatabaseServer
Server name of the database server
 
Default value is: "localhost"
 
.PARAMETER ModelstoreDatabase
Name of the modelstore database
 
Default value is: "MicrosoftDynamicsAx_model"
 
Note: From AX 2012 R2 and upwards you need to provide the full name for the modelstore database. E.g. "AX2012R3_PROD_model"
 
.PARAMETER InstanceName
Name of the instance that you are working against
 
If not supplied the cmdlet will take the name of the database and use that
 
.PARAMETER Suffix
A suffix text value that you want to add to the name of the file while it is exported
 
The default value is: (Get-Date).ToString("yyyyMMdd")
 
This will always name you file with the current date
 
.PARAMETER Path
Path to the location where you want the file to be exported
 
Default value is: "c:\temp\ax2012.tools"
 
.PARAMETER GenerateScript
Switch to instruct the cmdlet to only generate the needed command and not execute it
 
.EXAMPLE
Invoke-AxExportModelstore
 
This will execute the cmdlet will all the default values.
This will work against the SQL server that is on localhost.
The database is expected to be "MicrosoftDynamicsAx_model".
The path where the exported modelstore file will be saved is: "c:\temp\ax2012.tools".
 
.NOTES
Author: Mötz Jensen (@Splaxi)
 
#>

Function Invoke-AxExportModelstore {    
    Param(
        [string] $DatabaseServer = "localhost",

        [string] $ModelstoreDatabase = "MicrosoftDynamicsAx_model",

        [string] $InstanceName, 

        [string] $Suffix = $((Get-Date).ToString("yyyyMMdd")),
        
        [string] $Path = "c:\temp\ax2012.tools",

        [switch] $GenerateScript
    )

    Invoke-TimeSignal -Start       

    if (-not (Test-PathExists -Path $Path -Type Container -Create)) { return }

    if (-not (Test-PathExists -Path $Script:AxPowerShellModule -Type Leaf)) { return }

    $null = Import-Module $Script:AxPowerShellModule
        
    $DateString = $((Get-Date).ToString("yyyyMMdd"))
        
    if ([System.String]::IsNullOrEmpty($InstanceName)) {
        $InstanceName = "{0}" -f $ModelstoreDatabase.Replace("_model", "")
    }

    $ExportPath = Join-Path $Path "$($InstanceName)_$($Suffix)"

    $params = @{
        DatabaseServer = $DatabaseServer
        ModelstoreDatabase = $ModelstoreDatabase
        File = $ExportPath
    }

    if($GenerateScript) 
    {
        $arguments = Convert-HashToArgString -InputObject $params

        "Export-AxModelStore $($arguments -join ' ')"
    }
    else {
        Write-PSFMessage -Level Verbose -Message "Starting the export of the model store"
        
        Export-AxModelStore @params
    }

    Invoke-TimeSignal -End
}