SkyWire-PSWebConfig.psm1


<#
 .SYNOPSIS
  Create a new Web Config application in IIS called WebConfig.
 
 .DESCRIPTION
  A new Web Config application will be created under the specified tag. For example, if POS-0000 is specified for the tag, the default IIS app path will be "Default Web Site/POS-0000/WebConfig".
 
 .PARAMETER tag
  The tag (ticket number) for the WebConfig build to download and deploy. This tag will be used on every relevant name field (ex: folder names, db names, etc).
 
 .PARAMETER rootDir
  Specify the root directory where all data will reside for the current deployment (default: %TEMP%\SkyWire-PSInstaller).
 
 .PARAMETER rootSite
  Specify the root IIS Site where the web applications reside (default: Default Web Site).
 
 .PARAMETER posDbBacpac
  The POS bacpac file to use as the starting db with data before applying the dacpac file for this build (default: %LOCALAPPDATA%\SkyWire-PSInstaller\SkyWire.Pos.Database.bacpac).
 
 .PARAMETER dbServer
  The database server (default: localhost).
 
 .PARAMETER dbUser
  The database user id.
 
 .PARAMETER dbPassword
  The database password.
 
 .PARAMETER smtpUser
  The user for SMTP account.
 
 .PARAMETER smtpPassword
  The password for SMTP account.
 
 .NOTES
  All related names will use the tag as the identifier (ex: folder names, db name, app container name, etc).
  By default, the database will use Trusted Connection. Provide dbUser and dbPassword if user/password need to be used.
 
 .EXAMPLE
  New-WebConfig
 
 .EXAMPLE
  New-WebConfig -tag POS-0000 -posTag POS-0000
 
 .EXAMPLE
  New-WebConfig -tag POS-0000 -posTag POS-0000 -dbUser sa -dbPassword password
#>

function New-SWWebConfig {
  param(
    [Parameter(Mandatory=$true)]
    [string] $tag,

    [Parameter(Mandatory=$false)]
    [string] $rootDir = "$env:PROGRAMDATA\SkyWire-PSInstaller",

    [Parameter(Mandatory=$false)]
    [string] $rootSite = "Default Web Site",

    [Parameter(Mandatory=$false)]
    [string] $name = "$tag-WebConfig",

    [Parameter(Mandatory=$false)]
    [string] $dbServer = "localhost",

    [Parameter(Mandatory=$false)]
    [string] $dbUser = "",

    [Parameter(Mandatory=$false)]
    [string] $dbPassword = "",

    [Parameter(Mandatory=$false)]
    [string] $smtpUser = "mailUser",

    [Parameter(Mandatory=$false)]
    [string] $smtpPassword = "mailPassword"
  )

  $trustedConnection = !$dbUser -and !$dbPassword
  $connectionStringSuffix = "Server=$dbServer;Integrated Security=$trustedConnection;"
  if (!($trustedConnection)) {
    $connectionStringSuffix = "$($connectionStringSuffix)User Id=$dbUser;Password=$dbPassword;"
  }

  if (!$rootSite) {
    throw "The parameter rootSite is required."
  }
  $package = Get-SWInstallPackage -app "WebConfig" -tag $tag -rootDir $rootDir

  try {
    $parameters = @{
      "IIS Web Application Name" = "$rootSite/$($name)"
      "PointOfSaleConnection-Web.config Connection String" = "Initial Catalog=$tag-POS;$connectionStringSuffix"
      "SkywireSchedulerConnection-Web.config Connection String" = "Initial Catalog=$tag-SRDM;$connectionStringSuffix"
      "ReportingContext-Web.config Connection String" = "Initial Catalog=$tag-Reporting;$connectionStringSuffix"
      "SMTP Username" = $smtpUser
      "SMTP Password" = $smtpPassword
    }

    Restore-WDPackage $package -Parameters $parameters
  } catch {
    Write-Error "An error occurred while installing WebConfig. This can be caused by an IIS permissions or configuration problem, such as web deploy not being installed."
    throw $_.Exception
  }
}

<#
 .SYNOPSIS
  Remove an existing SkyWire Web Config application.
 
 .DESCRIPTION
  This will remove a SkyWire Web Config application in IIS.
 
 .PARAMETER tag
  The version of Web Config.
 
 .PARAMETER rootSite
  Specify the root IIS Site where the web applications reside (default: Default Web Site).
 
 .PARAMETER name
  If Web Config was deployed to a custom name, specify name (default: $tag-WebConfig).
 
 .PARAMETER workingDir
  The directory of the Web Config application (default: C:\inetpub\wwwroot\$name).
 
 .EXAMPLE
  Remove-SWWebConfig -tag POS-1337-beta
 
 .EXAMPLE
  Remove-SWWebConfig -tag POS-1337-beta -rootSite MyCustomSite
 
 .EXAMPLE
  Remove-SWWebConfig -name MyWebConfig
#>

function Remove-SWWebConfig {
  param(
    [Parameter(Mandatory=$false)]
    [string] $tag,

    [Parameter(Mandatory=$false)]
    [string] $rootSite = "Default Web Site",

    [Parameter(Mandatory=$false)]
    [string] $name = "$tag-WebConfig",

    [Parameter(Mandatory=$false)]
    [string] $workingDir = "C:\inetpub\wwwroot\$name"
  )

  try {
    Remove-WebApplication -Site $rootSite -Name $name
    Remove-Item -Path $workingDir -Recurse -Force
  } catch {
    Write-Error "An error occurred while removing WebConfig. This can be caused by an IIS permissions or configuration problem, such as web deploy not being installed."
    throw $_.Exception
  }
}