/// <summary>
        /// Process the request using the provided connection context
        /// </summary>
        /// <param name="context">The connection context</param>
        private void ProcessWithContext(IServerDataServiceContext context)
        {
            // This is to enforce the mutual exclusivity of the parameters: Database
            // and DatabaseName.  This can't be done with parameter sets without changing
            // existing behaviour of the cmdlet.
            if (
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                this.WriteError(new ErrorRecord(
                                    new PSArgumentException(
                                        string.Format(Resources.InvalidParameterCombination, DatabaseParameter, DatabaseNameParameter)),
                                    string.Empty,
                                    ErrorCategory.InvalidArgument,
                                    null));
            }

            // ... and similarly for RestorableDroppedDatabase and DatabaseName / DatabaseDeletionDate
            if (
                this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                this.WriteError(new ErrorRecord(
                                    new PSArgumentException(
                                        string.Format(Resources.InvalidParameterCombination, RestorableDroppedDatabaseParameter, DatabaseNameParameter)),
                                    string.Empty,
                                    ErrorCategory.InvalidArgument,
                                    null));
            }
            if (
                this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter) &&
                this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                this.WriteError(new ErrorRecord(
                                    new PSArgumentException(
                                        string.Format(Resources.InvalidParameterCombination, RestorableDroppedDatabaseParameter, DatabaseDeletionDateParameter)),
                                    string.Empty,
                                    ErrorCategory.InvalidArgument,
                                    null));
            }

            // The DatabaseDeletionDate parameter can only be used if the RestorableDropped switch is also present
            if (!this.RestorableDropped.IsPresent && this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                throw new PSArgumentException(
                          string.Format(Resources.RestorableDroppedSwitchNotSpecified, DatabaseDeletionDateParameter));
            }

            // The Database parameter can only be used if the RestorableDropped switch is not present
            if (this.RestorableDropped.IsPresent && this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter))
            {
                throw new PSArgumentException(
                          string.Format(Resources.InvalidParameterCombination, RestorableDroppedParameter, DatabaseParameter));
            }

            // If the RestorableDropped switch is present, then either both the DatabaseName and DatabaseDeletionDate parameters must be present, or neither of them should be present.
            if (
                this.RestorableDropped.IsPresent && (
                    (this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter) && !this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter)) ||
                    (!this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter) && this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))))
            {
                throw new PSArgumentException(Resources.BothDatabaseNameAndDeletionDateNeedToBeSpecified);
            }

            // Obtain the database name from the given parameters.
            string databaseName = null;

            if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseParameter))
            {
                databaseName = this.Database.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter))
            {
                databaseName = this.RestorableDroppedDatabase.Name;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseNameParameter))
            {
                databaseName = this.DatabaseName;
            }

            DateTime databaseDeletionDate = default(DateTime);

            if (this.MyInvocation.BoundParameters.ContainsKey(RestorableDroppedDatabaseParameter))
            {
                databaseDeletionDate = this.RestorableDroppedDatabase.DeletionDate;
            }
            else if (this.MyInvocation.BoundParameters.ContainsKey(DatabaseDeletionDateParameter))
            {
                databaseDeletionDate = this.DatabaseDeletionDate;
            }
            databaseDeletionDate = CmdletCommon.NormalizeToUtc(databaseDeletionDate);

            try
            {
                if (!this.RestorableDropped.IsPresent && this.RestorableDroppedDatabase == null)
                {
                    // Live databases

                    if (databaseName != null)
                    {
                        // Retrieve the database with the specified name
                        this.WriteObject(context.GetDatabase(databaseName), true);
                    }
                    else
                    {
                        // No name specified, retrieve all databases in the server
                        this.WriteObject(context.GetDatabases(), true);
                    }
                }

                else
                {
                    // Dropped databases

                    if (databaseName != null)
                    {
                        // Retrieve the database with the specified name
                        this.WriteObject(context.GetRestorableDroppedDatabase(databaseName, databaseDeletionDate), true);
                    }
                    else
                    {
                        // No name specified, retrieve all databases in the server
                        this.WriteObject(context.GetRestorableDroppedDatabases(), true);
                    }
                }
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    context.ClientRequestId,
                    ex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Execute the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            // Obtain the name of the source database / source restorable dropped database from the parameters
            this.SourceDatabaseName =
                this.SourceDatabase != null ? this.SourceDatabase.Name :
                this.SourceRestorableDroppedDatabase != null ? this.SourceRestorableDroppedDatabase.Name :
                this.SourceDatabaseName;

            // Obtain the deletion date of the source restorable dropped database from the parameters
            DateTime?sourceDatabaseDeletionDate = null;

            if (this.SourceRestorableDroppedDatabase != null)
            {
                sourceDatabaseDeletionDate = this.SourceRestorableDroppedDatabase.DeletionDate;
            }
            else if (this.RestorableDropped.IsPresent)
            {
                sourceDatabaseDeletionDate = this.SourceDatabaseDeletionDate;
            }

            // Normalize the deletion date and point in time to UTC, if given
            if (sourceDatabaseDeletionDate != null)
            {
                sourceDatabaseDeletionDate = CmdletCommon.NormalizeToUtc(sourceDatabaseDeletionDate.Value);
            }

            if (this.PointInTime != null)
            {
                this.PointInTime = CmdletCommon.NormalizeToUtc(this.PointInTime.Value);
            }

            IServerDataServiceContext connectionContext = null;

            // If a database object was piped in, use its connection context...
            if (this.SourceDatabase != null)
            {
                connectionContext = this.SourceDatabase.Context;
            }
            else if (this.SourceRestorableDroppedDatabase != null)
            {
                connectionContext = this.SourceRestorableDroppedDatabase.Context;
            }

            // ... but only if it's a cert auth context. Otherwise, create a cert auth context using this.ServerName or the server name of the SQL auth context.
            if (!(connectionContext is ServerDataServiceCertAuth))
            {
                string serverName = this.SourceServerName ?? connectionContext.ServerName;

                connectionContext = ServerDataServiceCertAuth.Create(serverName, Profile, Profile.Context.Subscription);
            }

            string clientRequestId = connectionContext.ClientRequestId;

            try
            {
                RestoreDatabaseOperation operation = connectionContext.RestoreDatabase(
                    this.SourceDatabaseName,
                    sourceDatabaseDeletionDate,
                    this.TargetServerName,
                    this.TargetDatabaseName,
                    this.PointInTime);

                this.WriteObject(operation);
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    clientRequestId,
                    ex);
            }
        }