DSCResources/ROACommon/Src/Resolve-ROAPackagePath.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
function Resolve-ROAPackagePath {
<#
    .SYNOPSIS
        Resolves the latest RES ONE Automation/Automation Manager installation package.
#>

    [CmdletBinding()]
    param (
        ## The literal file path or root search path
        [Parameter(Mandatory)]
        [System.String] $Path,

        ## Required RES ONE Automation/Automation Manager component
        [Parameter(Mandatory)]
        [ValidateSet('Console','Dispatcher','Agent','Installer','AgentPlus','ManagementPortal')]
        [System.String] $Component,

        ## RES ONE Automation component version to be installed, i.e. 7.0.4 or 7.5.3.1
        [Parameter(Mandatory)]
        [System.String] $Version,

        ## The specified Path is a literal file reference (bypasses the Version check).
        [Parameter()]
        [System.Boolean] $IsLiteralPath
    )
    process {

        if (([System.String]::IsNullOrWhitespace($Version)) -and (-not $IsLiteralPath)) {

            throw ($localizedData.VersionNumberRequiredError);
        }
        elseif ($IsLiteralPath) {

            if ($Path -notmatch '\.msi$') {

                throw ($localizedData.SpecifedPathTypeError -f $Path, 'MSI');
            }
        }
        elseif ($Version -notmatch '^\d\d?\.\d\d?(\.\d\d?|\.\d\d?\.\d\d?)?$') {

            throw ($localizedData.InvalidVersionNumberFormatError -f $Version);
        }
        else {

            $versionMajor = $Version.Split('.')[0] -as [System.Int32];
            if (($Component -in 'AgentPlus','ManagementPortal') -and ($versionMajor -lt 10)) {
                
                throw ($localizedData.InvalidComponentVersionError -f $Component, 10);
            }
        }

        if ($IsLiteralPath) {

            $packagePath = $Path;
        }
        else {

            [System.Version] $productVersion = $Version;

            switch ($productVersion.Major) {

                10 {

                    switch ($productVersion.Minor) {

                        0 {

                            ## ProductName is only used by the 'Installer' and 'ManagementPortal' component
                            $productName = 'RES ONE Automation';
                            ## PackageName is used by all other components
                            $packageName = 'RES-ONE-Automation';
                        }
                        1 {

                            $productName = 'RES ONE Automation';
                            $packageName = 'RES-ONE-Automation';
                        }
                        2 {
                            
                            $productName = 'RES ONE Automation';
                            $packageName = 'RES-ONE-Automation';
                        }
                        Default {

                            throw ($localizedData.UnsupportedVersionError -f $productVersion.ToString());
                        }

                    } #end switch version minor

                } #end version 10

                7 {

                    switch ($productVersion.Minor) {

                        0 {

                            $packageName = 'RES-AM';
                            $productName = 'RES-AM-2014';
                        }
                        5 {

                            $packageName = 'RES-ONE-Automation';
                            $productName = 'RES-ONE-Automation-2015';
                        }
                        Default {

                            throw ($localizedData.UnsupportedVersionError -f $productVersion.ToString());
                        }

                    } #end switch version minor

                } #end version 7

                Default {

                    throw ($localizedData.UnsupportedVersionError -f $productVersion.ToString());
                }

            } #end switch version major

            ## Calculate the version search Regex. RES AM uses version numbers in the Console, Agent and Dispatcher MSIs
            ## This isn't used by the 'Installer' component as that has the SR moniker instead (like RES WM).
            if (($productVersion.Build -eq -1) -and ($productVersion.Revision -eq -1)) {

                ## We only have 'Major.Minor'
                $versionRegex = '{0}.{1}.\S+' -f $productVersion.Major, $productVersion.Minor;
            }
            elseif ($productVersion.Revision -eq -1) {

                ## We have 'Major.Minor.Build'
                $versionRegex = '{0}.{1}.{2}.\S+' -f $productVersion.Major, $productVersion.Minor, $productVersion.Build;
            }
            else {

                ## We have explicit version.
                $versionRegex = '{0}.{1}.{2}.{3}' -f $productVersion.Major, $productVersion.Minor, $productVersion.Build, $productVersion.Revision;
            }

            switch ($Component) {

                'Installer' {

                    if ($productVersion.Major -ge 10) {
                        
                        ## We need the RES ONE Automation 10.0.100.0.msi
                        $regex = '{0} {1}.msi' -f $productName, $versionRegex;
                    }
                    elseif ($productVersion.Build -eq 0) {

                        ## We're after the RTM release, e.g. specified 9.9.0 or 9.10.0
                        $regex = '{0}.msi' -f $productName;
                    }
                    elseif ($productVersion.Build -ge 1) {

                        ## We're after a specific SR, e.g. specified 9.9.3 or 9.10.2
                        $regex = '{0}-SR{1}.msi' -f $productName, $productVersion.Build;
                    }
                    else {

                        ## Find any
                        $regex = '{0}(-SR\d)?.msi' -f $productName;
                    }

                }

                'Agent' {

                    ## RES-ONE-Automation-2015-Agent-7.5.3.1 or RES-AM-Agent-7.0.4.3
                    $regex = '{0}-Agent-{1}.msi' -f $packageName, $versionRegex;

                } #end switch Agent

                'AgentPlus' {

                    $architecture = 'x86';
                    if ([System.Environment]::Is64BitOperatingSystem) {

                        $architecture = 'x64';
                    }

                    ## RES-ONE-Automation-Agent+(x64)-10.0.100.0
                    $regex = '{0}-Agent\+\({1}\)-{2}.msi' -f $packageName, $architecture, $versionRegex;
                }

                'Dispatcher' {

                    $architecture = 'x86';
                    if ([System.Environment]::Is64BitOperatingSystem) {

                        $architecture = 'x64';
                    }
                    ## RES-ONE-Automation-2015-Dispatcher+(x64)-7.5.3.1 or RES-AM-Dispatcher+(x64)-7.0.4.3
                    $regex = '{0}-Dispatcher\+\({1}\)-{2}.msi' -f $packageName, $architecture, $versionRegex;

                } #end switch Dispatcher

                'ManagementPortal' {

                    ## RES ONE Automation Management Portal 10.0.100.0.msi
                    $regex = '{0} Management Portal {1}.msi' -f $productName, $versionRegex;
                    
                }

                Default {

                    ## RES-ONE-Automation-2015-Console-7.5.3.1 or RES-AM-Console-7.0.4.3
                    $regex = '{0}-Console-{1}.msi' -f $packageName, $versionRegex;

                } #end switch Console/Database

            } #end switch component

            Write-Verbose -Message ($localizedData.SearchFilePatternMatch -f $regex);

            $packagePath = Get-ChildItem -Path $Path -Recurse |
                Where-Object { $_.Name -imatch $regex } |
                    Sort-Object -Property Name -Descending |
                        Select-Object -ExpandProperty FullName -First 1;

            if ((-not $IsLiteralPath) -and (-not [System.String]::IsNullOrEmpty($packagePath))) {

                Write-Verbose ($localizedData.LocatedPackagePath -f $packagePath);
                return $packagePath;
            }
            elseif ([System.String]::IsNullOrEmpty($packagePath)) {

                throw  ($localizedData.UnableToLocatePackageError -f $Component);
            }

        } #end if

    } #end process
} #end function