Guest User

Untitled

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