functions/new-d365careport.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

<#
    .SYNOPSIS
        Generate the Customization's Analysis Report (CAR)
         
    .DESCRIPTION
        A cmdlet that wraps some of the cumbersome work into a streamlined process
         
    .PARAMETER OutputPath
        Path where you want the CAR file (xlsx-file) saved to
         
        Default value is: "c:\temp\d365fo.tools\CAReport.xlsx"
         
    .PARAMETER BinDir
        The path to the bin directory for the environment
         
        Default path is the same as the AOS service PackagesLocalDirectory\bin
         
    .PARAMETER MetaDataDir
        The path to the meta data directory for the environment
         
        Default path is the same as the aos service PackagesLocalDirectory
         
    .PARAMETER Module
        Name of the Module to analyse
         
    .PARAMETER Model
        Name of the Model to analyse
         
    .PARAMETER XmlLog
        Path where you want to store the Xml log output generated from the best practice analyser
         
    .PARAMETER PackagesRoot
        Instructs the cmdlet to use binary metadata
         
    .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 output the command that you would have to execute by hand
         
        Will include full path to the executable and the needed parameters based on your selection
         
    .PARAMETER SuffixWithModule
        Instruct the cmdlet to append the module name as a suffix to the desired output file name
         
    .EXAMPLE
        PS C:\> New-D365CAReport -module "ApplicationSuite" -model "MyOverLayerModel"
         
        This will generate a CAR report against MyOverLayerModel in the ApplicationSuite Module.
        It will use the default value for the OutputPath parameter, which is "c:\temp\d365fo.tools\CAReport.xlsx".
         
    .EXAMPLE
        PS C:\> New-D365CAReport -OutputPath "c:\temp\CAReport.xlsx" -module "ApplicationSuite" -model "MyOverLayerModel"
         
        This will generate a CAR report against MyOverLayerModel in the ApplicationSuite Module.
        It will use the "c:\temp\CAReport.xlsx" value for the OutputPath parameter.
         
    .EXAMPLE
        PS C:\> New-D365CAReport -module "ApplicationSuite" -model "MyOverLayerModel" -SuffixWithModule
         
        This will generate a CAR report against MyOverLayerModel in the ApplicationSuite Module.
        It will use the default value for the OutputPath parameter, which is "c:\temp\d365fo.tools\CAReport.xlsx".
        It will append the module name to the desired output file, which will then be "c:\temp\d365fo.tools\CAReport-ApplicationSuite.xlsx".
         
    .EXAMPLE
        PS C:\> New-D365CAReport -OutputPath "c:\temp\CAReport.xlsx" -module "ApplicationSuite" -model "MyOverLayerModel" -PackagesRoot
         
        This will generate a CAR report against MyOverLayerModel in the ApplicationSuite Module.
        It will use the binary metadata to look for the module and model.
        It will use the "c:\temp\CAReport.xlsx" value for the OutputPath parameter.
         
    .NOTES
        Author: Tommy Skaue (@Skaue)
         
        Author: Mötz Jensen (@Splaxi)
         
#>

function New-D365CAReport {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
    [CmdletBinding()]
    param (
        [Alias('File')]
        [Alias('Path')]
        [string] $OutputPath = (Join-Path $Script:DefaultTempPath "CAReport.xlsx"),

        [Parameter(Mandatory = $true)]
        [Alias('Package')]
        [Alias("ModuleName")]
        [string] $Module,
        
        [Parameter(Mandatory = $true)]
        [string] $Model,

        [switch] $SuffixWithModule,

        [string] $BinDir = "$Script:PackageDirectory\bin",

        [string] $MetaDataDir = "$Script:MetaDataDir",

        [string] $XmlLog = (Join-Path $Script:DefaultTempPath "BPCheckLogcd.xml"),

        [switch] $PackagesRoot,

        [switch] $ShowOriginalProgress,

        [switch] $OutputCommandOnly
    )
    
    if (-not (Test-PathExists -Path $MetaDataDir, $BinDir -Type Container)) { return }

    $executable = Join-Path $BinDir "xppbp.exe"
    if (-not (Test-PathExists -Path $executable -Type Leaf)) { return }

    if ($SuffixWithModule) {
        $OutputPath = $OutputPath.Replace(".xlsx", "-$Module.xlsx")
    }

    $params = @(
        "-metadata=`"$MetaDataDir`"",
        "-all",
        "-module=`"$Module`"",
        "-model=`"$Model`"",
        "-xmlLog=`"$XmlLog`"",
        "-car=`"$OutputPath`""
    )

    if ($PackagesRoot -eq $true) {
        $params += "-packagesroot=`"$MetaDataDir`""
    }

    Write-PSFMessage -Level Verbose -Message "Starting the $executable with the parameter options." -Target $param

    Invoke-Process -Executable $executable -Params $params -ShowOriginalProgress:$ShowOriginalProgress -OutputCommandOnly:$OutputCommandOnly

    if (Test-PSFFunctionInterrupt) { return }

    [PSCustomObject]@{
        File     = $OutputPath
        Filename = (Split-Path $OutputPath -Leaf)
    }
}