Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .Synopsis
- Automated backup of the SharePoint Farm.
- .Description
- Automates the backup of the SharePoint Farm backing up the Farm, Farm Config, Service Applications, and individual Site Collections.
- .Parameter Path ("RootPath")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Days
- Global. Number of days to maintain all backups. Over-ridden by ConfigDays, FarmDays, SitesDays, & SADays for their respective backups If they exist. Default: 7.
- .Parameter From ("Sender")
- Global. Sender email address for all email notifications.
- .Parameter To ("Recipients")
- Global. Recipient(s) email address(es) for all email notifications.
- .Parameter SMTPHost ("SMTP")d
- Global. SMTP hostname or IP address.
- .Parameter SubjectPrefix ("Subject")
- Global. Subject prefix of the email subject. Default: "[SP2016 Backup] "
- .Parameter SPConfigFolderName ("SPConfigFolder")
- Folder name of the SP Config backups to be appended to the Root Path. Default: "Config".
- .Parameter ConfigDays
- Placeholder. This is currently not in use. Number of days to maintain Config backups. Default (from Days): 7.
- .Parameter FarmFolderName ("FarmFolder")
- Folder name of the Farm backups to be appended to the Root Path. Default: "Farm".
- .Parameter FarmDays
- Number of days to maintain Farm backups. Default (from Days): 7.
- .Parameter SPSitesFolderName ("SPSitesFolder")
- Folder name of the Site Collection backups to be appended to the Root Path. Default: "Sites".
- .Parameter SitesDays
- Number of days to maintain Site Collection backups. Default (from Days): 7.
- .Parameter SAFolderName ("SAFolder")
- Folder name of the Service Application backups to be appended to the Root Path. Default: "ServiceApplications".
- .Parameter SADays
- Placeholder. This is currently not in use. Number of days to maintain Service Application backups. Default (from Days): 7.
- .Example
- Backup-SP2016 -Path "\\servername-nms\Sharepoint Backup"
- -Days 30
- -From "no-reply@servername.com"
- -To "your_email_address@servername.com"
- -SMTP "SHAREPOINTAPP"
- .Notes
- Name: Backup-SP2016
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Backup-SP2016 {
- [CmdletBinding()]
- param(
- ## GLOBAL PARAMETERS ##
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("RootPath")]
- [System.String]
- $Path,
- # Over-ridden by FarmDays & SitesDays If they exist
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Sender")]
- [System.String]
- $From,
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Recipients")]
- [System.String]
- $To,
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SMTP")]
- [System.String]
- $SMTPHost,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Subject")]
- [System.String]
- $SubjectPrefix = "[SP2016 Backup] ",
- ## SPECIFIC PARAMETERS ##
- # Folder Names
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SPConfigFolder")]
- [System.String]
- $SPConfigFolderName = "Config",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $ConfigDays,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("FarmFolder")]
- [System.String]
- $FarmFolderName = "Farm",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $FarmDays,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SPSitesFolder")]
- [System.String]
- $SPSitesFolderName = "Sites",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $SitesDays,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SAFolder")]
- [System.String]
- $SAFolderName = "ServiceApplications",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $SADays
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2016Verbose "Root" "Starting" "Beginning backups..."
- # Create the Root Path
- #Create-SP2016BackupDirectory $Path
- # Set the Sites/Farm Days Retention
- If (!$ConfigDays) {
- $ConfigDays = $Days
- }
- Write-SP2016Verbose "Root" "Starting" "Setting the Farm Backups Retention to $ConfigDays Days"
- If (!$FarmDays) {
- $FarmDays = $Days
- }
- Write-SP2016Verbose "Root" "Starting" "Setting the Farm Backups Retention to $FarmDays Days"
- If (!$SitesDays) {
- $SitesDays = $Days
- }
- Write-SP2016Verbose "Root" "Starting" "Setting the Site Collection Backups Retention to $FarmDays Days"
- If (!$SADays) {
- $SADays = $Days
- }
- Write-SP2016Verbose "Root" "Starting" "Setting the Service Application Backups Retention to $FarmDays Days"
- # Set Path Global Variables
- Set-Variable -Name "SP2016Path" -Value $Path -Scope Global
- # Set Email Global Variables
- Set-SP2016EmailParameters $From $To $SMTPHost $SubjectPrefix
- }
- PROCESS {
- # Process Config Backup
- Backup-SP2016Config -Path $Path -Name $SPConfigFolderName -Days $ConfigDays -Verbose:$Verbose
- # Process Sites Backup
- #Backup-SP2016Sites -Path $Path -Name $SPSitesFolderName -Days $SitesDays -Verbose:$Verbose
- # Process Farm Backup
- Backup-SP2016Farm -Path $Path -Name $FarmFolderName -Days $FarmDays -Verbose:$Verbose -SendEmail
- # Process Service Applications Backup
- #Backup-SP2016ServiceApplications -Path $FarmFolderName -Name $SAFolderName -Days $SADays -Verbose:$Verbose
- }
- END {
- Write-SP2016Verbose "Root" "Ending" "Backups Complete"
- Exit
- }
- }
- <#
- .Synopsis
- Automated backup of the SharePoint Farm Config.
- .Description
- Automates the backup of the SharePoint Farm Config.
- .Parameter Path ("RootPath")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Name ("DirectoryName")
- Folder name of the SP Config backups to be appended to the Root Path. Default: "Config".
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Backup-SP2016Config -Path "\\servername\path\to\backups\root"
- .Notes
- Name: Backup-SP2016
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Backup-SP2016Config {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("DirectoryName")]
- [System.String]
- $Name = "Config",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2016Verbose "Config" "Starting" "Beginning SP Config Backup"
- # Set SP Config Backup Directory Path
- If ($Name)
- {
- $ConfigPath = $Path.Trimend('\') + "\$Name"
- }
- else
- {
- $ConfigPath = $Path
- }
- }
- PROCESS {
- Invoke-SP2016BackupProcess -Path $ConfigPath -Name "Config" -Days $Days -SendEmail:$SendEmail -Verbose:$Verbose
- }
- }
- <#
- .Synopsis
- Automated backup of the SharePoint Farm Config.
- .Description
- Automates the backup of the SharePoint Farm Config.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Namespace ("Name")
- Folder name of the SP Config backups to be appended to the Root Path.
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Parameter BackupMethod
- Method of Backup. Default: "Full".
- .Parameter OnError
- Error Action over-ride for ErrorActionPreference. Default: "Stop".
- .Parameter Percentage
- Percentage to expect a message. Default: 15.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Backup-SP2016Config -Path "\\servername\path\to\backups\root"
- .Notes
- Name: Invoke-SP2016BackupProcess
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Invoke-SP2016BackupProcess {
- [CmdletBinding()]
- param(
- # Expects Root Path
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Name")]
- [System.String]
- $Namespace,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Method")]
- [System.String]
- $BackupMethod = "Full",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.String]
- $OnError = "Stop",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Percent")]
- [System.String]
- $Percentage = 15,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2016Verbose $Namespace "Processing" "Starting Backup"
- Create-SP2016BackupDirectory $Path
- $BackupPath = Get-SP2016Directory $Path
- Write-SP2016Verbose $Namespace "Processing" "Backing up SP2016 Farm to $BackupPath"
- }
- PROCESS {
- # Perform new backup
- Invoke-SP2016Backup -Path $BackupPath -Name $Namespace -SendEmail:$SendEmail -Verbose:$Verbose
- # Clean up Old Backups
- Remove-SP2016OldBackups -Path $BackupPath -Name $Namespace -Days $Days -Verbose:$Verbose
- # Check for Backup Completion!
- Watch-2016BackupStatus -Path $Path -Name $Namespace -Verbose:$Verbose
- Write-SP2016Verbose $Namespace "Processing" "Monitoring Complete!" -ForegroundColor "Green"
- }
- END {
- If ($SendEmail -and $SP2016From -and $SP2016To -and $SP2016SMTPHost)
- {
- # The backup status is saved in the last 4 log lines from the file. Save that information off
- $eBody += (Get-Content (Get-SP2016BackupLogFile $Path)) | Select-Object -First 1 -Last 4 #(Get-SP2016BackupLogFile $Path -Contents)[-2 .. -4]
- # Send Email Status
- Write-SP2016Verbose $Namespace "Emailing" "Sending the email!"
- Send-SP2016BackupEmail -From $SP2016From -To $SP2016To -Subject $SP2016SubjectPrefix -Body $eBody -SMTP $SP2016SMTPHost -AttachmentName (Get-SP2016BackupLogFile $Path) #-Versbose:$Verbose
- }
- Else
- {
- Write-SP2016Verbose $Namespace "Emailing" "Cannot send email. Please run Set-SP2016EmailParameters." -Error
- }
- }
- }
- <#
- .Synopsis
- Automated backup of the SharePoint Farm.
- .Description
- Automates the backup of the SharePoint Farm.
- .Parameter Path ("RootPath")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Name ("DirectoryName")
- Folder name of the SP Config backups to be appended to the Root Path. Default: "Farm".
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Backup-SP2016Farm -Path "\\servername\path\to\backups\root" -SendEmail
- .Notes
- Name: Backup-SP2016Farm
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Backup-SP2016Farm {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("DirectoryName")]
- [System.String]
- $Name = "Farm",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2016Verbose "Farm" "Starting" "Beginning SP Farm Backup Beginning..."
- # Set SP Farm Backup Directory Path
- If ($Name) {
- $FarmPath = $Path.Trimend('\') + "\$Name"
- }
- Else
- {
- $FarmPath = $Path
- }
- }
- PROCESS {
- Invoke-SP2016BackupProcess -Path $FarmPath -Name "Farm" -Days $Days -SendEmail:$SendEmail -Verbose:$Verbose
- }
- }
- <#
- .Synopsis
- Automated backup of the SharePoint Service Applications.
- .Description
- Automates the backup of the SharePoint Service Applications.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Name ("DirectoryName")
- Folder name of the SP Config backups to be appended to the Root Path. Default: "Farm".
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Backup-SP2016ServiceApplications -Path "\\servername\path\to\backups\root" -SendEmail
- .Notes
- Name: Backup-SP2016ServiceApplications
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Backup-SP2016ServiceApplications {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("DirectoryName")]
- [System.String]
- $Name = "ServiceApplications",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2016Verbose "ServiceApplications" "Starting" "Beginning SP Service Applications Backup"
- # Set SP Service Applications Backup Directory Path
- If ($Name)
- {
- $SAPath = $Path.Trimend('\') + "\$Name"
- }
- else
- {
- $SAPath = $Path
- }
- }
- PROCESS {
- Invoke-SP2016BackupProcess -Path $SAPath -Name "ServiceApplications" -Days $Days -SendEmail:$SendEmail -Verbose:$Verbose
- }
- }
- <#
- .Synopsis
- Automated backup of the SharePoint Service Applications.
- .Description
- Automates the backup of the SharePoint Service Applications.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Name ("DirectoryName")
- Folder name of the SP Config backups to be appended to the Root Path. Default: "Farm".
- .Parameter Sites
- Array of sites to be backed up. Default: Get-SPSite -Limit All.
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Backup-SP2016Sites -Path "\\servername\path\to\backups\root" -SendEmail
- .Notes
- Name: Backup-SP2016Sites
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Backup-SP2016Sites {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("DirectoryName")]
- [System.String]
- $Name = "Sites",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Collections.ArrayList]
- $Sites,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2016Verbose "Sites" "Starting" "Backing up SP2016 Site Collections"
- # Get sites
- If (!$Sites) {
- Write-SP2016Verbose "Sites" "Starting" "Getting ALL sites!"
- $Sites = Get-SPSite -Limit All
- }
- Write-SP2016Verbose "Sites" "Starting" "Sorting sites from largest to smallest by DiskSizeRequired"
- $Sites = $Sites | Sort-Object ContentDatabase.DiskSizeRequired
- If ($Name) {
- $SitesPath = $Path.Trimend('\') + "\$Name"
- }
- Create-SP2016BackupDirectory $SitesPath
- $BackupPath = Get-SP2016Directory $SitesPath
- Write-SP2016Verbose "Sites" "Starting" "Backing up SP2016 Farm to $SitesPath"
- }
- PROCESS {
- foreach ($site in $Sites)
- {
- $BackupPath = $site.PrimaryUri.Host
- If ($site.PrimaryUri.Segments.Length -gt 1)
- {
- $BackupPath += "." + $site.PrimaryUri.Segments[1].TrimEnd("/")
- If ($site.PrimaryUri.Segments.Length -gt 2)
- {
- $BackupPath += "." + $site.PrimaryUri.Segments[2]
- }
- }
- Write-SP2016Verbose "Sites" "Processing" "Backing up $($site.Url) to $BackupPath\$BackupPath.bak"
- # Take the site backup without locking the site (-NoSiteLock) and using SQL snapshot
- Backup-SPSite -Identity $site.id -Path "$BackupPath\$BackupPath.bak" -NoSiteLock -UseSqlSnapshot -Verbose:$Verbose
- }
- }
- END {
- Write-SP2016Verbose "Sites" "Ending" "Cleaning old Site Collection Backups"
- Remove-SP2016OldSites -Path $SitesPath - Days $Days
- }
- }
- <#
- .Synopsis
- Automated cleanup of the SharePoint Site Collections.
- .Description
- Automated cleanup of the SharePoint Site Collections.
- .Parameter Path ("Directory")
- The path to the Sites backups directory (e.g. \\servername\path\to\backups\root\Sites).
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Example
- Remove-SP2016OldSites -Path "\\servername\path\to\backups\root\Sites" -SendEmail
- .Notes
- Name: Remove-SP2016OldSites
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Remove-SP2016OldSites {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7
- )
- BEGIN {
- Write-SP2016Verbose "Sites - Clean" "Starting" "Cleaning up SP2016 Site Collection Backups"
- # Get/Set path
- If (!$Path -and $SP2016Path)
- {
- $Path = $SP2016Path
- }
- }
- PROCESS {
- # Remove old backup directories
- $old = gci $Path | ? { $_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-$Days) }
- # Check If old directories exist
- If ($old -eq $null)
- {
- # Do Nothing
- break
- }
- # Remove Old Directories
- If ($Verbose)
- {
- $old | % { Write-SP2016Verbose "Sites - Clean" "Processing" "Removing " + $_.FullName }
- }
- $old | % { Remove-Item $_.FullName -Recurse }
- }
- }
- <#
- .Synopsis
- Automated cleanup of the SharePoint Backups (Farm, Config, & Service Applications).
- .Description
- Automated cleanup of the SharePoint Backups (Farm, Config, & Service Applications; NOT Sites).
- .Parameter Path ("Directory")
- The full path to the specific backups directory (e.g. \\servername\path\to\backups\root\Farm\{date.time}).
- .Parameter Namespace ("Name")
- Folder name of the SP Config backups to be appended to the Root Path.
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Example
- Remove-SP2016OldBackups -Path "\\servername\path\to\backups\root\Farm\{date.time}" -Name "Farm"
- .Notes
- Name: Remove-SP2016OldBackups
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Remove-SP2016OldBackups {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Name")]
- [System.String]
- $Namespace,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7
- )
- BEGIN {
- Write-SP2016Verbose $Namespace "Cleaning" "Beginning to clean up old Farm backups"
- }
- PROCESS {
- Write-SP2016Verbose $Namespace "Cleaning" "Getting backup folders"
- $files = Get-ChildItem -Path ((get-item $path).parent.FullName) | ?{ $_.PSIsContainer }
- if ($files.Count -gt $Days) {
- $files | Sort-Object CreationTime | Select-Object -First ($files.Count - $Days) | Remove-Item -Force -Recurse
- }
- }
- }
- # REGION: Helper Functions
- <#
- .Synopsis
- Automated backup of the SharePoint Farm Config.
- .Description
- Automates the backup of the SharePoint Farm Config.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Namespace ("Name")
- Folder name of the SP Config backups to be appended to the Root Path.
- .Parameter BackupMethod
- Method of Backup. Default: "Full".
- .Parameter OnError
- Error Action over-ride for ErrorActionPreference. Default: "Stop".
- .Parameter Percentage
- Percentage to expect a message. Default: 15.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Invoke-SP2016Backup -Path "\\servername\path\to\backups\root"
- .Notes
- Name: Invoke-SP2016Backup
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Invoke-SP2016Backup {
- [CmdletBinding()]
- param(
- # Expects full Backup Path
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Name")]
- [System.String]
- $Namespace,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Method")]
- [System.String]
- $BackupMethod = "Full",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.String]
- $OnError = "Stop",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Percent")]
- [System.String]
- $Percentage = 15,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- PROCESS {
- # Perform the backup
- Try
- {
- Write-SP2016Verbose $Namespace "Processing" "Beginning backup..."
- # Run a new backup
- Switch ($Namespace)
- {
- "Config" {
- Write-Verbose "Doing Config Backup"
- # Run a new full configuration-only backup
- Backup-SPFarm -Directory $Path -BackupMethod $BackupMethod -ConfigurationOnly -ErrorAction $OnError -Verbose:$Verbose -Percentage $Percentage
- }
- "Farm" {
- Write-Verbose "Doing Farm Backup"
- # Run a new full farm backup
- Backup-SPFarm -Directory $Path -BackupMethod $BackupMethod -ErrorAction $OnError -Verbose:$Verbose -Percentage $Percentage
- }
- "ServiceApplications" {
- Write-Verbose "Doing SA Backup"
- $Path
- $BackupMethod
- $OnError
- $Percentage
- Backup-SPFarm -Directory $Path -BackupMethod $BackupMethod -Item "Farm\Shared Services" -ErrorAction $OnError -Verbose:$Verbose -Percentage $Percentage
- }
- }
- $eSubject += " Backup Completed Successfully"
- }
- Catch [system.exception] # check for exceptions
- {
- Write-SP2016Verbose $Namespace "Processing" "Backup Failed!" -Error
- Write-SP2016Verbose $Namespace "Processing" $_.Exception.Message -Error
- If ($SendEmail)
- {
- # save off the exception message
- $eBody = $_.Exception.Message
- # new email subject
- $eSubject += "Backup Failed"
- # send an email containing the backup failure
- Send-SP2016BackupEmail $From $To $eSubject $eBody $SMTPHost -Versbose:$Verbose
- }
- # halt the script so we preserve older backups
- break
- }
- }
- }
- <#
- .Synopsis
- Creates the Backup Directory.
- .Description
- Creates the Backup Directory from the Path (Backups Root Path) & Name (Specific Backup Name) or the specific Path.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups\root\Config).
- .Parameter Namespace ("Name")
- Folder name of the SP Config backups to be appended to the Root Path.
- .Example
- Create-SP2016BackupDirectory -Path "\\servername\path\to\backups\root" -Name "Config"
- Create-SP2016BackupDirectory -Path "\\servername\path\to\backups\root\Config"
- .Notes
- Name: Create-SP2016BackupDirectory
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Create-SP2016BackupDirectory {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("DirectoryName")]
- [System.String]
- $Name
- )
- BEGIN {
- $today = Get-SP2016Date
- }
- PROCESS {
- If ($Name)
- {
- Write-Verbose "Creating Directory $Path\$Name\$today"
- New-Item $Path\$Name\$today -Type directory
- }
- Else
- {
- Write-Verbose "Creating Directory $Path\$today"
- New-Item $Path\$today -Type directory
- }
- }
- }
- <#
- .Synopsis
- Gets the current date and caches it to a global variable, $SP2016Today.
- .Description
- Gets the current date and caches it to a global variable, $SP2016Today.
- .Parameter Format
- Format of the Date. Default: "yyyyMMdd.HHmmss".
- .Example
- Get-SP2016Date
- .Notes
- Name: Get-SP2016Date
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Get-SP2016Date {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.String]
- $Format = "yyyyMMdd.HHmmss"
- )
- If ($SP2016Today)
- {
- Return $SP2016Today
- }
- $today = Get-Date -Format $Format
- Set-Variable -Name "SP2016Today" -Value $today -Scope Global
- Return $today
- }
- <#
- .Synopsis
- Creates the Backup Directory.
- .Description
- Creates the Backup Directory from the Path (Backups Root Path) & Name (Specific Backup Name) or the specific Path.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups\root\Config).
- .Example
- Get-SP2016Directory -Path "\\servername\path\to\backups\root\Config"
- .Notes
- Name: Get-SP2016Directory
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Get-SP2016Directory {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $false,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path
- )
- Write-Verbose "Path: $Path"
- Write-Verbose ("Returning: $Path" + "\" + (Get-SP2016Date))
- Return $Path.Trimend('\') + "\" + (Get-SP2016Date)
- }
- <#
- .Synopsis
- Watches the current backup for completion and completion status.
- .Description
- Watches the current backup for completion and completion status.
- .Parameter Path ("Directory")
- The path to the specific root (FarmPath, ConfigPath, SitesPath, SAPath) backup directory (e.g. \\servername\path\to\backups\root\Config).
- .Parameter Namespace ("Name")
- Folder name of the SP Config backups to be appended to the Root Path.
- .Example
- Watch-2016BackupStatus -Path "\\servername\path\to\backups\root\Config" -Name "Config"
- .Notes
- Name: Watch-2016BackupStatus
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Watch-2016BackupStatus {
- [CmdletBinding()]
- param(
- # Expects
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Name")]
- [System.String]
- $Namespace
- )
- BEGIN {
- ## Check backup progress and rip status when complete ##
- Write-SP2016Verbose $Namespace "Processing" "Monitoring backup..."
- $start = Get-Date
- # wait 15s for backup to initialize and restore log to be created
- Sleep 15
- $time = New-TimeSpan $start (Get-Date)
- }
- PROCESS {
- do
- {
- $time = New-TimeSpan $start (Get-Date)
- #Write-SP2016Verbose $Namespace "Processing" "Still monitoring. It's only been " + $time.TotalSeconds + "s..."
- $backupPath = Get-SP2016BackupLogFile $Path -Contents
- # Check for line at the end of the backup script
- $backupStatusSuccess = Select-String -Path $backupPath -Pattern "Backup completed successfully." #-Quiet
- # Check Success Result and check for Fail If nothing is there.
- If ($backupStatusSuccess -eq $null)
- {
- $backupStatusFail = Select-String -Path $backupPath -Pattern "Backup failed" #-Quiet
- If ($backupStatusSuccess -ne $null)
- {
- Write-SP2016Verbose $Namespace "Processing" "FatalError: Backup failed. " + (Get-SP2016BackupLogFile $Path -Contents) -Error
- }
- }
- Else
- {
- Write-SP2016Verbose $Namespace "Processing" "Backup Completed Successfully." -ForegroundColor "Green"
- }
- # Run Loop while both Backup Statuses are empty
- } while ($backupStatusSuccess -eq $null -and $backupStatusFail -eq $null)
- }
- }
- <#
- .Synopsis
- Gets the backup log file from farm backups or partial farm backups (Config/Service Applications).
- .Description
- Gets the backup log file from farm backups or partial farm backups (Config/Service Applications).
- .Parameter Path ("Directory")
- The path to the specific root (FarmPath, ConfigPath, SitesPath, SAPath) backup directory (e.g. \\servername\path\to\backups\root\Config).
- .Parameter Content ("GetContents")
- Switch whether to return the file's contents. Default: $false.
- .Example
- Get-SP2016BackupLogFile -Path "\\servername\path\to\backups\root\Config" -Contents
- .Notes
- Name: Get-SP2016BackupLogFile
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Get-SP2016BackupLogFile {
- [CmdletBinding()]
- param(
- # Expects FarmPath, ConfigPath, SitesPath
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("GetContents")]
- [Switch]
- $Contents
- )
- PROCESS {
- # find last backup directory
- $lastBackupDir = gci $Path | ? { $_.PSIsContainer } | sort -prop LastWriteTime | select -last 1
- # grab the backup log file from that directory
- $backupLogFile = $lastBackupDir.FullName + "\spbr0000\spbackup.log"
- If((Test-Path $backupLogFile) -And (Test-Path $Contents))
- {
- Return gc $backupLogFile
- }
- ElseIf(Test-Path $backupLogFile)
- {
- Return $backupLogFile
- }
- Write-SP2016Verbose "Get-SP2016BackupLogFile" "Ending" "Backup Log File ($backupLogFile) does not exist." -Error
- Return
- }
- }
- <#
- .Synopsis
- Sets global variables for the email operations.
- .Description
- Sets global variables for the email operations: From ($SP2016From), To ($SP2016To), SMTP ($SP2016SMTPHost), SubjectPrefix ($SP2016SubjectPrefix)
- .Parameter From ("Sender")
- Global. Sender email address for all email notifications.
- .Parameter To ("Recipients")
- Global. Recipient(s) email address(es) for all email notifications.
- .Parameter SMTPHost ("SMTP")
- Global. SMTP hostname or IP address.
- .Parameter SubjectPrefix ("Subject")
- Global. Subject prefix of the email subject. Default: "[SP2016 Backup] ".
- .Example
- Set-SP2016EmailParameters -From "no-reply@domain.com" -To "name@domain.com" -SMTP "mail.domain.com"
- .Notes
- Name: Set-SP2016EmailParameters
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Set-SP2016EmailParameters {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Sender")]
- [System.String]
- $From,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Recipients")]
- [System.String]
- $To,
- [Parameter(
- Mandatory = $true,
- Position = 2,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SMTP")]
- [System.String]
- $SMTPHost,
- [Parameter(
- Mandatory = $false,
- Position = 3,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Subject")]
- [System.String]
- $SubjectPrefix = "[SP2016 Backup] "
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2016Verbose "Email" "Starting" "Beginning to set Email Global Variables..."
- }
- PROCESS {
- Set-Variable -Name "SP2016From" -Value $From -Scope Global
- Set-Variable -Name "SP2016To" -Value $To -Scope Global
- Set-Variable -Name "SP2016SMTPHost" -Value $SMTPHost -Scope Global
- Set-Variable -Name "SP2016SubjectPrefix" -Value $SubjectPrefix -Scope Global
- }
- END {
- Write-SP2016Verbose "Email" "Ending" "Email Global Variables Set."
- }
- }
- <#
- .Synopsis
- Sends the email.
- .Description
- Sends the email.
- .Parameter From ("Sender")
- Sender email address.
- .Parameter To ("Recipients")
- Recipient(s) email address(es).
- .Parameter Subject
- Subject of the email.
- .Parameter Body ("Message")
- Body of the email.
- .Parameter SMTPHost ("SMTP")
- SMTP hostname or IP address.
- .Parameter AttachmentName ("Attachment")
- Attachment Name.
- .Example
- Set-SP2016EmailParameters -From "no-reply@domain.com" -To "name@domain.com" -SMTP "mail.domain.com"
- .ToDo
- Use an array for Send-Message
- @{
- Subject = "Backup Failed: Farm Configuration Database"
- Body = "ERROR $_."
- From = $FromAddress
- To = $AdminEmail
- SmtpServer = $MailServer
- }
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Mail")]
- [System.Collections.ArrayList]
- $Mail
- .Notes
- Name: Send-SP2016BackupEmail
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Send-SP2016BackupEmail {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Sender")]
- [System.String]
- $From,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Recipients")]
- [System.String]
- $To,
- [Parameter(
- Mandatory = $true,
- Position = 2,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.String]
- $Subject,
- [Parameter(
- Mandatory = $true,
- Position = 3,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Message")]
- [System.String[]]
- $Body,
- [Parameter(
- Mandatory = $true,
- Position = 4,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SMTP")]
- [System.String]
- $SMTPHost,
- [Parameter(
- Mandatory = $false,
- Position = 5,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Attachment")]
- [System.String]
- $AttachmentName
- )
- PROCESS {
- # check for an attachment (successful backup)
- If ($AttachmentName)
- {
- # send email with attachment
- Send-MailMessage -from $from -to $to -Subject $subject -body ($body -join "`r`n") -SmtpServer $SMTPHost -Attachments $AttachmentName -Verbose:$Verbose
- }
- else
- {
- # send email sans attachment
- Send-MailMessage -from $from -to $to -Subject $subject -body ($body -join "`r`n") -SmtpServer $SMTPHost -Verbose:$Verbose
- }
- }
- }
- <#
- .Synopsis
- Write function for all write operations to format the output.
- .Description
- Write function for all write operations to format the output.
- .Parameter Namespace ("Name")
- Name of the current operation being run (e.g., "Farm", "Config", "Sites", "ServiceApplications")
- .Parameter Step ("Stage")
- Current stage of the operation (e.g., "Starting", "Processing", "Ending").
- .Parameter Message ("Msg")
- Message to be written to the screen.
- .Parameter ForegroundColor ("Color")
- Color to write the message on the screen.
- .Parameter WriteHost ("Host")
- Switch whether to write to host. Default: $false ($true if $ForegroundColor exists).
- .Parameter WriteWarning ("Warning")
- Switch whether to write a warning to host. Default: $false.
- .Parameter WriteError ("Error")
- Switch whether to write an error to host. Default: $false.
- .Example
- Get-SP2016BackupLogFile -Path "\\servername\path\to\backups\root\Config" -Contents
- .Notes
- Name: Write-SP2016Verbose
- Author: Travis Smith
- Author: Tim Hansen
- Author: Ali Nooshabadi
- LastEdit: 05/09/2018
- #>
- Function Write-SP2016Verbose {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Name")]
- [System.String]
- $Namespace,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Stage")]
- [System.String]
- $Step,
- [Parameter(
- Mandatory = $true,
- Position = 2,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Msg")]
- [System.String]
- $Message,
- [Parameter(
- Mandatory = $false,
- Position = 3,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Color")]
- [System.String]
- $ForegroundColor,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Host")]
- [Switch]
- $WriteHost,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Warning")]
- [Switch]
- $WriteWarning,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Error")]
- [Switch]
- $WriteError
- )
- BEGIN {
- $Step = $Step.ToUpper();
- $m = [string]::Format("[{0}] {1}: {2}", $Namespace, $Step, $Message)
- }
- PROCESS {
- If ($WriteWarning)
- {
- Write-Warning $m
- }
- ElseIf ($WriteError)
- {
- Write-Warning $m
- }
- ElseIf ($WriteHost -or $ForegroundColor)
- {
- Write-Host $m -ForegroundColor $ForegroundColor
- }
- Else
- {
- Write-Verbose $m
- }
- }
- }
Add Comment
Please, Sign In to add comment