Backup-WSPSharePoint.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID e7bf25fd-2daa-45d8-8255-35e7db6b9e79
 
.AUTHOR MohamedEl-Qassas
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Backup all SharePoint WSP Solutions with a versioning Using PowerShell
 
#>
 
Param()

function BackupWSPSolutions()
    {
    Set-ExecutionPolicy "Unrestricted"
    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop
    Try { 
        #Backup all wps solutions
        $WSPCount = (Get-SPSolution).count
        #Check if there are deployed solutions
        if($WSPCount -eq 0)
            {
                Write-Output "There are no solutions in the solution store" -ForegroundColor Red
            }
        else
            {
                #Create a new folder with the current date and time
                $RootFolderPath = "c:\WSPSolutions-Backup"
                Write-Output "Backup all WPS SharePoint Solutions("$WSPCount ") to "$RootFolderPath -ForegroundColor Green
                Get-SPSolution | Select-Object Name, Deployed | Format-Table
                Write-Output "--------------------------------------------------------------"
                $reply = Read-Host -Prompt "Are you sure you would like to backup all wsp solutions to $RootFolderPath ?[y/n]" 
                
                if ( $reply -match "[yY]" ) { 
                  
                    #create latest backup folder
                    $RootFolderPathLatest = "$RootFolderPath\01-Latest-WSPBackup"
                    if(-not(Test-path $RootFolderPathLatest))
                    {
                        New-Item -ItemType Directory -Path $RootFolderPathLatest
                    }
                    
                    # create log file
                    $LogFilePath = "$RootFolderPath\Log.txt"
                    if(-not(Test-path $LogFilePath -PathType Leaf))
                    {
                        New-Item -ItemType File -Path $LogFilePath
                    }
                    
                    foreach ($solution in Get-SPSolution)
                     {
                      $WSPname = $Solution.Name
                      $WSPFolderPath = "$RootFolderPath\$WSPname\$((Get-Date).ToString('yyyy-MM-dd-hh-mm-ss-tt'))"
                      New-Item -ItemType Directory -Path $WSPFolderPath
                      #save versioning
                      $solution.SolutionFile.SaveAs("$WSPFolderPath\$WSPname")                              
                      $solution.SolutionFile.SaveAs("$RootFolderPathLatest\$WSPname")
                      
                     }
                    
                    Write-Output "All WPS SharePoint Solutions("$WSPCount ") have been backuped to "$RootFolderPath -ForegroundColor Cyan
                    $userName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
                    Add-Content $LogFilePath "`nWSP Count: $WSPCount`t | Done by: $userName`t | Backup date: $((Get-Date))"
                    # Open the beackup folder
                    Invoke-Item -Path $RootFolderPath
                    }
            }
        }
    Catch
        {
            Write-Output $_.Exception.Message -ForegroundColor Red
            break
        }
  }


#run the bacpup all SharePoint WSP solutions
BackupWSPSolutions