Skip to content

Instantly share code, notes, and snippets.

@niaher
Last active April 2, 2019 08:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niaher/6711c02a33738aecea9f to your computer and use it in GitHub Desktop.
Save niaher/6711c02a33738aecea9f to your computer and use it in GitHub Desktop.
This script configures IIS application pool and site. It needs to be run once on each machine where the app is deployed. Run it in powershell with admin rights. For example to deploy release config run `server-setup.ps1 -Target release`
# This script configures IIS application pool and site.
# This script needs to be run once on each machine where the app is deployed.
# Run this file in powershell with admin rights. For example to deploy release config:
# PS > server-setup.ps1 -Target release
param (
[Parameter(Mandatory=$true,
HelpMessage="Name of the build (e.g. - local, debug, release, etc)")]
[string]$Target
)
function Get-Config ([string]$path) {
return (Get-Content -Path $path | Where { $_ -notmatch "^[\s]*//" }) -join ' ' | ConvertFrom-Json
}
function Init-Server
{
# Load config.
$config = Get-Config "server-setup.$Target.json"
if ($config -eq $null)
{
return;
}
$apppool = $config.appPool
$appName = $config.appName
$site = $config.site
$appPath = $config.appVirtualDirectory
$appPhysicalPath = $config.appPhysicalPath
# Get app pool credentials.
$credential = Get-Credential -Message 'Please enter account name and password under which IIS Application Pool will run'
if ($credential.UserName -eq $null)
{
Write-Host 'Aborting server setup, as no credentials were supplied.' -ForegroundColor Yellow
return;
}
$appcmd = [System.Environment]::ExpandEnvironmentVariables("%systemroot%\system32\inetsrv\AppCmd.exe")
# Ensure app pool.
$appPoolExists = (&$appcmd list apppool /name:$apppool) -ne $null
if ($appPoolExists -eq $false)
{
&$appcmd add apppool `
/name:$apppool `
/managedRuntimeVersion:'v4.0' `
/managedPipelineMode:'Integrated'
}
# Configure app pool.
&$appcmd set config /section:applicationPools `
"/[name='$($config.appPool)'].processModel.identityType:SpecificUser" `
"/[name='$($config.appPool)'].processModel.userName:`"$($credential.UserName)`"" `
"/[name='$($config.appPool)'].processModel.password:`"$($credential.Password)`"" `
/commit:apphost
# Ensure app.
$appExists = (&$appcmd list app /path:/$appPath) -ne $null
if ($appExists -eq $false)
{
&$appcmd add app /site.name:$site `
"/path:/$appPath" `
"/physicalPath:$appPhysicalPath"
}
# Configure app.
&$appcmd set app /app.name:$site/$appPath /applicationPool:$apppool
Write-Host 'Server setup completed successfully.' -ForegroundColor Green
}
Init-Server
{
"appPool": "MyApp",
"appName": "MyApp",
"site": "Default Web Site",
"appVirtualDirectory": "MyApp",
"appPhysicalPath": "C:\\Data\\Projects\\MyApp"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment