How to Deploy Web Applications to IIS Using Powershell

|

My development team utilizes the old school zip-and-deploy method for building, packaging, and deploying web applications. Our typical web application runs on the Microsoft .NET Framework on a Windows server running IIS (Internet Information Services).

Our deployment strategy going to production servers is much more complex and involves an IT operations team. However, for our test and development servers, we have liberty on how we deploy. I was formerly pushing across all of the files in the build to the webserver from our build server. As the number of files in our web applications has grown, this became quite time-consuming. I needed a way to push over a ZIP archive of the web application, which we are already building, and then deploy it on the webserver.

The first step of this process required a way to deploy the web application on the web server itself (i.e. locally). Since we are a Microsoft/Windows shop, Powershell seemed like a good fit.

The steps for this are straightforward:

  1. Stop the running IIS server
  2. Delete all of the files and folders inside the web application directory
  3. Extract the new web application files to the directory
  4. Start the IIS server

The Script

Save the contents of this code in a text file named Install-WebProject.ps1.

For my purposes, I placed this script into a directory named D:\DeployWebApps. Then I copy a web application build (ZIP file) to this directory and run the script like this:

.\Install-WebProject.ps1 -path C:\inetpub\MyAwesomeWebApp -file MyAwesomeWebApp-1.0.zip

See script contents below.

<#
.SYNOPSIS
    Deploy web project

.DESCRIPTION
    Deploy web project

.PARAMETER path
    Path to the deployment/installation directory
.PARAMETER file
    ZIP file containing the web project to deploy

.EXAMPLE
    .\Install-WebProject.ps1 -path C:\inetpub\MyAwesomeWebApp -file MytAwesomeWebApp-1.0.zip

.NOTES
    Author: Jonathan Franzone
    Date  : March 17, 2021
#>
param (
    [string] $path,
    [string] $file
)

Write-Host @"
===========================================================================
DEPLOYING WEB PROJECT
===========================================================================
ARCHIVE : ${file}
PATH    : ${path}
===========================================================================
"@

# Stop IIS
Write-Host "Stopping IIS..."
iisreset /stop

# Delete existing deployment
Write-Host "Removing existing files..."
Get-ChildItem $path | Remove-Item -Recurse -Force

# Extract release
Write-Host "Deploying new files..."
Expand-Archive -Path $file -DestinationPath $path -Force

# Start IIS
Write-Host "Starting IIS..."

Write-Host @"
===========================================================================
DONE
===========================================================================
"@