/// <summary> /// Process the request using the provided connection context /// </summary> /// <param name="databaseName">the name of the database to retrieve</param> private void ProcessWithContext(IServerDataServiceContext context) { string databaseName = GetDatabaseName(); try { 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); } } catch (Exception ex) { SqlDatabaseExceptionHandler.WriteErrorDetails( this, context.ClientRequestId, ex); } }
/// <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); } }