functions/invoke-axstartautorun.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

<#
    .SYNOPSIS
        Execute the AutoRun feature of the AX 2012 Client
         
    .DESCRIPTION
        AX 2012 offers several ways to automate some core developer tasks, like DB Sync & Xpp Compile
         
    .PARAMETER Path
        Path to the autorun xml file that contains the desired automation tasks that you want to execute
         
    .PARAMETER AxClientExePath
        Path to the AX 2012 Client (ax32.exe) file, which is needed to run the automated tasks
         
        The default value is read from the registry on the local machine
         
    .PARAMETER TimeoutInMinutes
        Number of minutes the autorun task is allowed to run, before you want it to exit
         
        Default value is: 360 minutes (6 hours)
         
    .PARAMETER ShowOriginalProgress
        Instruct the cmdlet to show the standard output in the console
         
        Default is $false which will silence the standard output
         
    .PARAMETER OutputCommandOnly
        Instruct the cmdlet to only generate the needed command and not execute it
         
    .PARAMETER EnableException
        This parameters disables user-friendly warnings and enables the throwing of exceptions
        This is less user friendly, but allows catching exceptions in calling scripts
         
    .EXAMPLE
        PS C:\> Invoke-AxStartAutoRun -Path "c:\temp\ax2012.tools\autorun_syncDB.xml"
         
        This will invoke the autorun feature of the AX 2012 client.
        It will use the "c:\temp\ax2012.tools\autorun_syncDB.xml" as path passed to the AX 2012 Client.
        It will use the default path of the AX 2012 Client, read from the registry.
        It will run for a maximum of 360 minutes.
         
    .NOTES
        Tags:
         
        Author: Mötz Jensen (@Splaxi)
         
#>


function Invoke-AxStartAutoRun {
    [CmdletBinding()]
    [OutputType([System.String], ParameterSetName = "Generate")]
    param (
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('AutoRunXml')]
        [string] $Path,

        [string] $AxClientExePath = $(Join-Path $Script:ClientBin "Ax32.exe"),

        [Int32] $TimeoutInMinutes = 360,

        [switch] $ShowOriginalProgress,

        [Parameter(ParameterSetName = "Generate")]
        [switch] $OutputCommandOnly,

        [switch] $EnableException
    )

    $executable = $AxClientExePath
    $logPath = ""

    if (-not (Test-PathExists -Path $Path, $executable, $AxClientExePath -Type Leaf)) { return }

    if (Test-PSFFunctionInterrupt) { return }

    $autoRunNode = Select-Xml -Path $Path -XPath "//AxaptaAutoRun"
    
    if ([System.IO.Path]::HasExtension($($autoRunNode.Node.logFile))) {
        $logPath = $autoRunNode.Node.logFile
        $parentPath = Split-Path -Path $logPath -Parent

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

        if (Test-PSFFunctionInterrupt) { return }
    }

    $params = New-Object System.Collections.Generic.List[string]

    $params.Add("-development")
    $params.Add("-internal=noModalBoxes")
    $params.Add("-StartupCmd=autorun_`"$Path`"")

    Invoke-Process -Executable $executable -Params $($params.ToArray()) -TimeoutInMinutes $TimeoutInMinutes -ShowOriginalProgress:$ShowOriginalProgress -OutputCommandOnly:$OutputCommandOnly -EnableException:$EnableException

    if (-not ($OutputCommandOnly)) {
        # [string]$logString = Get-Content -Path $logPath -Raw

        # [xml]$outputContent = $logString.Replace("&", "&amp;")

        # $stringWriter = New-Object System.IO.StringWriter

        # $xmlSettings = New-object System.Xml.XmlWriterSettings
        # $xmlSettings.Encoding = [System.Text.Encoding]::UTF8
        # $xmlSettings.Indent = $true
        # $xmlSettings.NewLineHandling = [System.Xml.NewLineHandling]::Entitize
        
        # $xmlWriter = [System.Xml.XmlWriter]::Create($stringWriter, $xmlSettings)
        
        # # $xmlWriter.Indentation = 4
        
        # $outputContent.WriteTo($xmlWriter)
        # $xmlWriter.Flush()
        # $stringWriter.Flush()
        # $outputContent.LoadXml($stringWriter.ToString())
        # $outputContent.Save($logPath)

        [PSCustomObject]@{
            Path = $logPath
        }
    }
}