DSCResources/ROACommon/Src/Resolve-ROAPackagePath.ps1
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)] [ValidateNotNullOrEmpty()] [System.String] $Path, ## Required RES ONE Automation/Automation Manager component [Parameter(Mandatory)] [ValidateSet('Console','Dispatcher','Agent','Installer')] [System.String] $Component, ## RES ONE Automation component version to be installed, i.e. 7.0.4 or 7.5.3.1 [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [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?)?$') { throw ($localizedData.InvalidVersionNumberFormatError -f $Version); } if ($IsLiteralPath) { $packagePath = $Path; } else { [System.Version] $productVersion = $Version; switch ($productVersion.Major) { 7 { switch ($productVersion.Minor) { ## ProductName is only used by the 'Installer' component, PackageName is used by all other components 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 } 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' { ## We need the RES-ONE-Automation-2015-SR3.msi or RES-AM-2014-SR4.msi if ($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 '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 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 Resolve-ROAPackagePath |