Public/Install-ISComponent.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
function ModuleRoot
{
    $MyInvocation.ScriptName | Split-Path -Parent
}

$PublicCmdlets = Join-Path (ModuleRoot | Split-Path -Parent) "Public"
$PrivateCmdlets = Join-Path (ModuleRoot | Split-Path -Parent) "Private"
. $PublicCmdlets\Get-ISComponent.ps1
. $PublicCmdlets\Invoke-ISNuGetCommand.ps1

<#
.SYNOPSIS
    Installs a component from nuget based on name
.DESCRIPTION
    The Install-ISComponent cmdlet will install a nuget package by package name.
.PARAMETER Name
    List of package names to install.
.PARAMETER InstallDirectory
    The directory to install the package to.
    Defaults to the ComponentStore setting. Usually C:\Program Files\IntelliSearch.
.PARAMETER PreRelease
    Flag to allow pre-release packages.
.PARAMETER Version
    Specifies version of package to install.
.PARAMETER Source
    Specifies NuGet source to install from. All enabled sources will be checked if not set.
.EXAMPLE
    C:\PS> Install-ISComponent -Name "IntelliSearch.Server.CrawlerManager"
 
    ComponentName ComponentVersion
    ------------- ----------------
    IntelliSearch.Server.CrawlerManager.6.0.0 6.0.0
 
    This will install the latest version of the IntelliSearch.Server.CrawlerManager component.
.EXAMPLE
    C:\PS> Install-ISComponent -Name "IntelliSearch.Server.CrawlerManager" -Version "7.1.9" -Source "IntelliSearch"
 
    ComponentName ComponentVersion
    ------------- ----------------
    IntelliSearch.Server.CrawlerManager.7.1.9 7.1.9
 
    This will install the 7.1.9 version of the IntelliSearch.Server.CrawlerManager component from IntelliSearch nuget sources.
.EXAMPLE
    C:\PS> Install-ISComponent -Name "IntelliSearch.Server.CrawlerManager" -PreRelease
 
    ComponentName ComponentVersion
    ------------- ----------------
    IntelliSearch.Server.CrawlerManager.6.0.0-Beta.13 6.0.0-Beta.13
 
    This will install the latest pre-release version of the IntelliSearch.Server.CrawlerManager component.
.INPUTS
    System.String
.OUTPUTS
    IntelliSearch.Component.Detail
        An object representing an IntelliSearch component.
#>

function Install-ISComponent
{
    [CmdletBinding(ConfirmImpact = 'Medium')]
    [OutputType()]
    param(
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias()]
        [String[]] $Name,

        [Parameter(
            Mandatory = $false,
            Position = 1,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateScript( { Test-Path $_ -IsValid -PathType:Container})]
        [Alias()]
        [String] $InstallDirectory = $IS_Settings.ComponentStore,

        [Parameter(
            Mandatory = $false,
            Position = 2,            
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias()]
        [String] $Version,

        [Parameter(
            Mandatory = $false,
            Position = 3,            
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias()]
        [String] $Source,

        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true
        )]
        [Alias()]
        [switch] $PreRelease
    )

    begin
    {
        #region Set up NuGet
        if ($Source)
        {
            $NugetSources = Invoke-ISNuGetCommand -Action "sources" -Arguments @{ 'Format' = "Short"; '-NonInteractive' = $null}
            if ($NugetSources | Select-String -Pattern $Source)
            {
                $NuGetArguments = @{
                    'enable'          = $null
                    'Name'            = $Source
                    '-NonInteractive' = $null
                }
                Invoke-ISNuGetCommand -Action "sources" -Arguments $NugetArguments | Out-Null
            }
        }
        #endregion Set up NuGet
    }

    process
    {
        foreach ($aName in $Name)
        {
            $NuGetArguments = @{
                $aName            = $null
                '-NonInteractive' = $null
                'OutputDirectory' = $InstallDirectory                
            }
            
            if ($Prerelease)
            {
                $NugetArguments.Add("-PreRelease", $null)
            }

            if ($Version)
            {
                $NugetArguments.Add("Version", $Version)
            }

            if ($Source)
            {
                $NugetArguments.Add("Source", $Source)
            }
    
            # Execute the built command
            Write-Verbose -Message ("Attempting to install {0} with NuGet" -f $aName)
            $NugetResult = Invoke-ISNuGetCommand -Action "install" -Arguments $NuGetArguments -ErrorAction:Stop

            foreach ($nugetLine in $NugetResult)
            {
                Write-Verbose $nugetLine
            }

            switch -regex ($NugetResult)
            {
                "(?:Added package ')(?<InstalledName>.*)(?:' to folder)"
                {
                    Write-Verbose "Package installed"
                    $InstalledName = $Matches.InstalledName
                }
                '(?:Package \")(?<InstalledName>.*)(?:\" is already installed.)'
                {
                    Write-Verbose "Package already installed."
                    $InstalledName = $Matches.InstalledName
                }
                Default
                {
                    Write-Error "NuGet output unknown. Could not find the installed package." -ErrorAction:Stop
                }
            }

            Write-Verbose "Found package name: $($InstalledName)"
            Get-ISComponent -Name $InstalledName
        }
    }

    end
    {
    }
}