internal/functions/Find-SqlInstanceSetup.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
function Find-SqlInstanceSetup {
    <#
        .SYNOPSIS
            Returns a SQL Server setup.exe filesystem object based on parameters
        .DESCRIPTION
            Recursively searches specified folder for a setup.exe file that has the following .VersionInfo:
            - FileDescription in :
                * Sql Server Setup Bootstrapper
                * Native SQL Install Bootstrapper
            - Product:
                Microsoft SQL Server
                Microsoft SQL Server Setup
            - ProductVersion: Major and Minor versions should be the same
 
        .EXAMPLE
            PS> Find-SqlInstanceSetup -Version 11.0 -Path \\my\updates
 
            Looks for setup.exe in \\my\updates and all the subfolders
    #>

    [CmdletBinding()]
    Param
    (
        [DbaInstanceParameter]$ComputerName = $env:COMPUTERNAME,
        [pscredential]$Credential,
        [ValidateSet('Default', 'Basic', 'Negotiate', 'NegotiateWithImplicitCredential', 'Credssp', 'Digest', 'Kerberos')]
        [string]$Authentication = 'Default',
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [version]$Version,
        [string[]]$Path

    )
    begin {
    }
    process {
        $getFileScript = {
            Param (
                [string[]]$Path,
                [version]$Version
            )
            $excludePath = @(
                'sql2008support\pfiles\sqlservr\100\setup\release' #SQL2008 support in more recent installations
            )
            foreach ($folder in (Get-Item -Path $Path -ErrorAction Stop)) {
                $file = Get-ChildItem -Path $folder -Filter 'setup.exe' -File -Recurse -ErrorAction Stop
                foreach ($f in $file) {
                    try {
                        $currentVersion = [version]$f.VersionInfo.ProductVersion
                    } catch {
                        $currentVersion = $null
                    }
                    if (
                        $f.VersionInfo.ProductName -in 'Microsoft SQL Server', 'Microsoft SQL Server Setup' -and
                        $f.VersionInfo.FileDescription -in 'Sql Server Setup Bootstrapper', 'Native SQL Install Bootstrapper' -and
                        $currentVersion.Major -eq $Version.Major -and
                        $currentVersion.Minor -eq $Version.Minor
                    ) {
                        foreach ($exPath in $excludePath) {
                            if ($f.FullName -notlike "*$exPath*") { return $f.FullName }
                        }
                    }
                }
            }
        }
        $params = @{
            ComputerName   = $ComputerName
            Credential     = $Credential
            Authentication = $Authentication
            ScriptBlock    = $getFileScript
            ArgumentList   = @($Path, $Version.ToString())
            ErrorAction    = 'Stop'
            Raw            = $true
        }
        Invoke-CommandWithFallback @params
    }
}