Public/Install-ISComponent.ps1

function ModuleRoot
{
    $MyInvocation.ScriptName | Split-Path -Parent
}

$PublicCmdlets = Join-Path (ModuleRoot | Split-Path -Parent) "Public"
. $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.
.EXAMPLE
    Install-ISComponent -Name "CrawlerManagerService"
    This will install the CrawlerManagerService component. If you want a pre-release package, append the -prerelease switch.
.INPUTS
    System.String
.OUTPUTS
    None
#>

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,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateScript( { Test-Path $_ -IsValid -PathType:Container})]
        [Alias()]
        [String] $InstallDirectory = $IS_Settings.ComponentStore,

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

    begin
    {
        #region Set up NuGet
        $NugetSources = Invoke-ISNuGetCommand -Action "sources" -Arguments @{ 'Format' = "Short"; '-NonInteractive' = $null}
        if ($NugetSources | Select-String -Pattern "D http://nuget.intellisearch.com/nuget/Packages")
        {
            $NuGetArguments = @{
                'enable'          = $null
                'Name'            = "IntelliSearch"
                '-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
                'Source'          = "IntelliSearch"
            }
            
            if ($Prerelease)
            {
                $NugetArguments.Add("-PreRelease", $null)
            }
    
            # Execute the built command
            Write-Verbose -Message ("Attempting to install {0} with NuGet" -f $aName)
            $NugetResult = Invoke-ISNuGetCommand -Action "install" -Arguments $NuGetArguments
            
            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-Verbose "Found package name: $($InstalledName)"
            Get-ISComponent -Name $InstalledName
        }
    }

    end
    {
    }
}