Skip to content

Instantly share code, notes, and snippets.

@spy86
Created August 3, 2017 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spy86/0623e154059dd3428d1ac2c48aa4a12e to your computer and use it in GitHub Desktop.
Save spy86/0623e154059dd3428d1ac2c48aa4a12e to your computer and use it in GitHub Desktop.
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment