Advertisement
Guest User

Untitled

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