Public/New-ISInstance.ps1

#requires -version 4
function ModuleRoot { $MyInvocation.ScriptName | Split-Path -Parent}
$PrivPath = $(Join-Path $(ModuleRoot | Split-Path -Parent) "\Private")
$PluginPath = $(Join-Path $(ModuleRoot | Split-Path -Parent) "\InstanceCreationPlugins")
. $PrivPath\HelperFunctions.ps1
. $PrivPath\New-SymLink.ps1
. $PrivPath\Open-ISComponentConfiguration.ps1
. $PrivPath\Register-ISPlugins.ps1
. $PrivPath\Invoke-ISPlugins.ps1

<#
.SYNOPSIS
    Creates a new instance of an IntelliSearch component
.DESCRIPTION
    The New-ISInstance cmdlet will create a new, blank component from an installed component package.
    It will use the configuration file found in the package at /etc/ISPSInstance.config
.EXAMPLE
    Install-ISComponent -Name IntelliSearch.Connector.File | New-ISInstance -Name FileShare
.EXAMPLE
    Get-ISComponent -Name Crawler* | New-ISInstance -Name FileCrawlerManager
.INPUTS
    Component, System.String
.OUTPUTS
    Instance
#>

function New-ISInstance
{
    [CmdletBinding(ConfirmImpact = 'Low')]
    [OutputType()]
    param(
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateScript( {
            if (ValidateInstanceName $_)
            {
                return $true
            }
            else
            {
                Throw [System.Management.Automation.ValidationMetadataException] "The instance name '${_}' is not valid."
            }
        })]
        [Alias("ComponentName")]
        [String] $Name,

        [Parameter(
            Mandatory = $true,
            Position = 1,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateScript( { Test-Path $_ -PathType Container})]
        [Alias()]
        [String] $ComponentDirectory,

        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true
        )]
        [Alias()]
        [ValidateScript( {
            if (ValidatePath $_)
            {
                return $true
            }
            else
            {
                Throw [System.Management.Automation.ValidationMetadataException] "The instance store path '${_}' is not valid."
            }
        })]
        [String[]] $Path = $IS_Settings.InstanceStore
    )

    begin
    {
        Write-Verbose "Loading plugins..."
        $Plugins = Register-ISPlugins $IS_Settings.InstanceCreationPluginStore
        Write-Verbose "Loaded $($Plugins.Count) plugins."
    }

    process
    {
        $ConfigurationPath = Join-Path $ComponentDirectory "\etc\ISPSInstance.config"
        $InstanceDirectory = (Join-Path $Path $Name)

        Write-Verbose ("Making new instance {0}" -f $Name)
        if (-not (ValidatePath $Path))
        {
            Write-Error "Instance path is not valid. Got: $($Path)" -ErrorAction:Stop
        }

        if (Test-Path $InstanceDirectory -PathType Container)
        {
            Write-Error "Instance name already exists. Names must be unique. Got $Name" -ErrorAction:Stop
        }

        Write-Verbose ("Processing ISPSInstance.config for {0}" -f $Name)
        try
        {
            $ConfigurationNodes = Open-ISComponentConfiguration -Path $ConfigurationPath
        }
        catch [System.Management.Automation.ParameterBindingException], [System.Management.Automation.MethodInvocationException]
        {
            Throw "Found no valid ISPSInstance.config file"
        }

        $InstanceDirectory = New-Item -Path $InstanceDirectory -ItemType Container

        $PluginArguments = @{
            "InstanceName"       = $Name
            "InstanceDirectory"  = $InstanceDirectory
            "ComponentDirectory" = $ComponentDirectory
        }

        if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent)
        {
            $PluginArguments.Add("Verbose", $true)
        }

        foreach ($Node in $ConfigurationNodes.ChildNodes)
        {
            Write-Verbose "Working on node: $($Node.Name)"
            # If the child node is a comment, skip it
            if ($Node.Name -eq "#comment")
            {
                continue
            }

            $PluginArguments.Node = $Node

            Write-Verbose "Arguments: $($PluginArguments | Out-String)"
            Invoke-ISPlugins -Plugins $Plugins -PluginArguments $PluginArguments
        }
    }

    end
    {

    }

}