Public/New-ISInstance.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
#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
    {

    }

}