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
146
147
148
149
150
151
152
153
154
155
156
157
158
function ModuleRoot { $MyInvocation.ScriptName | Split-Path -Parent}
$PrivPath = $(Join-Path $(ModuleRoot | Split-Path -Parent) "\Private")
. "$PrivPath\NewInstanceHelperFunctions.ps1"
. "$PrivPath\New-SymLink.ps1"

<#
.SYNOPSIS
    Creates a new instance of an IntelliSearch component
.DESCRIPTION
    The New-ISComponentInstance cmdlet will create a new, blank component from an installed component package, complete with a Windows service.
.EXAMPLE
    Install-ISComponentFromNuGet -Name FileshareConnector | New-ISComponentInstance -Name FileShare
.EXAMPLE
    Get-ISComponent -Name Crawler* | New-ISComponentInstance -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()]
        [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( { Test-Path $_ -IsValid})]
        [String] $Path = $IS_Settings.InstanceStore
    )

    begin
    {

    }

    process
    {
        Write-Verbose ("Making new instance {0}" -f $Name)
        if (-not (Test-Path $Path -IsValid))
        {
            Write-Error "Instance path is not valid. Got: $($Path)" -ErrorAction:Stop
        }
        
        $SetupConf = Join-Path $ComponentDirectory "\etc\ISPSInstance.config"
        if (-not (Test-Path $SetupConf))
        {
            Write-Error "Found no valid ISPSInstance.config" -ErrorAction:Stop
        }
        
        Write-Verbose ("Processing ISPSInstance.config for {0}" -f $Name)
        $Xml = New-Object System.Xml.XmlDocument
        $Xml.Load($SetupConf)
        
        $InstanceDirectory = (Join-Path $Path $Name)

        if (Test-Path $InstanceDirectory -PathType Container)
        {
            Write-Error "Instance names must be unique" -ErrorAction:Stop
        }

        $InstallBinPath = Get-ChildItem -Path $ComponentDirectory -Directory | Where-Object { $_.Name -like "bin"}
        $InstallCfgPath = Get-ChildItem -Path $ComponentDirectory -Directory | Where-Object { $_.Name -like "cfg"}

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

        foreach ($Node in $Xml.SelectNodes("//IntelliSearchComponent/*"))
        {   
            switch ($Node.Name)
            {
                "Service"
                {
                    Write-Verbose "Creating new service"
                    $InstanceBinPath = Join-Path $InstanceDirectory "/bin"
                    New-SymLink -Path $InstallBinPath.FullName -SymName $InstanceBinPath -Directory | Out-Null

                    $CopySplat = @{
                        Path        = $InstallCfgPath.FullName
                        Destination = $InstanceDirectory.FullName
                        PassThru    = $true
                        Force       = $true
                        Recurse     = $true
                    }
                    Copy-Item @CopySplat | Out-Null

                    $InstanceExePath = Join-Path $InstanceDirectory $Node.Executable
                    $InstanceCfgFile = Join-Path $InstanceDirectory $Node.ConfigurationFile
                    CreateService $Name $InstanceExePath $InstanceCfgFile $Node.StartupArgument
                }

                "Shortcut"
                {
                    Write-Verbose "Creating new shortcut"
                    $ShortcutName = $Node.ShortcutName
                    if ($ShortcutName -notmatch '.+\.(?:lnk|url)$')
                    {
                        $ShortcutName = $ShortcutName + ".lnk"
                    }

                    if ($ShortcutName -match '.+\.lnk$')
                    {
                        $ShortcutTarget = Join-Path $ComponentDirectory $Node.Target
                        if ($Node.StartupArgument)
                        {
                            $ShortcutArguments = $Node.StartupArgument -f (Join-Path $InstanceDirectory $Node.ConfigurationFile)
                        }
                    }
                    elseif ($ShortcutName -match '.+\.url$')
                    {
                        $ShortcutTarget = $Node.Target
                        $ShortcutArguments = $null
                    }
                    
                    $ShortcutDestination = Join-Path $InstanceDirectory $ShortcutName
                    CreateShortcut $ShortcutTarget $ShortcutDestination $ShortcutArguments
                }

                "WebApplication"
                {
                    Write-Verbose "Creating new WebApplication"
                    CreateWebApplication $Node $Name $ComponentDirectory $InstanceDirectory
                }

                Default
                {
                    Write-Verbose "Node did not return a known type. Got $($Node.Name)"
                }
            }
        }
    }

    end
    {

    }

}