functions/get-d365module.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

<#
    .SYNOPSIS
        Get installed package / module from Dynamics 365 Finance & Operations environment
         
    .DESCRIPTION
        Get installed package / module from the machine running the AOS service for Dynamics 365 Finance & Operations
         
    .PARAMETER BinDir
        The path to the bin directory for the environment
         
        Default path is the same as the AOS service PackagesLocalDirectory\bin
         
        Default value is fetched from the current configuration on the machine
         
    .PARAMETER PackageDirectory
        Path to the directory containing the installed package / module
         
        Normally it is located under the AOSService directory in "PackagesLocalDirectory"
         
        Default value is fetched from the current configuration on the machine
         
    .PARAMETER Name
        Name of the package / module that you are looking for
         
        Accepts wildcards for searching. E.g. -Name "Application*Adaptor"
         
        Default value is "*" which will search for all packages / modules
         
    .PARAMETER Expand
        Adds the version of the package / module to the output
         
    .EXAMPLE
        PS C:\> Get-D365Module
         
        Shows the entire list of installed packages / modules located in the default location on the machine.
         
    .EXAMPLE
        PS C:\> Get-D365Module -Expand
         
        Shows the entire list of installed packages / modules located in the default location on the machine.
        Will include the file version for each package / module.
         
    .EXAMPLE
        PS C:\> Get-D365Module -Name "Application*Adaptor"
         
        Shows the list of installed packages / modules where the name fits the search "Application*Adaptor".
         
        A result set example:
        ApplicationFoundationFormAdaptor
        ApplicationPlatformFormAdaptor
        ApplicationSuiteFormAdaptor
        ApplicationWorkspacesFormAdaptor
         
    .EXAMPLE
        PS C:\> Get-D365Module -Name "Application*Adaptor" -Expand
         
        Shows the list of installed packages / modules where the name fits the search "Application*Adaptor".
        Will include the file version for each package / module.
         
    .EXAMPLE
        PS C:\> Get-D365Module -PackageDirectory "J:\AOSService\PackagesLocalDirectory"
         
        Shows the entire list of installed packages / modules located in "J:\AOSService\PackagesLocalDirectory" on the machine
         
    .NOTES
        Tags: PackagesLocalDirectory, Servicing, Model, Models, Package, Packages
         
        Author: Mötz Jensen (@Splaxi)
         
        The cmdlet supports piping and can be used in advanced scenarios. See more on github and the wiki pages.
         
#>

function Get-D365Module {
    [Alias("Get-D365Package")]
    [Alias("Get-D365Model")]
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(Mandatory = $false, ParameterSetName = 'Default', Position = 1 )]
        [string] $BinDir = "$Script:BinDir\bin",

        [Parameter(Mandatory = $false, ParameterSetName = 'Default', Position = 2 )]
        [string] $PackageDirectory = $Script:PackageDirectory,

        [Parameter(Mandatory = $false, ParameterSetName = 'Default', Position = 3 )]
        [string] $Name = "*",

        [Parameter(Mandatory = $false, ParameterSetName = 'Default', Position = 4 )]
        [switch] $Expand
    )

    [System.Collections.ArrayList] $Files2Process = New-Object -TypeName "System.Collections.ArrayList"
        
    $null = $Files2Process.Add((Join-Path $BinDir "Microsoft.Dynamics.AX.Metadata.Management.Delta.dll"))
    $null = $Files2Process.Add((Join-Path $BinDir "Microsoft.Dynamics.AX.Metadata.Management.Diff.dll"))
    $null = $Files2Process.Add((Join-Path $BinDir "Microsoft.Dynamics.AX.Metadata.Management.Merge.dll"))
    $null = $Files2Process.Add((Join-Path $BinDir "Microsoft.Dynamics.AX.Metadata.Management.Core.dll"))
    $null = $Files2Process.Add((Join-Path $BinDir "Microsoft.Dynamics.AX.Metadata.dll"))
    $null = $Files2Process.Add((Join-Path $BinDir "Microsoft.Dynamics.AX.Metadata.Core.dll"))
    $null = $Files2Process.Add((Join-Path $BinDir "Microsoft.Dynamics.AX.Metadata.Storage.dll"))
    $null = $Files2Process.Add((Join-Path $BinDir "Microsoft.Dynamics.ApplicationPlatform.XppServices.Instrumentation.dll"))

    Import-AssemblyFileIntoMemory -Path $($Files2Process.ToArray())

    if (Test-PSFFunctionInterrupt) { return }

    Write-PSFMessage -Level Verbose -Message "Intializing RuntimeProvider."

    $runtimeProviderConfiguration = New-Object Microsoft.Dynamics.AX.Metadata.Storage.Runtime.RuntimeProviderConfiguration -ArgumentList $Script:PackageDirectory
    $metadataProviderFactory = New-Object Microsoft.Dynamics.AX.Metadata.Storage.MetadataProviderFactory
    $metadataProvider = $metadataProviderFactory.CreateRuntimeProvider($runtimeProviderConfiguration)

    Write-PSFMessage -Level Verbose -Message "MetadataProvider initialized." -Target $metadataProvider

    $modules = $metadataProvider.ModelManifest.ListModules()

    Write-PSFMessage -Level Verbose -Message "Testing if the cmdlet is running on a OneBox or not." -Target $Script:IsOnebox
    
    if ($Script:IsOnebox) {
        Write-PSFMessage -Level Verbose -Message "Machine is onebox. Initializing DiskProvider too."

        $diskProviderConfiguration = New-Object Microsoft.Dynamics.AX.Metadata.Storage.DiskProvider.DiskProviderConfiguration
        $diskProviderConfiguration.AddMetadataPath($PackageDirectory)
        $metadataProviderFactory = New-Object Microsoft.Dynamics.AX.Metadata.Storage.MetadataProviderFactory
        $metadataProvider = $metadataProviderFactory.CreateDiskProvider($diskProviderConfiguration)

        Write-PSFMessage -Level Verbose -Message "MetadataProvider initialized." -Target $metadataProvider

        $diskModules = $metadataProvider.ModelManifest.ListModules()

        foreach ($module in $diskModules){
            if ($modules.Name -NotContains $module.Name)
            {
                $modules += $module
            }
        }
    }

    Write-PSFMessage -Level Verbose -Message "Looping through all modules."

    foreach ($obj in $($modules | Sort-Object Name)) {
        Write-PSFMessage -Level Verbose -Message "Filtering out all modules that doesn't match the model search." -Target $obj
        if ($obj.Name -NotLike $Name) {continue}

        if ($Expand -eq $true)
        {
            $modulepath = Join-Path (Join-Path $PackageDirectory $obj.Name) "bin"

            if (Test-Path -Path $modulepath -PathType Container)
            {
                $fileversion = Get-FileVersion -Path (Get-ChildItem $modulepath -Filter "Dynamics.AX.$($obj.Name).dll").FullName
                $version = $fileversion.FileVersion
                $versionUpdated = $fileversion.FileVersionUpdated
            }
            else
            {
                $version = ""
                $versionUpdated = ""
            }
            
            [PSCustomObject]@{
                Module          = $obj.Name
                References      = $obj.References
                Version         = $version
                VersionUpdated  = $versionUpdated
            }
        }
        else
        {

            [PSCustomObject]@{
                Module     = $obj.Name
                References = $obj.References
            }
        }
    }
}