Monday, January 11, 2016

Sharepoint web.config backup using powershell

Here is the PowerShell that i use in my SharePoint farm to backup PowerShell on WFE. I have added the script as scheduled task on windows WFE server in the farm.

The PowerShell goes as below.

#Developed by Hemant Basavapattan for web.config Backup

Add-PSSnapin -Name Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue
$webappName = “YourWebappName”
$backupDir = “D:\Backups\Web_Config_Backup”
Write-Host "Backing up $webappName web.config file…" -foreground Gray –nonewline
## Get the web application – by display name
$w = Get-SPWebApplication | where { $_.DisplayName -eq "$webappName"}
## Get the default (first) zone for the web app…
## You may wish to iterate through all the available zones
for($i=0; $i -lt $w.AlternateUrls.Count; $i++)
 $zone = $w.AlternateUrls[$i].UrlZone
 ## Get the collection of IIS settings for the zone
 $iisSettings = $w.IisSettings[$zone]
 ## Get the path from the settings
 $path = $iisSettings.Path.ToString() + "\web.config"
 ## copy the web.config file from the path
 $a = (Get-Date).ToString('MM-dd-yyyy')
 New-Item -ItemType Directory -Force -Path $backupDir\$a\$url
 copy-item $path -destination $backupDir\$a\$url
Write-Host "done" -foreground Green

I have modified Brian's post to suit my requirements of multi-authentication extended WebApp's