PowerShell – Backup / Restore IIS Site and Configuration

|
<#
.SYNOPSIS
    This script will archive IIS App Pool and IIS Sites
.DESCRIPTION
    This script will archive all App Pool and IIS Site settings and configurations.
    This script will also allow the restore of the App Pool and IIS Site settings and configurations.
.PARAMETER Path    
.PARAMETER LiteralPath
.Example
    IISBackup.ps1 1
        Run Backup Process
    IISBackup.ps1 2
        List all archives
    IISBackup.ps1 3 IIS_11-10-2015_011330
.Inputs
    [String]$Operation
    [String]$ArchiveName
#>

Import-Module WebAdministration

function test-variable
    {
        # return $false if variable:\$name is missing or $null
        param( [string]$name )
        $isMissingOrNull = (!(test-path ('variable:'+$name)) -or ((get-variable -name $name -value) -eq $null))
        return !$isMissingOrNull
    }

$operation = $args[0]
$restoreloc = $args[1]

# If operation was not passed we will force 0 to show instructions
if (!(test-variable "operation")) {$operation ="0"}

if ($operation -eq 3)
    {
        if (!(test-variable "restoreloc"))
            {
                $operation ="0"
            }
    }

clear

$CurrentDate = Get-Date -format M-d-yyyy_hhmmss
$BackupName = "IIS_$($CurrentDate)"

# Backup Web Configuration
# Backup will get created in C:\Windows\System32\inetsrv\backup
# Backup-WebConfiguration -Name  $BackupName

# Restore Web Configuration
# Restore-WebConfiguration -Name IIS_11-10-2015_120802

write-host "IIS Backup/Restore"

switch ($operation)
    {
        "0" {
            "`nBackup / Restore Process"
            Write-Host "    To backup run:`n        IISBackup.ps1 1`n`n    To list backups run:`n        IISBackup.ps1 2`n`n    To restore run:`n        IISBackup.ps1 3 <Backup Name>"
        }
        "1" {
            "Backup Process Selected"
            Backup-WebConfiguration -Name  $BackupName

            # Write Documentation - Start
            $sites = @{Expression={$_.Name};Label="Site Name"}, ` @{Expression={$_.applicationPool};Label="Site App Pool";}, ` @{Expression={$_.PhysicalPath};Label="Site Physical Path";}

            dir IIS:\Sites | Format-Table $sites -AutoSize > $("C:\Windows\System32\inetsrv\backup\$BackupName\IIS.txt")

            "Web Config File Paths:" >> $("C:\Windows\System32\inetsrv\backup\$BackupName\IIS.txt")

            ForEach($item in (dir IIS:\Sites))
                {
                    $item.Name >> $("C:\Windows\System32\inetsrv\backup\$BackupName\IIS.txt")
                    $filePath = $item.PhysicalPath
                    $fileName = "web.config"
                    Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue | Where-Object { ( $_.Name -like "*$fileName*") } | Select-Object FullName | format-Table * -AutoSize -HideTableHeaders >> $("C:\Windows\System32\inetsrv\backup\$BackupName\IIS.txt")
                }
            # Write Documentation - Stop

        }
        "2" {
            "Backups" 
            $currentdirectorypath = Split-Path (Get-Variable MyInvocation).Value.MyCommand.Path
            cd "C:\Windows\System32\inetsrv\backup"
            ls
            cd $currentdirectorypath
        }
        "3" {
            "Restore Process Selected"            
            Write-host "Restoring $($restoreloc)"
            Restore-WebConfiguration -Name $restoreloc
        }
    }
Originally Posted on November 10, 2015
Last Updated on June 8, 2017
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.