Automated deployment to an IIS server with Powershell

This is a very simple Powershell script that will backup and deploy application files to an IIS server. The script will also recycle the app pool after deploying. It's important to note that in a production setting, deployments should be handled by a CI/CD platform that can provide a controlled environment where deployments can be tested, audited, and rolled back if necessary.

param(
    [Parameter(Mandatory=$true)]
    [string]$sourcePath,

    [Parameter(Mandatory=$true)]
    [string]$destinationPath,

    [Parameter(Mandatory=$true)]
    [string]$backupPath,

    [Parameter(Mandatory=$true)]
    [string]$iisSiteName
)

# Import WebAdministration Module
Import-Module WebAdministration

# Check if the source path exists
if (!(Test-Path -Path $sourcePath)) {
    Write-Host "Source path '$sourcePath' does not exist. Exiting..."
    exit 1
}

# Check if the destination path exists
if (!(Test-Path -Path $destinationPath)) {
    Write-Host "Destination path '$destinationPath' does not exist. Exiting..."
    exit 1
}

# Check if the backup path exists, if not create it
if (!(Test-Path -Path $backupPath)) {
    Write-Host "Backup path '$backupPath' does not exist. Creating it now..."
    New-Item -ItemType Directory -Force -Path $backupPath
}

# Backup current application files
Copy-Item -Path $destinationPath -Destination $backupPath -Recurse -Force

# Copy new files from source to destination
Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse -Force

# Restart the website in IIS
Restart-WebSite -Name $iisSiteName

Write-Host "Deployment complete"