Extension/Install-WebApp.ps1

<#
.SYNOPSIS
    Installs web applications using the provided installer.
.DESCRIPTION
    Runs provided installer and supplies parameters to it.
.EXAMPLE
    Install-WebApp.ps1 -SqlServerName localhost -SqlAuthenticationType SQL -SqlUserName sa -AppPoolUserName domain\admin
 
    Install web apps with SQL authentication type. Password for SQL user and App Pool user will be requested interactively.
.EXAMPLE
    Install-WebApp.ps1 -SqlServerName localhost -SqlAuthenticationType WIN -AppPoolUserName domain\admin
 
    Install web apps with WIN authentication type. Password for App Pool user will be requested interactively.
.EXAMPLE
    Install-WebApp.ps1 -SqlServerName localhost -SqlAuthenticationType WIN -AppPoolUserName domain\admin -AppPoolUserPassword (ConvertTo-SecureString "password" -AsPlainText -Force) -Verbose
 
    Install web apps with WIN authentication type. Password for App Pool user provided as a parameter.
#>

[CmdletBinding()]
param (
    # Path to incadea.WebApp.Installer.exe. If omitted then installer is searched in script directory. If installer is not found runtime error will be thrown.
    [Parameter()]
    [string]$InstallerPath = (Join-Path $PSScriptRoot "\incadea.WebApp.Installer.exe"),
    [Parameter()]
    # Product line. Default is "DMS".
    [ValidateSet("DMS", "Fastfit")]
    [string]$SoftwareLine = "DMS",
    # Modules which will be installed. Default is "ALL".
    [Parameter()]
    [ValidateSet("ALL", "WPB", "WSP", "WTC", "WPB/WSP", "WSP/WTC", "WPB/WTC")]
    [string]$Modules = "ALL",
    # Connection stirng to state server. If empty then session type will be "local".
    [Parameter()]
    [string]$StateServerConnectionString,

    # DNS name of SQL server
    [Parameter(Mandatory = $true)]
    [string]$SqlServerName,
    # Name of configuration database. Defaults to "WebApplicationsConfig".
    [Parameter()]
    [string]$SqlDatabaseName = "WebApplicationsConfig",
    # Type of authentication to connect to SQL server. "WIN" is default.
    [Parameter()]
    [ValidateSet("WIN", "SQL")]
    [string]$SqlAuthenticationType = "WIN",
    # If SQL authentication type is selected then this field is mandatory.
    [Parameter()]
    [string]$SqlUserName,
    # If SQL authentication type is selected then this field is mandatory. You can pass it as a parameter value or type it into a prompt.
    [Parameter()]
    [securestring]$SqlUserPassword,

    # User name to be used as application pool identity for IIS application pool.
    # This field is mandatory. You can pass it as a parameter value or type it into a prompt.
    [Parameter()]
    [string]$AppPoolUserName,
    # This field is mandatory. You can pass it as a parameter value or type it into a prompt.
    [Parameter()]
    [securestring]$AppPoolUserPassword,

    [Parameter()]
    [switch]$InitialLoad,
    [Parameter()]
    [switch]$LoadCaptions,
    [Parameter()]
    [string]$LogPath = "C:\Logs\incadea_Web_Application_Suite_$(Get-Date -Format yyyyMMddHHmmss).txt"
)
begin {
    # Ask for SQL connection parameters
    if ($SqlAuthenticationType -eq "SQL") {
        if (!$SqlUserName) { $SqlUserName = Read-Host "Enter SQL user name"; if (!$SqlUserName) { throw "This parameter is mandatory." } }
        if (!$SqlUserPassword) { $SqlUserPassword = Read-Host "Enter SQL user password for [$SqlUserName]" -AsSecureString; if (!$SqlUserPassword) { throw "This parameter is mandatory." } }
    }
    # Ask for application pool parameters
    if (!$AppPoolUserName) { $AppPoolUserName = Read-Host "Enter application pool user name"; if (!$AppPoolUserName) { throw "This parameter is mandatory." } }
    if (!$AppPoolUserPassword) { $AppPoolUserPassword = Read-Host "Enter application pool user password for [$AppPoolUserName]" -AsSecureString; if (!$AppPoolUserPassword) { throw "This parameter is mandatory." } }

    function Get-ArgString {
        param ( $SqlUserPwd = '***', $AppPoolUserPwd = '***' )
        $SqlUserPwd = [System.Net.NetworkCredential]::new("", $SqlUserPwd).Password
        $AppPoolUserPwd = [System.Net.NetworkCredential]::new("", $AppPoolUserPwd).Password
        "$Modules $UseStateServer $StateServerConnectionString $SqlServerName $SqlDatabaseName $SqlAuthenticationType $IsInitialLoad $IsLoadCaptions $SoftwareLine $SqlUserName SQLPASSWORD=$SqlUserPwd $AppPoolUserName PASSWORD=$AppPoolUserPwd"
    }

    Write-Host  "========================================================================================="
    Write-Host  ("WebApp-Installer script starting at " + (Get-Date).ToLongTimeString() + "...")
    Write-Host  "========================================================================================="
}
process {
    $UseStateServer = if ($StateServerConnectionString) { "Y" } else { "N" }
    if (!$StateServerConnectionString) { $StateServerConnectionString = "N" }
    $IsInitialLoad = if ($InitialLoad) { "Y" } else { "N" }
    $IsLoadCaptions = if ($LoadCaptions) { "Y" } else { "N" }
    if (!$SqlUserName) { $SqlUserName = "N" }
    if (!$SqlUserPassword) { $SqlUserPassword = ConvertTo-SecureString "N" -AsPlainText -Force }

    Write-Verbose "Start-Process -FilePath $InstallerPath -Wait -ArgumentList ""/Silent $(Get-ArgString) PowerShellHost -l $LogPath"""

    $ArgString = Get-ArgString -SqlUserPwd $SqlUserPassword -AppPoolUserPwd $AppPoolUserPassword

    Start-Process -FilePath $InstallerPath -Wait -ArgumentList "/Silent $ArgString PowerShellHost -l $LogPath"
}
end {
    Write-Host  "========================================================================================="
    Write-Host  ("WebApp-Installer Script finished at " + (Get-Date).ToLongTimeString() + ".")
    Write-Host  ("Please check logs in '$LogPath' folder for more details.")
    Write-Host  "========================================================================================="
}