InstanceCreationPlugins/webapplication-plugin.psm1

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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#requires -version 4

function ModuleRoot
{
    $MyInvocation.ScriptName | Split-Path -Parent
}
$PrivPath = $(Join-Path $(ModuleRoot | Split-Path -Parent) "\Private")

<#
.SYNOPSIS
    Plugin for creating web applications as defined in the ISPSInstance.config
.DESCRIPTION
    This plugin only acts on nodes with the Name attribute of "WebApplication".
    This element is an instruction to create an application shortcut or a URL shortcut.
    The shortcut will be placed in the %progdata%\IntelliSearch\<InstanceName>\<ShortcutName> folder
    The target path should be expanded and the shortcut path would look something like this:
    C:\ProgramFiles\IntelliSearch\<packageName>\bin\someexecutablefile.exe -f C:\ProgramData\IntelliSearch\<InstanceName>\someconfigfile.exe.config
 
    Both the StartupArgument and ConfigurationFile parameters are optional, but the ConfigurationFile parameter will not be used unless the StartupArgument is specified.
    The {0} in the StartupArgument parameter is a placeholder for the configuration path.
 
    <!--This element is an instruction to create a regular IIS website.
    The web client should be copied to its own instance folder.
    The website root should be at the absolute path to the WebRoot element in the instance copy.
    -->
    <WebApplication
        Type="IIS"
        Classic="true"
        WebRoot="\WebClient">
        <!--This element lists all authentication settings that should be set by default on an IIS website
        If DisableUnspecified is set to true, all authentication methods are disabled except for the ones listed here-->
        <SecurityModes DisableUnspecified="false">
            <!--SecurityMode elements specify what modes of authentication should be enabled or disabled-->
            <SecurityMode
                Name="basicAuthentication"
                Enabled="true" />
            <SecurityMode
                Name="windowsAuthentication"
                Enabled="true" />
        </SecurityModes>
    </WebApplication>
 
.EXAMPLE
    $Splat = @{
        Node = <XML node>
        InstanceName = "WebClient"
        InstanceDirectory = "C:\ProgramData\IntelliSearch\MyInstance\"
        ComponentDirectory "C:\ProgramFiles\IntelliSearch\IntelliSearch.Server.IndexManager.4.0.2\"
    }
    shortcut.plugin @Splat
.PARAMETER Node
    The XML node currently being iterated over
.PARAMETER InstanceName
    A hashtable with arguments for the plugin.
.PARAMETER InstanceDirectory
    The root directory of the new instance
.PARAMETER ComponentDirectory
    The root directory of the installed component
.INPUTS
    System.String
.OUTPUTS
    None
#>

function webapplication-plugin
{
    param (
        # XML node from ISPSInstance.config
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "XML node from ISPSInstance.config")]
        $Node,

        # The name representing the new instance
        [Parameter(
            Mandatory = $true,
            Position = 1,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "Instance name")]
        [ValidateNotNullOrEmpty()]
        [string] $InstanceName,

        # Path to the directory of the new instance
        [Parameter(
            Mandatory = $true,
            Position = 2,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "Path to the directory of the new instance")]
        [ValidateNotNullOrEmpty()]
        [ValidateScript( {
                if (Test-Path -Path $_)
                {
                    $true
                }
                else
                {
                    Throw [System.Management.Automation.ItemNotFoundException] "${_}"
                }
                if (Test-Path -Path $_ -PathType Container)
                {
                    $true
                }
                else
                {
                    Throw [System.Management.Automation.ValidationMetadataException] "The path '${_}' is not a directory."
                }
            })]
        [string] $InstanceDirectory,

        # Path to the component nuget package directory
        [Parameter(
            Mandatory = $true,
            Position = 3,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "Path to the component nuget package directory")]
        [ValidateNotNullOrEmpty()]
        [ValidateScript( {
                if (Test-Path -Path $_)
                {
                    $true
                }
                else
                {
                    Throw [System.Management.Automation.ItemNotFoundException] "${_}"
                }
                if (Test-Path -Path $_ -PathType Container)
                {
                    $true
                }
                else
                {
                    Throw [System.Management.Automation.ValidationMetadataException] "The path '${_}' is not a directory."
                }
            })]
        [string] $ComponentDirectory
    )

    if ($Node.Name -ne "WebApplication")
    {
        Write-Debug "Node name was not WebApplication"
        return
    }

    if ($Node.Type -ne "IIS")
    {
        Write-Debug "Node type was not IIS"
        return
    }

    if ((Get-WebSite -Name $InstanceName) -ne $null)
    {
        Throw "A website already exists with the name $InstanceName"
    }

    # Create an app pool for the website so we don't overwrite an older pool
    $AppPool = CreateWebAppPool -Name $InstanceName -Classic:([System.convert]::ToBoolean($Node.Classic))
    $WebSite = New-WebSite -Name $InstanceName -PhysicalPath (Join-Path $InstanceDirectory $Node.WebRoot) -ApplicationPool $AppPool.Name

    # Splat to simplify changes to security modes
    $WebConfSplat = New-Object -TypeName System.Collections.Hashtable
    $WebConfSplat.Filter = "/system.webServer/security/authentication/*"
    $WebConfSplat.Name = "Enabled"
    $WebConfSplat.PSPath = "IIS:\"
    $WebConfSplat.Location = "{0}" -f $InstanceName

    # DisableUnspecified flag disables all security modes before applying the specified modes.
    if ($Node.SecurityModes.DisableUnspecified)
    {
        $AuthenticationTypes = Get-WebConfigurationProperty @WebConfSplat

        $WebConfSplat.ErrorAction = "SilentlyContinue"
        $WebConfSplat.Value = $False
        foreach ($AuthType in $AuthenticationTypes)
        {
            $WebConfSplat.Filter = $AuthType.ItemXPath
            Set-WebConfigurationProperty @WebConfSplat | Out-Null
        }
    }

    # We want to continue applying settings even if the node is no longer valid
    $WebConfSplat.ErrorAction = "SilentlyContinue"
    foreach ($SecurityMode in $Node.SelectNodes("//SecurityModes/*"))
    {
        $WebConfSplat.Filter = "/system.webServer/security/authentication/{0}" -f $SecurityMode.Mode
        $WebConfSplat.Value = $SecurityMode.Enabled

        Write-Verbose ("Setting security mode for {0}" -f $SecurityMode.Mode)
        Set-WebConfigurationProperty @WebConfSplat | Out-Null
    }

    # Return the website info
    $WebSite
}

function CreateWebAppPool ([string]$Name, [switch]$Classic)
{
    # Forcefully overwrite a pool if it already exists.
    $AppPool = New-WebAppPool -Name $Name -Force

    if ($Classic)
    {
        $AppPool.managedPipelineMode = "Classic"
    }

    # Set any changes done to the pool before returning
    $AppPool | Set-Item -PassThru
}