Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – Backup / Restore IIS Site and Configuration

Posted on November 10, 2015June 8, 2017 By David Kittell
<#
.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.

Related

Code PowerShell

Post navigation

Previous post
Next post

Related Posts

Code

Ubuntu – WordPress Install

Posted on September 2, 2015May 31, 2017

These steps are based on Ubuntu 14.10 Server 64-bit but can be applied to virtually any Debian based UNIX distribution. Originally Posted on September 2, 2015Last Updated on May 31, 2017 All information on this site is shared with the intention to help. Before any source code or program is…

Read More

Android Contacts – Clear Name If It Matches Company Name

Posted on February 21, 2014October 26, 2015

UPDATE data SET data1 = NULL ,data2 = NULL ,data3 = NULL WHERE mimetype_id IN ( SELECT _id FROM mimetypes WHERE mimetype LIKE "%name%" ) AND raw_contact_id IN ( SELECT raw_contact_id FROM data dt1 INNER JOIN mimetypes ON mimetype_id = mimetypes._id WHERE mimetype LIKE "%name%" AND length(data1) > 0 AND…

Read More

Remove Directory

Posted on October 12, 2015October 12, 2015

rd c:\temp\directorytoremove Now if this first command doesn’t work make sure it’s a directory you want to delete and try this next one rd /s c:\temp\directorytoremove 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)…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes