Guest User

Untitled

a guest
Sep 24th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 45.14 KB | None | 0 0
  1. <#
  2. .Synopsis
  3. Automated backup of the SharePoint Farm.
  4.  
  5. .Description
  6. Automates the backup of the SharePoint Farm backing up the Farm, Farm Config, Service Applications, and individual Site Collections.
  7.  
  8. .Parameter Path ("RootPath")
  9. The path to the root backups directory (e.g. \\servername\path\to\backups).
  10.  
  11. .Parameter Days
  12. Global. Number of days to maintain all backups. Over-ridden by ConfigDays, FarmDays, SitesDays, & SADays for their respective backups If they exist. Default: 7.
  13.  
  14. .Parameter From ("Sender")
  15. Global. Sender email address for all email notifications.
  16.  
  17. .Parameter To ("Recipients")
  18. Global. Recipient(s) email address(es) for all email notifications.
  19.  
  20. .Parameter SMTPHost ("SMTP")d
  21. Global. SMTP hostname or IP address.
  22.  
  23. .Parameter SubjectPrefix ("Subject")
  24. Global. Subject prefix of the email subject. Default: "[SP2016 Backup] "
  25.  
  26. .Parameter SPConfigFolderName ("SPConfigFolder")
  27. Folder name of the SP Config backups to be appended to the Root Path. Default: "Config".
  28.  
  29. .Parameter ConfigDays
  30. Placeholder. This is currently not in use. Number of days to maintain Config backups. Default (from Days): 7.
  31.  
  32. .Parameter FarmFolderName ("FarmFolder")
  33. Folder name of the Farm backups to be appended to the Root Path. Default: "Farm".
  34.  
  35. .Parameter FarmDays
  36. Number of days to maintain Farm backups. Default (from Days): 7.
  37.  
  38. .Parameter SPSitesFolderName ("SPSitesFolder")
  39. Folder name of the Site Collection backups to be appended to the Root Path. Default: "Sites".
  40.  
  41. .Parameter SitesDays
  42. Number of days to maintain Site Collection backups. Default (from Days): 7.
  43.  
  44. .Parameter SAFolderName ("SAFolder")
  45. Folder name of the Service Application backups to be appended to the Root Path. Default: "ServiceApplications".
  46.  
  47. .Parameter SADays
  48. Placeholder. This is currently not in use. Number of days to maintain Service Application backups. Default (from Days): 7.
  49.  
  50. .Example
  51. Backup-SP2016 -Path "\\servername-nms\Sharepoint Backup"
  52. -Days 30
  53. -From "no-reply@servername.com"
  54. -To "your_email_address@servername.com"
  55. -SMTP "SHAREPOINTAPP"
  56.  
  57. .Notes
  58. Name: Backup-SP2016
  59. Author: Travis Smith
  60. Author: Tim Hansen
  61. Author: Ali Nooshabadi
  62. LastEdit: 05/09/2018
  63.  
  64. #>
  65. Function Backup-SP2016 {
  66. [CmdletBinding()]
  67. param(
  68. ## GLOBAL PARAMETERS ##
  69. [Parameter(
  70. Mandatory = $true,
  71. ValueFromPipeline = $true,
  72. ValueFromPipelinebyPropertyName = $true)]
  73. [Alias("RootPath")]
  74. [System.String]
  75. $Path,
  76.  
  77. # Over-ridden by FarmDays & SitesDays If they exist
  78. [Parameter(
  79. Mandatory = $false,
  80. ValueFromPipeline = $true,
  81. ValueFromPipelinebyPropertyName = $true)]
  82. [System.Int32]
  83. $Days = 7,
  84.  
  85. [Parameter(
  86. Mandatory = $true,
  87. ValueFromPipeline = $true,
  88. ValueFromPipelinebyPropertyName = $true)]
  89. [Alias("Sender")]
  90. [System.String]
  91. $From,
  92.  
  93. [Parameter(
  94. Mandatory = $true,
  95. ValueFromPipeline = $true,
  96. ValueFromPipelinebyPropertyName = $true)]
  97. [Alias("Recipients")]
  98. [System.String]
  99. $To,
  100.  
  101. [Parameter(
  102. Mandatory = $true,
  103. ValueFromPipeline = $true,
  104. ValueFromPipelinebyPropertyName = $true)]
  105. [Alias("SMTP")]
  106. [System.String]
  107. $SMTPHost,
  108.  
  109. [Parameter(
  110. Mandatory = $false,
  111. ValueFromPipeline = $true,
  112. ValueFromPipelinebyPropertyName = $true)]
  113. [Alias("Subject")]
  114. [System.String]
  115. $SubjectPrefix = "[SP2016 Backup] ",
  116.  
  117. ## SPECIFIC PARAMETERS ##
  118.  
  119. # Folder Names
  120. [Parameter(
  121. Mandatory = $false,
  122. ValueFromPipeline = $true,
  123. ValueFromPipelinebyPropertyName = $true)]
  124. [Alias("SPConfigFolder")]
  125. [System.String]
  126. $SPConfigFolderName = "Config",
  127.  
  128. [Parameter(
  129. Mandatory = $false,
  130. ValueFromPipeline = $true,
  131. ValueFromPipelinebyPropertyName = $true)]
  132. [System.Int32]
  133. $ConfigDays,
  134.  
  135. [Parameter(
  136. Mandatory = $false,
  137. ValueFromPipeline = $true,
  138. ValueFromPipelinebyPropertyName = $true)]
  139. [Alias("FarmFolder")]
  140. [System.String]
  141. $FarmFolderName = "Farm",
  142.  
  143. [Parameter(
  144. Mandatory = $false,
  145. ValueFromPipeline = $true,
  146. ValueFromPipelinebyPropertyName = $true)]
  147. [System.Int32]
  148. $FarmDays,
  149.  
  150. [Parameter(
  151. Mandatory = $false,
  152. ValueFromPipeline = $true,
  153. ValueFromPipelinebyPropertyName = $true)]
  154. [Alias("SPSitesFolder")]
  155. [System.String]
  156. $SPSitesFolderName = "Sites",
  157.  
  158. [Parameter(
  159. Mandatory = $false,
  160. ValueFromPipeline = $true,
  161. ValueFromPipelinebyPropertyName = $true)]
  162. [System.Int32]
  163. $SitesDays,
  164.  
  165. [Parameter(
  166. Mandatory = $false,
  167. ValueFromPipeline = $true,
  168. ValueFromPipelinebyPropertyName = $true)]
  169. [Alias("SAFolder")]
  170. [System.String]
  171. $SAFolderName = "ServiceApplications",
  172.  
  173. [Parameter(
  174. Mandatory = $false,
  175. ValueFromPipeline = $true,
  176. ValueFromPipelinebyPropertyName = $true)]
  177. [System.Int32]
  178. $SADays
  179. )
  180. BEGIN {
  181. If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
  182. {
  183. Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
  184. }
  185. Write-SP2016Verbose "Root" "Starting" "Beginning backups..."
  186.  
  187. # Create the Root Path
  188. #Create-SP2016BackupDirectory $Path
  189.  
  190. # Set the Sites/Farm Days Retention
  191. If (!$ConfigDays) {
  192. $ConfigDays = $Days
  193. }
  194. Write-SP2016Verbose "Root" "Starting" "Setting the Farm Backups Retention to $ConfigDays Days"
  195.  
  196. If (!$FarmDays) {
  197. $FarmDays = $Days
  198. }
  199. Write-SP2016Verbose "Root" "Starting" "Setting the Farm Backups Retention to $FarmDays Days"
  200.  
  201. If (!$SitesDays) {
  202. $SitesDays = $Days
  203. }
  204. Write-SP2016Verbose "Root" "Starting" "Setting the Site Collection Backups Retention to $FarmDays Days"
  205.  
  206. If (!$SADays) {
  207. $SADays = $Days
  208. }
  209. Write-SP2016Verbose "Root" "Starting" "Setting the Service Application Backups Retention to $FarmDays Days"
  210.  
  211. # Set Path Global Variables
  212. Set-Variable -Name "SP2016Path" -Value $Path -Scope Global
  213.  
  214. # Set Email Global Variables
  215. Set-SP2016EmailParameters $From $To $SMTPHost $SubjectPrefix
  216. }
  217. PROCESS {
  218. # Process Config Backup
  219. Backup-SP2016Config -Path $Path -Name $SPConfigFolderName -Days $ConfigDays -Verbose:$Verbose
  220.  
  221. # Process Sites Backup
  222. #Backup-SP2016Sites -Path $Path -Name $SPSitesFolderName -Days $SitesDays -Verbose:$Verbose
  223.  
  224. # Process Farm Backup
  225. Backup-SP2016Farm -Path $Path -Name $FarmFolderName -Days $FarmDays -Verbose:$Verbose -SendEmail
  226.  
  227. # Process Service Applications Backup
  228. #Backup-SP2016ServiceApplications -Path $FarmFolderName -Name $SAFolderName -Days $SADays -Verbose:$Verbose
  229. }
  230. END {
  231. Write-SP2016Verbose "Root" "Ending" "Backups Complete"
  232. Exit
  233. }
  234. }
  235.  
  236. <#
  237. .Synopsis
  238. Automated backup of the SharePoint Farm Config.
  239.  
  240. .Description
  241. Automates the backup of the SharePoint Farm Config.
  242.  
  243. .Parameter Path ("RootPath")
  244. The path to the root backups directory (e.g. \\servername\path\to\backups).
  245.  
  246. .Parameter Name ("DirectoryName")
  247. Folder name of the SP Config backups to be appended to the Root Path. Default: "Config".
  248.  
  249. .Parameter Days
  250. Number of days to maintain all backups. Default: 7.
  251.  
  252. .Parameter SendEmail ("Email")
  253. Switch whether to send an email or not. Default: $false
  254.  
  255. .Example
  256. Backup-SP2016Config -Path "\\servername\path\to\backups\root"
  257.  
  258. .Notes
  259. Name: Backup-SP2016
  260. Author: Travis Smith
  261. Author: Tim Hansen
  262. Author: Ali Nooshabadi
  263. LastEdit: 05/09/2018
  264.  
  265. #>
  266. Function Backup-SP2016Config {
  267. [CmdletBinding()]
  268. param(
  269. [Parameter(
  270. Mandatory = $true,
  271. ValueFromPipeline = $true,
  272. ValueFromPipelinebyPropertyName = $true)]
  273. [Alias("Directory")]
  274. [System.String]
  275. $Path,
  276.  
  277. [Parameter(
  278. Mandatory = $false,
  279. ValueFromPipeline = $true,
  280. ValueFromPipelinebyPropertyName = $true)]
  281. [Alias("DirectoryName")]
  282. [System.String]
  283. $Name = "Config",
  284.  
  285. [Parameter(
  286. Mandatory = $false,
  287. ValueFromPipeline = $true,
  288. ValueFromPipelinebyPropertyName = $true)]
  289. [System.Int32]
  290. $Days = 7,
  291.  
  292. [Parameter(
  293. Mandatory = $false,
  294. ValueFromPipeline = $true,
  295. ValueFromPipelinebyPropertyName = $true)]
  296. [Alias("Email")]
  297. [Switch]
  298. $SendEmail
  299. )
  300. BEGIN {
  301. If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
  302. {
  303. Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
  304. }
  305.  
  306. Write-SP2016Verbose "Config" "Starting" "Beginning SP Config Backup"
  307.  
  308. # Set SP Config Backup Directory Path
  309. If ($Name)
  310. {
  311. $ConfigPath = $Path.Trimend('\') + "\$Name"
  312. }
  313. else
  314. {
  315. $ConfigPath = $Path
  316. }
  317. }
  318. PROCESS {
  319. Invoke-SP2016BackupProcess -Path $ConfigPath -Name "Config" -Days $Days -SendEmail:$SendEmail -Verbose:$Verbose
  320. }
  321. }
  322.  
  323. <#
  324. .Synopsis
  325. Automated backup of the SharePoint Farm Config.
  326.  
  327. .Description
  328. Automates the backup of the SharePoint Farm Config.
  329.  
  330. .Parameter Path ("Directory")
  331. The path to the root backups directory (e.g. \\servername\path\to\backups).
  332.  
  333. .Parameter Namespace ("Name")
  334. Folder name of the SP Config backups to be appended to the Root Path.
  335.  
  336. .Parameter Days
  337. Number of days to maintain all backups. Default: 7.
  338.  
  339. .Parameter BackupMethod
  340. Method of Backup. Default: "Full".
  341.  
  342. .Parameter OnError
  343. Error Action over-ride for ErrorActionPreference. Default: "Stop".
  344.  
  345. .Parameter Percentage
  346. Percentage to expect a message. Default: 15.
  347.  
  348. .Parameter SendEmail ("Email")
  349. Switch whether to send an email or not. Default: $false
  350.  
  351. .Example
  352. Backup-SP2016Config -Path "\\servername\path\to\backups\root"
  353.  
  354. .Notes
  355. Name: Invoke-SP2016BackupProcess
  356. Author: Travis Smith
  357. Author: Tim Hansen
  358. Author: Ali Nooshabadi
  359. LastEdit: 05/09/2018
  360.  
  361. #>
  362. Function Invoke-SP2016BackupProcess {
  363. [CmdletBinding()]
  364. param(
  365. # Expects Root Path
  366. [Parameter(
  367. Mandatory = $true,
  368. Position = 0,
  369. ValueFromPipeline = $true,
  370. ValueFromPipelinebyPropertyName = $true)]
  371. [Alias("Directory")]
  372. [System.String]
  373. $Path,
  374.  
  375. [Parameter(
  376. Mandatory = $true,
  377. Position = 1,
  378. ValueFromPipeline = $true,
  379. ValueFromPipelinebyPropertyName = $true)]
  380. [Alias("Name")]
  381. [System.String]
  382. $Namespace,
  383.  
  384. [Parameter(
  385. Mandatory = $false,
  386. ValueFromPipeline = $true,
  387. ValueFromPipelinebyPropertyName = $true)]
  388. [System.Int32]
  389. $Days = 7,
  390.  
  391. [Parameter(
  392. Mandatory = $false,
  393. ValueFromPipeline = $true,
  394. ValueFromPipelinebyPropertyName = $true)]
  395. [Alias("Method")]
  396. [System.String]
  397. $BackupMethod = "Full",
  398.  
  399. [Parameter(
  400. Mandatory = $false,
  401. ValueFromPipeline = $true,
  402. ValueFromPipelinebyPropertyName = $true)]
  403. [System.String]
  404. $OnError = "Stop",
  405.  
  406. [Parameter(
  407. Mandatory = $false,
  408. ValueFromPipeline = $true,
  409. ValueFromPipelinebyPropertyName = $true)]
  410. [Alias("Percent")]
  411. [System.String]
  412. $Percentage = 15,
  413.  
  414. [Parameter(
  415. Mandatory = $false,
  416. ValueFromPipeline = $true,
  417. ValueFromPipelinebyPropertyName = $true)]
  418. [Alias("Email")]
  419. [Switch]
  420. $SendEmail
  421. )
  422. BEGIN {
  423. If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
  424. {
  425. Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
  426. }
  427.  
  428. Write-SP2016Verbose $Namespace "Processing" "Starting Backup"
  429.  
  430. Create-SP2016BackupDirectory $Path
  431. $BackupPath = Get-SP2016Directory $Path
  432.  
  433. Write-SP2016Verbose $Namespace "Processing" "Backing up SP2016 Farm to $BackupPath"
  434.  
  435. }
  436. PROCESS {
  437. # Perform new backup
  438. Invoke-SP2016Backup -Path $BackupPath -Name $Namespace -SendEmail:$SendEmail -Verbose:$Verbose
  439.  
  440. # Clean up Old Backups
  441. Remove-SP2016OldBackups -Path $BackupPath -Name $Namespace -Days $Days -Verbose:$Verbose
  442.  
  443. # Check for Backup Completion!
  444. Watch-2016BackupStatus -Path $Path -Name $Namespace -Verbose:$Verbose
  445.  
  446.  
  447. Write-SP2016Verbose $Namespace "Processing" "Monitoring Complete!" -ForegroundColor "Green"
  448. }
  449. END {
  450. If ($SendEmail -and $SP2016From -and $SP2016To -and $SP2016SMTPHost)
  451. {
  452. # The backup status is saved in the last 4 log lines from the file. Save that information off
  453. $eBody += (Get-Content (Get-SP2016BackupLogFile $Path)) | Select-Object -First 1 -Last 4 #(Get-SP2016BackupLogFile $Path -Contents)[-2 .. -4]
  454.  
  455. # Send Email Status
  456. Write-SP2016Verbose $Namespace "Emailing" "Sending the email!"
  457. Send-SP2016BackupEmail -From $SP2016From -To $SP2016To -Subject $SP2016SubjectPrefix -Body $eBody -SMTP $SP2016SMTPHost -AttachmentName (Get-SP2016BackupLogFile $Path) #-Versbose:$Verbose
  458. }
  459. Else
  460. {
  461. Write-SP2016Verbose $Namespace "Emailing" "Cannot send email. Please run Set-SP2016EmailParameters." -Error
  462. }
  463. }
  464. }
  465.  
  466. <#
  467. .Synopsis
  468. Automated backup of the SharePoint Farm.
  469.  
  470. .Description
  471. Automates the backup of the SharePoint Farm.
  472.  
  473. .Parameter Path ("RootPath")
  474. The path to the root backups directory (e.g. \\servername\path\to\backups).
  475.  
  476. .Parameter Name ("DirectoryName")
  477. Folder name of the SP Config backups to be appended to the Root Path. Default: "Farm".
  478.  
  479. .Parameter Days
  480. Number of days to maintain all backups. Default: 7.
  481.  
  482. .Parameter SendEmail ("Email")
  483. Switch whether to send an email or not. Default: $false
  484.  
  485. .Example
  486. Backup-SP2016Farm -Path "\\servername\path\to\backups\root" -SendEmail
  487.  
  488. .Notes
  489. Name: Backup-SP2016Farm
  490. Author: Travis Smith
  491. Author: Tim Hansen
  492. Author: Ali Nooshabadi
  493. LastEdit: 05/09/2018
  494.  
  495. #>
  496. Function Backup-SP2016Farm {
  497. [CmdletBinding()]
  498. param(
  499. [Parameter(
  500. Mandatory = $true,
  501. ValueFromPipeline = $true,
  502. ValueFromPipelinebyPropertyName = $true)]
  503. [Alias("Directory")]
  504. [System.String]
  505. $Path,
  506.  
  507. [Parameter(
  508. Mandatory = $false,
  509. ValueFromPipeline = $true,
  510. ValueFromPipelinebyPropertyName = $true)]
  511. [Alias("DirectoryName")]
  512. [System.String]
  513. $Name = "Farm",
  514.  
  515. [Parameter(
  516. Mandatory = $false,
  517. ValueFromPipeline = $true,
  518. ValueFromPipelinebyPropertyName = $true)]
  519. [System.Int32]
  520. $Days = 7,
  521.  
  522. [Parameter(
  523. Mandatory = $false,
  524. ValueFromPipeline = $true,
  525. ValueFromPipelinebyPropertyName = $true)]
  526. [Alias("Email")]
  527. [Switch]
  528. $SendEmail
  529. )
  530. BEGIN {
  531. If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
  532. {
  533. Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
  534. }
  535.  
  536. Write-SP2016Verbose "Farm" "Starting" "Beginning SP Farm Backup Beginning..."
  537.  
  538. # Set SP Farm Backup Directory Path
  539. If ($Name) {
  540. $FarmPath = $Path.Trimend('\') + "\$Name"
  541. }
  542. Else
  543. {
  544. $FarmPath = $Path
  545. }
  546.  
  547. }
  548. PROCESS {
  549. Invoke-SP2016BackupProcess -Path $FarmPath -Name "Farm" -Days $Days -SendEmail:$SendEmail -Verbose:$Verbose
  550. }
  551. }
  552.  
  553. <#
  554. .Synopsis
  555. Automated backup of the SharePoint Service Applications.
  556.  
  557. .Description
  558. Automates the backup of the SharePoint Service Applications.
  559.  
  560. .Parameter Path ("Directory")
  561. The path to the root backups directory (e.g. \\servername\path\to\backups).
  562.  
  563. .Parameter Name ("DirectoryName")
  564. Folder name of the SP Config backups to be appended to the Root Path. Default: "Farm".
  565.  
  566. .Parameter Days
  567. Number of days to maintain all backups. Default: 7.
  568.  
  569. .Parameter SendEmail ("Email")
  570. Switch whether to send an email or not. Default: $false
  571.  
  572. .Example
  573. Backup-SP2016ServiceApplications -Path "\\servername\path\to\backups\root" -SendEmail
  574.  
  575. .Notes
  576. Name: Backup-SP2016ServiceApplications
  577. Author: Travis Smith
  578. Author: Tim Hansen
  579. Author: Ali Nooshabadi
  580. LastEdit: 05/09/2018
  581.  
  582. #>
  583. Function Backup-SP2016ServiceApplications {
  584. [CmdletBinding()]
  585. param(
  586. [Parameter(
  587. Mandatory = $true,
  588. ValueFromPipeline = $true,
  589. ValueFromPipelinebyPropertyName = $true)]
  590. [Alias("Directory")]
  591. [System.String]
  592. $Path,
  593.  
  594. [Parameter(
  595. Mandatory = $false,
  596. ValueFromPipeline = $true,
  597. ValueFromPipelinebyPropertyName = $true)]
  598. [Alias("DirectoryName")]
  599. [System.String]
  600. $Name = "ServiceApplications",
  601.  
  602. [Parameter(
  603. Mandatory = $false,
  604. ValueFromPipeline = $true,
  605. ValueFromPipelinebyPropertyName = $true)]
  606. [System.Int32]
  607. $Days = 7,
  608.  
  609. [Parameter(
  610. Mandatory = $false,
  611. ValueFromPipeline = $true,
  612. ValueFromPipelinebyPropertyName = $true)]
  613. [Alias("Email")]
  614. [Switch]
  615. $SendEmail
  616. )
  617. BEGIN {
  618. If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
  619. {
  620. Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
  621. }
  622.  
  623. Write-SP2016Verbose "ServiceApplications" "Starting" "Beginning SP Service Applications Backup"
  624.  
  625. # Set SP Service Applications Backup Directory Path
  626. If ($Name)
  627. {
  628. $SAPath = $Path.Trimend('\') + "\$Name"
  629. }
  630. else
  631. {
  632. $SAPath = $Path
  633. }
  634. }
  635. PROCESS {
  636. Invoke-SP2016BackupProcess -Path $SAPath -Name "ServiceApplications" -Days $Days -SendEmail:$SendEmail -Verbose:$Verbose
  637. }
  638. }
  639.  
  640. <#
  641. .Synopsis
  642. Automated backup of the SharePoint Service Applications.
  643.  
  644. .Description
  645. Automates the backup of the SharePoint Service Applications.
  646.  
  647. .Parameter Path ("Directory")
  648. The path to the root backups directory (e.g. \\servername\path\to\backups).
  649.  
  650. .Parameter Name ("DirectoryName")
  651. Folder name of the SP Config backups to be appended to the Root Path. Default: "Farm".
  652.  
  653. .Parameter Sites
  654. Array of sites to be backed up. Default: Get-SPSite -Limit All.
  655.  
  656. .Parameter Days
  657. Number of days to maintain all backups. Default: 7.
  658.  
  659. .Parameter SendEmail ("Email")
  660. Switch whether to send an email or not. Default: $false
  661.  
  662. .Example
  663. Backup-SP2016Sites -Path "\\servername\path\to\backups\root" -SendEmail
  664.  
  665. .Notes
  666. Name: Backup-SP2016Sites
  667. Author: Travis Smith
  668. Author: Tim Hansen
  669. Author: Ali Nooshabadi
  670. LastEdit: 05/09/2018
  671.  
  672. #>
  673. Function Backup-SP2016Sites {
  674. [CmdletBinding()]
  675. param(
  676. [Parameter(
  677. Mandatory = $true,
  678. ValueFromPipeline = $true,
  679. ValueFromPipelinebyPropertyName = $true)]
  680. [Alias("Directory")]
  681. [System.String]
  682. $Path,
  683.  
  684. [Parameter(
  685. Mandatory = $false,
  686. ValueFromPipeline = $true,
  687. ValueFromPipelinebyPropertyName = $true)]
  688. [Alias("DirectoryName")]
  689. [System.String]
  690. $Name = "Sites",
  691.  
  692. [Parameter(
  693. Mandatory = $false,
  694. ValueFromPipeline = $true,
  695. ValueFromPipelinebyPropertyName = $true)]
  696. [System.Collections.ArrayList]
  697. $Sites,
  698.  
  699. [Parameter(
  700. Mandatory = $false,
  701. ValueFromPipeline = $true,
  702. ValueFromPipelinebyPropertyName = $true)]
  703. [System.Int32]
  704. $Days = 7,
  705.  
  706. [Parameter(
  707. Mandatory = $false,
  708. ValueFromPipeline = $true,
  709. ValueFromPipelinebyPropertyName = $true)]
  710. [Alias("Email")]
  711. [Switch]
  712. $SendEmail
  713. )
  714. BEGIN {
  715. If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
  716. {
  717. Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
  718. }
  719.  
  720. Write-SP2016Verbose "Sites" "Starting" "Backing up SP2016 Site Collections"
  721.  
  722. # Get sites
  723. If (!$Sites) {
  724. Write-SP2016Verbose "Sites" "Starting" "Getting ALL sites!"
  725. $Sites = Get-SPSite -Limit All
  726. }
  727.  
  728. Write-SP2016Verbose "Sites" "Starting" "Sorting sites from largest to smallest by DiskSizeRequired"
  729. $Sites = $Sites | Sort-Object ContentDatabase.DiskSizeRequired
  730.  
  731. If ($Name) {
  732. $SitesPath = $Path.Trimend('\') + "\$Name"
  733. }
  734.  
  735. Create-SP2016BackupDirectory $SitesPath
  736.  
  737. $BackupPath = Get-SP2016Directory $SitesPath
  738.  
  739. Write-SP2016Verbose "Sites" "Starting" "Backing up SP2016 Farm to $SitesPath"
  740. }
  741. PROCESS {
  742. foreach ($site in $Sites)
  743. {
  744. $BackupPath = $site.PrimaryUri.Host
  745. If ($site.PrimaryUri.Segments.Length -gt 1)
  746. {
  747. $BackupPath += "." + $site.PrimaryUri.Segments[1].TrimEnd("/")
  748. If ($site.PrimaryUri.Segments.Length -gt 2)
  749. {
  750. $BackupPath += "." + $site.PrimaryUri.Segments[2]
  751. }
  752. }
  753.  
  754. Write-SP2016Verbose "Sites" "Processing" "Backing up $($site.Url) to $BackupPath\$BackupPath.bak"
  755.  
  756. # Take the site backup without locking the site (-NoSiteLock) and using SQL snapshot
  757. Backup-SPSite -Identity $site.id -Path "$BackupPath\$BackupPath.bak" -NoSiteLock -UseSqlSnapshot -Verbose:$Verbose
  758. }
  759. }
  760. END {
  761. Write-SP2016Verbose "Sites" "Ending" "Cleaning old Site Collection Backups"
  762. Remove-SP2016OldSites -Path $SitesPath - Days $Days
  763. }
  764. }
  765.  
  766. <#
  767. .Synopsis
  768. Automated cleanup of the SharePoint Site Collections.
  769.  
  770. .Description
  771. Automated cleanup of the SharePoint Site Collections.
  772.  
  773. .Parameter Path ("Directory")
  774. The path to the Sites backups directory (e.g. \\servername\path\to\backups\root\Sites).
  775.  
  776. .Parameter Days
  777. Number of days to maintain all backups. Default: 7.
  778.  
  779. .Example
  780. Remove-SP2016OldSites -Path "\\servername\path\to\backups\root\Sites" -SendEmail
  781.  
  782. .Notes
  783. Name: Remove-SP2016OldSites
  784. Author: Travis Smith
  785. Author: Tim Hansen
  786. Author: Ali Nooshabadi
  787. LastEdit: 05/09/2018
  788.  
  789. #>
  790. Function Remove-SP2016OldSites {
  791. [CmdletBinding()]
  792. param(
  793. [Parameter(
  794. Mandatory = $true,
  795. ValueFromPipeline = $true,
  796. ValueFromPipelinebyPropertyName = $true)]
  797. [Alias("Directory")]
  798. [System.String]
  799. $Path,
  800.  
  801. [Parameter(
  802. Mandatory = $false,
  803. ValueFromPipeline = $true,
  804. ValueFromPipelinebyPropertyName = $true)]
  805. [System.Int32]
  806. $Days = 7
  807. )
  808. BEGIN {
  809. Write-SP2016Verbose "Sites - Clean" "Starting" "Cleaning up SP2016 Site Collection Backups"
  810.  
  811. # Get/Set path
  812. If (!$Path -and $SP2016Path)
  813. {
  814. $Path = $SP2016Path
  815. }
  816. }
  817. PROCESS {
  818. # Remove old backup directories
  819. $old = gci $Path | ? { $_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-$Days) }
  820.  
  821. # Check If old directories exist
  822. If ($old -eq $null)
  823. {
  824. # Do Nothing
  825. break
  826. }
  827.  
  828. # Remove Old Directories
  829. If ($Verbose)
  830. {
  831. $old | % { Write-SP2016Verbose "Sites - Clean" "Processing" "Removing " + $_.FullName }
  832. }
  833. $old | % { Remove-Item $_.FullName -Recurse }
  834. }
  835. }
  836.  
  837. <#
  838. .Synopsis
  839. Automated cleanup of the SharePoint Backups (Farm, Config, & Service Applications).
  840.  
  841. .Description
  842. Automated cleanup of the SharePoint Backups (Farm, Config, & Service Applications; NOT Sites).
  843.  
  844. .Parameter Path ("Directory")
  845. The full path to the specific backups directory (e.g. \\servername\path\to\backups\root\Farm\{date.time}).
  846.  
  847. .Parameter Namespace ("Name")
  848. Folder name of the SP Config backups to be appended to the Root Path.
  849.  
  850. .Parameter Days
  851. Number of days to maintain all backups. Default: 7.
  852.  
  853. .Example
  854. Remove-SP2016OldBackups -Path "\\servername\path\to\backups\root\Farm\{date.time}" -Name "Farm"
  855.  
  856. .Notes
  857. Name: Remove-SP2016OldBackups
  858. Author: Travis Smith
  859. Author: Tim Hansen
  860. Author: Ali Nooshabadi
  861. LastEdit: 05/09/2018
  862.  
  863. #>
  864. Function Remove-SP2016OldBackups {
  865. [CmdletBinding()]
  866. param(
  867. [Parameter(
  868. Mandatory = $true,
  869. Position = 0,
  870. ValueFromPipeline = $true,
  871. ValueFromPipelinebyPropertyName = $true)]
  872. [Alias("Directory")]
  873. [System.String]
  874. $Path,
  875.  
  876. [Parameter(
  877. Mandatory = $true,
  878. Position = 1,
  879. ValueFromPipeline = $true,
  880. ValueFromPipelinebyPropertyName = $true)]
  881. [Alias("Name")]
  882. [System.String]
  883. $Namespace,
  884.  
  885. [Parameter(
  886. Mandatory = $false,
  887. ValueFromPipeline = $true,
  888. ValueFromPipelinebyPropertyName = $true)]
  889. [System.Int32]
  890. $Days = 7
  891. )
  892. BEGIN {
  893. Write-SP2016Verbose $Namespace "Cleaning" "Beginning to clean up old Farm backups"
  894. }
  895. PROCESS {
  896. Write-SP2016Verbose $Namespace "Cleaning" "Getting backup folders"
  897.  
  898. $files = Get-ChildItem -Path ((get-item $path).parent.FullName) | ?{ $_.PSIsContainer }
  899. if ($files.Count -gt $Days) {
  900. $files | Sort-Object CreationTime | Select-Object -First ($files.Count - $Days) | Remove-Item -Force -Recurse
  901. }
  902. }
  903. }
  904. # REGION: Helper Functions
  905.  
  906. <#
  907. .Synopsis
  908. Automated backup of the SharePoint Farm Config.
  909.  
  910. .Description
  911. Automates the backup of the SharePoint Farm Config.
  912.  
  913. .Parameter Path ("Directory")
  914. The path to the root backups directory (e.g. \\servername\path\to\backups).
  915.  
  916. .Parameter Namespace ("Name")
  917. Folder name of the SP Config backups to be appended to the Root Path.
  918.  
  919. .Parameter BackupMethod
  920. Method of Backup. Default: "Full".
  921.  
  922. .Parameter OnError
  923. Error Action over-ride for ErrorActionPreference. Default: "Stop".
  924.  
  925. .Parameter Percentage
  926. Percentage to expect a message. Default: 15.
  927.  
  928. .Parameter SendEmail ("Email")
  929. Switch whether to send an email or not. Default: $false
  930.  
  931. .Example
  932. Invoke-SP2016Backup -Path "\\servername\path\to\backups\root"
  933.  
  934. .Notes
  935. Name: Invoke-SP2016Backup
  936. Author: Travis Smith
  937. Author: Tim Hansen
  938. Author: Ali Nooshabadi
  939. LastEdit: 05/09/2018
  940.  
  941. #>
  942. Function Invoke-SP2016Backup {
  943. [CmdletBinding()]
  944. param(
  945. # Expects full Backup Path
  946. [Parameter(
  947. Mandatory = $true,
  948. Position = 0,
  949. ValueFromPipeline = $true,
  950. ValueFromPipelinebyPropertyName = $true)]
  951. [Alias("Directory")]
  952. [System.String]
  953. $Path,
  954.  
  955. [Parameter(
  956. Mandatory = $true,
  957. Position = 1,
  958. ValueFromPipeline = $true,
  959. ValueFromPipelinebyPropertyName = $true)]
  960. [Alias("Name")]
  961. [System.String]
  962. $Namespace,
  963.  
  964. [Parameter(
  965. Mandatory = $false,
  966. ValueFromPipeline = $true,
  967. ValueFromPipelinebyPropertyName = $true)]
  968. [Alias("Method")]
  969. [System.String]
  970. $BackupMethod = "Full",
  971.  
  972. [Parameter(
  973. Mandatory = $false,
  974. ValueFromPipeline = $true,
  975. ValueFromPipelinebyPropertyName = $true)]
  976. [System.String]
  977. $OnError = "Stop",
  978.  
  979. [Parameter(
  980. Mandatory = $false,
  981. ValueFromPipeline = $true,
  982. ValueFromPipelinebyPropertyName = $true)]
  983. [Alias("Percent")]
  984. [System.String]
  985. $Percentage = 15,
  986.  
  987. [Parameter(
  988. Mandatory = $false,
  989. ValueFromPipeline = $true,
  990. ValueFromPipelinebyPropertyName = $true)]
  991. [Alias("Email")]
  992. [Switch]
  993. $SendEmail
  994. )
  995. PROCESS {
  996. # Perform the backup
  997. Try
  998. {
  999. Write-SP2016Verbose $Namespace "Processing" "Beginning backup..."
  1000.  
  1001. # Run a new backup
  1002. Switch ($Namespace)
  1003. {
  1004. "Config" {
  1005. Write-Verbose "Doing Config Backup"
  1006. # Run a new full configuration-only backup
  1007. Backup-SPFarm -Directory $Path -BackupMethod $BackupMethod -ConfigurationOnly -ErrorAction $OnError -Verbose:$Verbose -Percentage $Percentage
  1008. }
  1009. "Farm" {
  1010. Write-Verbose "Doing Farm Backup"
  1011. # Run a new full farm backup
  1012. Backup-SPFarm -Directory $Path -BackupMethod $BackupMethod -ErrorAction $OnError -Verbose:$Verbose -Percentage $Percentage
  1013. }
  1014. "ServiceApplications" {
  1015. Write-Verbose "Doing SA Backup"
  1016. $Path
  1017. $BackupMethod
  1018. $OnError
  1019. $Percentage
  1020. Backup-SPFarm -Directory $Path -BackupMethod $BackupMethod -Item "Farm\Shared Services" -ErrorAction $OnError -Verbose:$Verbose -Percentage $Percentage
  1021. }
  1022. }
  1023.  
  1024. $eSubject += " Backup Completed Successfully"
  1025. }
  1026. Catch [system.exception] # check for exceptions
  1027. {
  1028. Write-SP2016Verbose $Namespace "Processing" "Backup Failed!" -Error
  1029. Write-SP2016Verbose $Namespace "Processing" $_.Exception.Message -Error
  1030.  
  1031. If ($SendEmail)
  1032. {
  1033. # save off the exception message
  1034. $eBody = $_.Exception.Message
  1035.  
  1036. # new email subject
  1037. $eSubject += "Backup Failed"
  1038.  
  1039. # send an email containing the backup failure
  1040. Send-SP2016BackupEmail $From $To $eSubject $eBody $SMTPHost -Versbose:$Verbose
  1041. }
  1042. # halt the script so we preserve older backups
  1043. break
  1044. }
  1045.  
  1046. }
  1047. }
  1048.  
  1049. <#
  1050. .Synopsis
  1051. Creates the Backup Directory.
  1052.  
  1053. .Description
  1054. Creates the Backup Directory from the Path (Backups Root Path) & Name (Specific Backup Name) or the specific Path.
  1055.  
  1056. .Parameter Path ("Directory")
  1057. The path to the root backups directory (e.g. \\servername\path\to\backups\root\Config).
  1058.  
  1059. .Parameter Namespace ("Name")
  1060. Folder name of the SP Config backups to be appended to the Root Path.
  1061.  
  1062. .Example
  1063. Create-SP2016BackupDirectory -Path "\\servername\path\to\backups\root" -Name "Config"
  1064. Create-SP2016BackupDirectory -Path "\\servername\path\to\backups\root\Config"
  1065.  
  1066. .Notes
  1067. Name: Create-SP2016BackupDirectory
  1068. Author: Travis Smith
  1069. Author: Tim Hansen
  1070. Author: Ali Nooshabadi
  1071. LastEdit: 05/09/2018
  1072.  
  1073. #>
  1074. Function Create-SP2016BackupDirectory {
  1075. [CmdletBinding()]
  1076. param(
  1077. [Parameter(
  1078. Mandatory = $true,
  1079. ValueFromPipeline = $true,
  1080. ValueFromPipelinebyPropertyName = $true)]
  1081. [Alias("Directory")]
  1082. [System.String]
  1083. $Path,
  1084.  
  1085. [Parameter(
  1086. Mandatory = $false,
  1087. ValueFromPipeline = $true,
  1088. ValueFromPipelinebyPropertyName = $true)]
  1089. [Alias("DirectoryName")]
  1090. [System.String]
  1091. $Name
  1092. )
  1093. BEGIN {
  1094. $today = Get-SP2016Date
  1095. }
  1096. PROCESS {
  1097. If ($Name)
  1098. {
  1099. Write-Verbose "Creating Directory $Path\$Name\$today"
  1100. New-Item $Path\$Name\$today -Type directory
  1101. }
  1102. Else
  1103. {
  1104. Write-Verbose "Creating Directory $Path\$today"
  1105. New-Item $Path\$today -Type directory
  1106. }
  1107. }
  1108. }
  1109.  
  1110. <#
  1111. .Synopsis
  1112. Gets the current date and caches it to a global variable, $SP2016Today.
  1113.  
  1114. .Description
  1115. Gets the current date and caches it to a global variable, $SP2016Today.
  1116.  
  1117. .Parameter Format
  1118. Format of the Date. Default: "yyyyMMdd.HHmmss".
  1119.  
  1120. .Example
  1121. Get-SP2016Date
  1122.  
  1123. .Notes
  1124. Name: Get-SP2016Date
  1125. Author: Travis Smith
  1126. Author: Tim Hansen
  1127. Author: Ali Nooshabadi
  1128. LastEdit: 05/09/2018
  1129.  
  1130. #>
  1131. Function Get-SP2016Date {
  1132. [CmdletBinding()]
  1133. param(
  1134. [Parameter(
  1135. Mandatory = $false,
  1136. ValueFromPipeline = $true,
  1137. ValueFromPipelinebyPropertyName = $true)]
  1138. [System.String]
  1139. $Format = "yyyyMMdd.HHmmss"
  1140. )
  1141. If ($SP2016Today)
  1142. {
  1143. Return $SP2016Today
  1144. }
  1145. $today = Get-Date -Format $Format
  1146. Set-Variable -Name "SP2016Today" -Value $today -Scope Global
  1147. Return $today
  1148. }
  1149.  
  1150. <#
  1151. .Synopsis
  1152. Creates the Backup Directory.
  1153.  
  1154. .Description
  1155. Creates the Backup Directory from the Path (Backups Root Path) & Name (Specific Backup Name) or the specific Path.
  1156.  
  1157. .Parameter Path ("Directory")
  1158. The path to the root backups directory (e.g. \\servername\path\to\backups\root\Config).
  1159.  
  1160. .Example
  1161. Get-SP2016Directory -Path "\\servername\path\to\backups\root\Config"
  1162.  
  1163. .Notes
  1164. Name: Get-SP2016Directory
  1165. Author: Travis Smith
  1166. Author: Tim Hansen
  1167. Author: Ali Nooshabadi
  1168. LastEdit: 05/09/2018
  1169.  
  1170. #>
  1171. Function Get-SP2016Directory {
  1172. [CmdletBinding()]
  1173. param(
  1174. [Parameter(
  1175. Mandatory = $false,
  1176. Position = 0,
  1177. ValueFromPipeline = $true,
  1178. ValueFromPipelinebyPropertyName = $true)]
  1179. [Alias("Directory")]
  1180. [System.String]
  1181. $Path
  1182. )
  1183.  
  1184. Write-Verbose "Path: $Path"
  1185. Write-Verbose ("Returning: $Path" + "\" + (Get-SP2016Date))
  1186. Return $Path.Trimend('\') + "\" + (Get-SP2016Date)
  1187. }
  1188.  
  1189. <#
  1190. .Synopsis
  1191. Watches the current backup for completion and completion status.
  1192.  
  1193. .Description
  1194. Watches the current backup for completion and completion status.
  1195.  
  1196. .Parameter Path ("Directory")
  1197. The path to the specific root (FarmPath, ConfigPath, SitesPath, SAPath) backup directory (e.g. \\servername\path\to\backups\root\Config).
  1198.  
  1199. .Parameter Namespace ("Name")
  1200. Folder name of the SP Config backups to be appended to the Root Path.
  1201.  
  1202. .Example
  1203. Watch-2016BackupStatus -Path "\\servername\path\to\backups\root\Config" -Name "Config"
  1204.  
  1205. .Notes
  1206. Name: Watch-2016BackupStatus
  1207. Author: Travis Smith
  1208. Author: Tim Hansen
  1209. Author: Ali Nooshabadi
  1210. LastEdit: 05/09/2018
  1211.  
  1212. #>
  1213. Function Watch-2016BackupStatus {
  1214. [CmdletBinding()]
  1215. param(
  1216. # Expects
  1217. [Parameter(
  1218. Mandatory = $true,
  1219. Position = 0,
  1220. ValueFromPipeline = $true,
  1221. ValueFromPipelinebyPropertyName = $true)]
  1222. [Alias("Directory")]
  1223. [System.String]
  1224. $Path,
  1225.  
  1226. [Parameter(
  1227. Mandatory = $true,
  1228. Position = 1,
  1229. ValueFromPipeline = $true,
  1230. ValueFromPipelinebyPropertyName = $true)]
  1231. [Alias("Name")]
  1232. [System.String]
  1233. $Namespace
  1234. )
  1235. BEGIN {
  1236. ## Check backup progress and rip status when complete ##
  1237. Write-SP2016Verbose $Namespace "Processing" "Monitoring backup..."
  1238. $start = Get-Date
  1239.  
  1240. # wait 15s for backup to initialize and restore log to be created
  1241. Sleep 15
  1242. $time = New-TimeSpan $start (Get-Date)
  1243. }
  1244. PROCESS {
  1245. do
  1246. {
  1247. $time = New-TimeSpan $start (Get-Date)
  1248. #Write-SP2016Verbose $Namespace "Processing" "Still monitoring. It's only been " + $time.TotalSeconds + "s..."
  1249.  
  1250. $backupPath = Get-SP2016BackupLogFile $Path -Contents
  1251. # Check for line at the end of the backup script
  1252. $backupStatusSuccess = Select-String -Path $backupPath -Pattern "Backup completed successfully." #-Quiet
  1253.  
  1254. # Check Success Result and check for Fail If nothing is there.
  1255. If ($backupStatusSuccess -eq $null)
  1256. {
  1257. $backupStatusFail = Select-String -Path $backupPath -Pattern "Backup failed" #-Quiet
  1258. If ($backupStatusSuccess -ne $null)
  1259. {
  1260. Write-SP2016Verbose $Namespace "Processing" "FatalError: Backup failed. " + (Get-SP2016BackupLogFile $Path -Contents) -Error
  1261. }
  1262. }
  1263. Else
  1264. {
  1265. Write-SP2016Verbose $Namespace "Processing" "Backup Completed Successfully." -ForegroundColor "Green"
  1266. }
  1267.  
  1268. # Run Loop while both Backup Statuses are empty
  1269. } while ($backupStatusSuccess -eq $null -and $backupStatusFail -eq $null)
  1270. }
  1271. }
  1272.  
  1273. <#
  1274. .Synopsis
  1275. Gets the backup log file from farm backups or partial farm backups (Config/Service Applications).
  1276.  
  1277. .Description
  1278. Gets the backup log file from farm backups or partial farm backups (Config/Service Applications).
  1279.  
  1280. .Parameter Path ("Directory")
  1281. The path to the specific root (FarmPath, ConfigPath, SitesPath, SAPath) backup directory (e.g. \\servername\path\to\backups\root\Config).
  1282.  
  1283. .Parameter Content ("GetContents")
  1284. Switch whether to return the file's contents. Default: $false.
  1285.  
  1286. .Example
  1287. Get-SP2016BackupLogFile -Path "\\servername\path\to\backups\root\Config" -Contents
  1288.  
  1289. .Notes
  1290. Name: Get-SP2016BackupLogFile
  1291. Author: Travis Smith
  1292. Author: Tim Hansen
  1293. Author: Ali Nooshabadi
  1294. LastEdit: 05/09/2018
  1295.  
  1296. #>
  1297. Function Get-SP2016BackupLogFile {
  1298. [CmdletBinding()]
  1299. param(
  1300. # Expects FarmPath, ConfigPath, SitesPath
  1301. [Parameter(
  1302. Mandatory = $true,
  1303. Position = 0,
  1304. ValueFromPipeline = $true,
  1305. ValueFromPipelinebyPropertyName = $true)]
  1306. [Alias("Directory")]
  1307. [System.String]
  1308. $Path,
  1309.  
  1310. [Parameter(
  1311. Mandatory = $false,
  1312. ValueFromPipeline = $true,
  1313. ValueFromPipelinebyPropertyName = $true)]
  1314. [Alias("GetContents")]
  1315. [Switch]
  1316. $Contents
  1317. )
  1318. PROCESS {
  1319. # find last backup directory
  1320. $lastBackupDir = gci $Path | ? { $_.PSIsContainer } | sort -prop LastWriteTime | select -last 1
  1321.  
  1322. # grab the backup log file from that directory
  1323. $backupLogFile = $lastBackupDir.FullName + "\spbr0000\spbackup.log"
  1324.  
  1325. If((Test-Path $backupLogFile) -And (Test-Path $Contents))
  1326. {
  1327. Return gc $backupLogFile
  1328. }
  1329. ElseIf(Test-Path $backupLogFile)
  1330. {
  1331. Return $backupLogFile
  1332. }
  1333.  
  1334. Write-SP2016Verbose "Get-SP2016BackupLogFile" "Ending" "Backup Log File ($backupLogFile) does not exist." -Error
  1335. Return
  1336. }
  1337. }
  1338.  
  1339. <#
  1340. .Synopsis
  1341. Sets global variables for the email operations.
  1342.  
  1343. .Description
  1344. Sets global variables for the email operations: From ($SP2016From), To ($SP2016To), SMTP ($SP2016SMTPHost), SubjectPrefix ($SP2016SubjectPrefix)
  1345.  
  1346. .Parameter From ("Sender")
  1347. Global. Sender email address for all email notifications.
  1348.  
  1349. .Parameter To ("Recipients")
  1350. Global. Recipient(s) email address(es) for all email notifications.
  1351.  
  1352. .Parameter SMTPHost ("SMTP")
  1353. Global. SMTP hostname or IP address.
  1354.  
  1355. .Parameter SubjectPrefix ("Subject")
  1356. Global. Subject prefix of the email subject. Default: "[SP2016 Backup] ".
  1357.  
  1358. .Example
  1359. Set-SP2016EmailParameters -From "no-reply@domain.com" -To "name@domain.com" -SMTP "mail.domain.com"
  1360.  
  1361. .Notes
  1362. Name: Set-SP2016EmailParameters
  1363. Author: Travis Smith
  1364. Author: Tim Hansen
  1365. Author: Ali Nooshabadi
  1366. LastEdit: 05/09/2018
  1367.  
  1368. #>
  1369. Function Set-SP2016EmailParameters {
  1370. [CmdletBinding()]
  1371. param(
  1372. [Parameter(
  1373. Mandatory = $true,
  1374. Position = 0,
  1375. ValueFromPipeline = $true,
  1376. ValueFromPipelinebyPropertyName = $true)]
  1377. [Alias("Sender")]
  1378. [System.String]
  1379. $From,
  1380.  
  1381. [Parameter(
  1382. Mandatory = $true,
  1383. Position = 1,
  1384. ValueFromPipeline = $true,
  1385. ValueFromPipelinebyPropertyName = $true)]
  1386. [Alias("Recipients")]
  1387. [System.String]
  1388. $To,
  1389.  
  1390. [Parameter(
  1391. Mandatory = $true,
  1392. Position = 2,
  1393. ValueFromPipeline = $true,
  1394. ValueFromPipelinebyPropertyName = $true)]
  1395. [Alias("SMTP")]
  1396. [System.String]
  1397. $SMTPHost,
  1398.  
  1399. [Parameter(
  1400. Mandatory = $false,
  1401. Position = 3,
  1402. ValueFromPipeline = $true,
  1403. ValueFromPipelinebyPropertyName = $true)]
  1404. [Alias("Subject")]
  1405. [System.String]
  1406. $SubjectPrefix = "[SP2016 Backup] "
  1407. )
  1408. BEGIN {
  1409. If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
  1410. {
  1411. Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
  1412. }
  1413. Write-SP2016Verbose "Email" "Starting" "Beginning to set Email Global Variables..."
  1414. }
  1415. PROCESS {
  1416. Set-Variable -Name "SP2016From" -Value $From -Scope Global
  1417. Set-Variable -Name "SP2016To" -Value $To -Scope Global
  1418. Set-Variable -Name "SP2016SMTPHost" -Value $SMTPHost -Scope Global
  1419. Set-Variable -Name "SP2016SubjectPrefix" -Value $SubjectPrefix -Scope Global
  1420. }
  1421. END {
  1422. Write-SP2016Verbose "Email" "Ending" "Email Global Variables Set."
  1423. }
  1424. }
  1425.  
  1426. <#
  1427. .Synopsis
  1428. Sends the email.
  1429.  
  1430. .Description
  1431. Sends the email.
  1432.  
  1433. .Parameter From ("Sender")
  1434. Sender email address.
  1435.  
  1436. .Parameter To ("Recipients")
  1437. Recipient(s) email address(es).
  1438.  
  1439. .Parameter Subject
  1440. Subject of the email.
  1441.  
  1442. .Parameter Body ("Message")
  1443. Body of the email.
  1444.  
  1445. .Parameter SMTPHost ("SMTP")
  1446. SMTP hostname or IP address.
  1447.  
  1448. .Parameter AttachmentName ("Attachment")
  1449. Attachment Name.
  1450.  
  1451. .Example
  1452. Set-SP2016EmailParameters -From "no-reply@domain.com" -To "name@domain.com" -SMTP "mail.domain.com"
  1453.  
  1454. .ToDo
  1455. Use an array for Send-Message
  1456. @{
  1457. Subject = "Backup Failed: Farm Configuration Database"
  1458. Body = "ERROR $_."
  1459. From = $FromAddress
  1460. To = $AdminEmail
  1461. SmtpServer = $MailServer
  1462. }
  1463. [Parameter(
  1464. Mandatory = $true,
  1465. ValueFromPipeline = $true,
  1466. ValueFromPipelinebyPropertyName = $true)]
  1467. [Alias("Mail")]
  1468. [System.Collections.ArrayList]
  1469. $Mail
  1470.  
  1471. .Notes
  1472. Name: Send-SP2016BackupEmail
  1473. Author: Travis Smith
  1474. Author: Tim Hansen
  1475. Author: Ali Nooshabadi
  1476. LastEdit: 05/09/2018
  1477.  
  1478. #>
  1479. Function Send-SP2016BackupEmail {
  1480. [CmdletBinding()]
  1481. param(
  1482. [Parameter(
  1483. Mandatory = $true,
  1484. Position = 0,
  1485. ValueFromPipeline = $true,
  1486. ValueFromPipelinebyPropertyName = $true)]
  1487. [Alias("Sender")]
  1488. [System.String]
  1489. $From,
  1490.  
  1491. [Parameter(
  1492. Mandatory = $true,
  1493. Position = 1,
  1494. ValueFromPipeline = $true,
  1495. ValueFromPipelinebyPropertyName = $true)]
  1496. [Alias("Recipients")]
  1497. [System.String]
  1498. $To,
  1499.  
  1500. [Parameter(
  1501. Mandatory = $true,
  1502. Position = 2,
  1503. ValueFromPipeline = $true,
  1504. ValueFromPipelinebyPropertyName = $true)]
  1505. [System.String]
  1506. $Subject,
  1507.  
  1508. [Parameter(
  1509. Mandatory = $true,
  1510. Position = 3,
  1511. ValueFromPipeline = $true,
  1512. ValueFromPipelinebyPropertyName = $true)]
  1513. [Alias("Message")]
  1514. [System.String[]]
  1515. $Body,
  1516.  
  1517. [Parameter(
  1518. Mandatory = $true,
  1519. Position = 4,
  1520. ValueFromPipeline = $true,
  1521. ValueFromPipelinebyPropertyName = $true)]
  1522. [Alias("SMTP")]
  1523. [System.String]
  1524. $SMTPHost,
  1525.  
  1526. [Parameter(
  1527. Mandatory = $false,
  1528. Position = 5,
  1529. ValueFromPipeline = $true,
  1530. ValueFromPipelinebyPropertyName = $true)]
  1531. [Alias("Attachment")]
  1532. [System.String]
  1533. $AttachmentName
  1534. )
  1535. PROCESS {
  1536. # check for an attachment (successful backup)
  1537. If ($AttachmentName)
  1538. {
  1539. # send email with attachment
  1540. Send-MailMessage -from $from -to $to -Subject $subject -body ($body -join "`r`n") -SmtpServer $SMTPHost -Attachments $AttachmentName -Verbose:$Verbose
  1541. }
  1542. else
  1543. {
  1544. # send email sans attachment
  1545. Send-MailMessage -from $from -to $to -Subject $subject -body ($body -join "`r`n") -SmtpServer $SMTPHost -Verbose:$Verbose
  1546. }
  1547. }
  1548. }
  1549.  
  1550. <#
  1551. .Synopsis
  1552. Write function for all write operations to format the output.
  1553.  
  1554. .Description
  1555. Write function for all write operations to format the output.
  1556.  
  1557. .Parameter Namespace ("Name")
  1558. Name of the current operation being run (e.g., "Farm", "Config", "Sites", "ServiceApplications")
  1559.  
  1560. .Parameter Step ("Stage")
  1561. Current stage of the operation (e.g., "Starting", "Processing", "Ending").
  1562.  
  1563. .Parameter Message ("Msg")
  1564. Message to be written to the screen.
  1565.  
  1566. .Parameter ForegroundColor ("Color")
  1567. Color to write the message on the screen.
  1568.  
  1569. .Parameter WriteHost ("Host")
  1570. Switch whether to write to host. Default: $false ($true if $ForegroundColor exists).
  1571.  
  1572. .Parameter WriteWarning ("Warning")
  1573. Switch whether to write a warning to host. Default: $false.
  1574.  
  1575. .Parameter WriteError ("Error")
  1576. Switch whether to write an error to host. Default: $false.
  1577.  
  1578. .Example
  1579. Get-SP2016BackupLogFile -Path "\\servername\path\to\backups\root\Config" -Contents
  1580.  
  1581. .Notes
  1582. Name: Write-SP2016Verbose
  1583. Author: Travis Smith
  1584. Author: Tim Hansen
  1585. Author: Ali Nooshabadi
  1586. LastEdit: 05/09/2018
  1587.  
  1588. #>
  1589. Function Write-SP2016Verbose {
  1590. [CmdletBinding()]
  1591. param(
  1592. [Parameter(
  1593. Mandatory = $true,
  1594. Position = 0,
  1595. ValueFromPipeline = $true,
  1596. ValueFromPipelinebyPropertyName = $true)]
  1597. [Alias("Name")]
  1598. [System.String]
  1599. $Namespace,
  1600.  
  1601. [Parameter(
  1602. Mandatory = $true,
  1603. Position = 1,
  1604. ValueFromPipeline = $true,
  1605. ValueFromPipelinebyPropertyName = $true)]
  1606. [Alias("Stage")]
  1607. [System.String]
  1608. $Step,
  1609.  
  1610. [Parameter(
  1611. Mandatory = $true,
  1612. Position = 2,
  1613. ValueFromPipeline = $true,
  1614. ValueFromPipelinebyPropertyName = $true)]
  1615. [Alias("Msg")]
  1616. [System.String]
  1617. $Message,
  1618.  
  1619. [Parameter(
  1620. Mandatory = $false,
  1621. Position = 3,
  1622. ValueFromPipeline = $true,
  1623. ValueFromPipelinebyPropertyName = $true)]
  1624. [Alias("Color")]
  1625. [System.String]
  1626. $ForegroundColor,
  1627.  
  1628. [Parameter(
  1629. Mandatory = $false,
  1630. ValueFromPipeline = $true,
  1631. ValueFromPipelinebyPropertyName = $true)]
  1632. [Alias("Host")]
  1633. [Switch]
  1634. $WriteHost,
  1635.  
  1636. [Parameter(
  1637. Mandatory = $false,
  1638. ValueFromPipeline = $true,
  1639. ValueFromPipelinebyPropertyName = $true)]
  1640. [Alias("Warning")]
  1641. [Switch]
  1642. $WriteWarning,
  1643.  
  1644. [Parameter(
  1645. Mandatory = $false,
  1646. ValueFromPipeline = $true,
  1647. ValueFromPipelinebyPropertyName = $true)]
  1648. [Alias("Error")]
  1649. [Switch]
  1650. $WriteError
  1651. )
  1652. BEGIN {
  1653. $Step = $Step.ToUpper();
  1654. $m = [string]::Format("[{0}] {1}: {2}", $Namespace, $Step, $Message)
  1655. }
  1656. PROCESS {
  1657. If ($WriteWarning)
  1658. {
  1659. Write-Warning $m
  1660. }
  1661. ElseIf ($WriteError)
  1662. {
  1663. Write-Warning $m
  1664. }
  1665. ElseIf ($WriteHost -or $ForegroundColor)
  1666. {
  1667. Write-Host $m -ForegroundColor $ForegroundColor
  1668. }
  1669. Else
  1670. {
  1671. Write-Verbose $m
  1672. }
  1673. }
  1674. }
Add Comment
Please, Sign In to add comment