The IT as a Utility

The IT infrastructure world has a history divided into:
<> The 90s for Networking
<> The 00s for Virtualization
<> The 10s for Cloud

So, if you still have not thought of cloud. you just living a life with trust issues and outdated technology.

PowerShell script to file browse, change file attribute, restore SharePoint site backup

Here is a nice little utility (written using PowerShell script) which I use to restore a site backup time to time.

Some features of this script are:

1. Displays a file browse dialog to select a file (see – Invoke-FileBrowser)

2. Removes the read-only attribute on the file (see – Remove-Readonly), this is required as the backup file is stored in TFS.

3. Restore a given backup file, without asking for confirmation (see – Restore-SPBackup)

——————————————————————————————————————————

$ErrorActionPreference = "Stop"

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0

# Example:
# $file = Invoke-FileBrowser -Title "Select a file" -Directory "D:\backups" -Filter "Powershell Scripts|(*.ps1)"
function Invoke-FileBrowser
{
      param([string]$Title,[string]$Directory,[string]$Filter="All Files (*.*)|*.*")
      [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
      $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
      $FileBrowser.InitialDirectory = $Directory
      $FileBrowser.Filter = $Filter
      $FileBrowser.Title = $Title
      $Show = $FileBrowser.ShowDialog()
      If ($Show -eq "OK")
      {
            Return $FileBrowser.FileName
      }
      Else
      {
            Write-Error "Restore cancelled by user."
      }
}

# Example:
# Remove-Readonly -FilePath "D:\backups\filename.txt"
function Remove-Readonly
{
    param([string]$FilePath)
    #Remove read-only attribute, otherwise access denied error.
    Set-ItemProperty -Path $FilePath -name IsReadOnly -value $false
}

# Example:
# Restore-SPBackup -FilePath "D:\backups\backupfile.bak"
function Restore-SPBackup
{
    param([string]$BackupFilePath, [string]$WebUrl)
    #Restore the backup without asking for confirmation.
    Restore-SPSite -Identity $WebUrl -Path $BackupFilePath -Confirm:$false
}

$backupLocation = "C:\mywork\scm\JRCP.Internet\JRCP.Internet\JRCP Data Structures\Site Backups"
$SiteUrl = "http://sp2010riyaz:4040"

$file = Invoke-FileBrowser -Title "Browse" -Directory $backupLocation -Filter "All Files (*.*)|*.*"
Remove-Readonly -FilePath $file
Restore-SPBackup -BackupFilePath $file -WebUrl $SiteUrl
--------------------------------------------------------------------

Wait for more useful utilities…