/// <summary> /// This method is called when the cmdlet is invoked, and it's where we take care of setting up an /// e-mail message and sending it out. /// </summary> /// <seealso cref="SPBackupCatalog"/> protected override void UpdateDataObject() { // Setup error object in case we need to terminate the pipeline; also create the flag // that will be used to support ShouldProcess functionality. PowerShellError psError = new PowerShellError(); Boolean? isProcessingConfirmed = null; // Read-in the needed backup catalog object for further operations. Pipebind object // will ensure validity prior to return and throw a terminating exception if invalid. SPBackupCatalog workingCatalog = this.Identity.Read(); workingCatalog.Refresh(); // Until further notice, we assume errors are tied to our catalog and unspecified. psError.Object = workingCatalog; psError.Category = ErrorCategory.NotSpecified; // If anything goes wrong, we need to terminate the pipeline. try { // First check to see if this should be an error-only e-mail; if so, we need to ensure that the // we only proceed if an error exists in the latest backup set. if (!this.OnErrorOnly || workingCatalog.LastBackupErrorCount > 0) { // Validation attributes will ensure that we've got a consistent set of parameters; the only thing // we need to do before proceeding is see if an e-mail should be sent (if appropriate). if (isProcessingConfirmed == null) { isProcessingConfirmed = ShouldProcess(workingCatalog.CatalogPath, res.SPCmdletSendSPBackupStatus_ShouldProcess1); } // Setup the e-mail subject, but override it if an alternate subject line is specified String mailSubject = String.Format(res.SPCmdletSendSPBackupStatus_DefaultSubject, workingCatalog.CatalogPath); if (!String.IsNullOrEmpty(this.Subject)) { mailSubject = this.Subject; } // Create the e-mail body that will communicate the status and then send the e-mail. String mailBody = BackupCatalogUtilities.BuildEmailStatusBody(workingCatalog); CommunicationUtilities.SendFarmEmail(this.Recipients, String.Empty, mailSubject, mailBody, true); } } catch (Exception ex) //string not valid datetime { // Something went wrong with the backup catalog, the e-mail process, or something in-between. // Regardless, we need to terminate the pipeline -- nothing more can be done. ThrowTerminatingError(ex, psError.Category, psError.Object); } }
/// <summary> /// This method is called when the cmdlet is invoked, and it's where we take care of parsing /// property sets/updates for the backup set and then apply them. /// </summary> /// <seealso cref="SPBackupCatalog"/> protected override void UpdateDataObject() { // Setup error object in case we need to terminate the pipeline; also create the flag // that will be used to support ShouldProcess functionality. PowerShellError psError = new PowerShellError(); Boolean? isProcessingConfirmed = null; // Read-in the needed backup catalog object for further operations. Pipebind object // will ensure validity prior to return and throw a terminating exception if invalid. SPBackupCatalog workingCatalog = this.Identity.Read(); workingCatalog.Refresh(); // Until further notice, we assume errors are tied to our catalog and unspecified. psError.Object = workingCatalog; psError.Category = ErrorCategory.NotSpecified; // If anything goes wrong, we need to terminate the pipeline. try { // Validation attributes will ensure that we've got a consistent set of parameters; the only thing // we need to do before proceeding is see if an e-mail should be sent (if appropriate). if (isProcessingConfirmed == null) { isProcessingConfirmed = ShouldProcess(workingCatalog.CatalogPath, res.SPCmdletSetSPBackupCatalog_ShouldProcess1); } // Get the current catalog path and use that as the basis for modification. String catalogPath = workingCatalog.CatalogPath; BackupCatalogUtilities.UpdateBackupCatalogPaths(workingCatalog, catalogPath); } catch (Exception ex) { // Something went wrong with the backup catalog, the e-mail process, or something in-between. // Regardless, we need to terminate the pipeline -- nothing more can be done. ThrowTerminatingError(ex, psError.Category, psError.Object); } }
/// <summary> /// This method is called when the cmdlet is invoked, and it's where we take care of setting up /// and creating the target backup archive/export. /// </summary> /// <seealso cref="SPBackupCatalog"/> protected override void UpdateDataObject() { // Setup error object in case we need to terminate the pipeline; also create the flag // that will be used to support ShouldProcess functionality. PowerShellError psError = new PowerShellError(); Boolean? isProcessingConfirmed = null; // Read-in the needed backup catalog object for further operations. Pipebind object // will ensure validity prior to return and throw a terminating exception if invalid. SPBackupCatalog workingCatalog = this.Identity.Read(); workingCatalog.Refresh(); // Until further notice, we assume errors are tied to our catalog and unspecified. psError.Object = workingCatalog; psError.Category = ErrorCategory.NotSpecified; // Some variables we'll need throughout the export process. String exportFileOrFolder; Int32 exportCount; BackupSetExportMode exportMode; // If anything goes wrong, we need to terminate the pipeline. try { // Check the export path to make sure that it's someplace we can actually get to. if (!Directory.Exists(this.ExportPath)) { psError.Category = ErrorCategory.InvalidArgument; psError.Object = null; throw new ArgumentException(res.SPCmdletExportSPBackupCatalog_Ex_Arg1); } // If an export name is specified, make sure that it doesn't contain any invalid // path or filename characters; if the export name isn't specified, create one // using date/time information. if (!String.IsNullOrEmpty(this.ExportName)) { // If we're using compression, then we need to check against invalid filename // characters; without compression, we're checking against bad path chars. if (this.NoCompression) { Char[] invalidPathChars = Path.GetInvalidPathChars(); if (this.ExportName.IndexOfAny(invalidPathChars) != -1) { psError.Category = ErrorCategory.InvalidArgument; psError.Object = null; throw new ArgumentException(res.SPCmdletExportSPBackupCatalog_Ex_Arg2); } } else { Char[] invalidPathChars = Path.GetInvalidFileNameChars(); if (this.ExportName.IndexOfAny(invalidPathChars) != -1) { psError.Category = ErrorCategory.InvalidArgument; psError.Object = null; throw new ArgumentException(res.SPCmdletExportSPBackupCatalog_Ex_Arg3); } } // If we're here, then the export name contains only valid characters. exportFileOrFolder = this.ExportName; } else { // No -ExportName was specified, so we'll generate one using the current date/time // and a template. exportFileOrFolder = String.Format(Globals.EXPORT_FILE_OR_PATH_TEMPLATE, Globals.BuildFileAndPathCompatibleDateTime(DateTime.Now)); } // Make sure we don't have conflicting -IncludeCount and -ExcludeCount parameter // values -- one or the other. Provided we've got acceptable values, we'll parse // them out into a usable form for the ultimate export operation. if (this.IncludeCount > 0 && this.ExcludeCount > 0) { psError.Category = ErrorCategory.InvalidOperation; psError.Object = null; throw new ArgumentException(res.SPCmdletExportSPBackupCatalog_Ex_Arg4); } else if (this.IncludeCount > 0) { exportMode = BackupSetExportMode.IncludeMode; exportCount = this.IncludeCount; } else if (this.ExcludeCount > 0) { exportMode = BackupSetExportMode.ExcludeMode; exportCount = this.ExcludeCount; } else { exportMode = BackupSetExportMode.ExportAll; exportCount = 0; } // Check to make sure we've got the go-ahead (if needed) to write out an export. if (isProcessingConfirmed == null) { isProcessingConfirmed = ShouldProcess(workingCatalog.CatalogPath, res.SPCmdletExportSPBackupCatalog_ShouldProcess1); } // Carry out delete if we've gotten the thumbs-up if (isProcessingConfirmed.HasValue && isProcessingConfirmed == true) { BackupCatalogUtilities.ExportBackupCatalog(workingCatalog, this.ExportPath, exportFileOrFolder, exportMode, exportCount, this.IgnoreBackupSetErrors, this.NoCompression, this.UpdateCatalogPath); workingCatalog.Refresh(); } } catch (Exception ex) { // Something went wrong with the backup catalog, the archiving process, or something in-between. // Regardless, we need to terminate the pipeline -- nothing more can be done. ThrowTerminatingError(ex, psError.Category, psError.Object); } }
/// <summary> /// This method is called when the cmdlet is invoked, and it's where we take care of examining /// the supplied backup catalog (as an <see cref="SPBackupCatalog"/>) to determine which backup /// sets, if any, should be removed from it. /// </summary> /// <seealso cref="SPBackupCatalog"/> protected override void DeleteDataObject() { // Setup error object in case we need to terminate the pipeline; also create the flag // that will be used to support ShouldProcess functionality. PowerShellError psError = new PowerShellError(); Boolean? isProcessingConfirmed = null; // Read-in the needed backup catalog object for further operations. Pipebind object // will ensure validity prior to return and throw a terminating exception if invalid. SPBackupCatalog workingCatalog = this.Identity.Read(); workingCatalog.Refresh(); // Until further notice, we assume errors are tied to our catalog and unspecified. psError.Object = workingCatalog; psError.Category = ErrorCategory.NotSpecified; // If anything goes wrong, we need to terminate the pipeline. try { // The path we take depends on whether or not the entire catalog should be deleted. if (this.EntireCatalog) { // See if we need to confirm processing. if (isProcessingConfirmed == null) { isProcessingConfirmed = ShouldProcess(workingCatalog.CatalogPath, res.SPCmdletRemoveSPBackupCatalog_ShouldProcess1); } // Carry out delete if we've gotten the thumbs-up if (isProcessingConfirmed.HasValue && isProcessingConfirmed == true) { // Empty out the backup catalog; once it completes, we'll need to refresh the // working catalog due to changes the method makes. BackupCatalogUtilities.EmptyBackupCatalog(workingCatalog); workingCatalog.Refresh(); } } else { // Something less than the entire catalog is to be deleted. Ensure that we've got // some usable combination of parameters. if (this.RetainCount == 0 && this.RetainSize == 0) { psError.Category = ErrorCategory.InvalidArgument; psError.Object = null; throw new ArgumentException(res.SPCmdletRemoveSPBackupCatalog_Ex_Arg1); } // First pass at retention is by count if specified; of course, we only need // to process if the number of full backups is greater than the retain number. if (this.RetainCount > 0 && workingCatalog.FullBackupCount > this.RetainCount) { // See if we need to confirm processing. if (isProcessingConfirmed == null) { isProcessingConfirmed = ShouldProcess(workingCatalog.CatalogPath, res.SPCmdletRemoveSPBackupCatalog_ShouldProcess1); } // Carry out delete if we've gotten the thumbs-up if (isProcessingConfirmed.HasValue && isProcessingConfirmed == true) { // Carry out the trim operation; once it completes, we'll need to refresh the // working catalog due to changes the method makes. BackupCatalogUtilities.TrimBackupCatalogCount(workingCatalog, this.RetainCount, this.IgnoreBackupSetErrors); workingCatalog.Refresh(); } } // Next pass at retention is based on the desired size of all backups combined. if (this.RetainSize > 0 && workingCatalog.CatalogSize > this.RetainSize) { // See if we need to confirm processing. if (isProcessingConfirmed == null) { isProcessingConfirmed = ShouldProcess(workingCatalog.CatalogPath, res.SPCmdletRemoveSPBackupCatalog_ShouldProcess1); } // Carry out delete if we've gotten the thumbs-up if (isProcessingConfirmed.HasValue && isProcessingConfirmed == true) { BackupCatalogUtilities.TrimBackupCatalogSize(workingCatalog, this.RetainSize, this.IgnoreBackupSetErrors); workingCatalog.Refresh(); } } } } catch (Exception ex) { // Something went wrong with the backup catalog, the actual grooming, or something in-between. // Regardless, we need to terminate the pipeline -- nothing more can be done. ThrowTerminatingError(ex, psError.Category, psError.Object); } }