DSInternals/Src/DSInternals.PowerShell/Commands/Datastore/RemoveADDBObjectCommand.cs
2016-07-03 17:36:40 +02:00

88 lines
3.4 KiB
C#

namespace DSInternals.PowerShell.Commands
{
using DSInternals.Common.Data;
using DSInternals.Common.Exceptions;
using DSInternals.DataStore;
using DSInternals.PowerShell.Properties;
using System;
using System.Management.Automation;
[Cmdlet(VerbsCommon.Remove, "ADDBObject",
ConfirmImpact = ConfirmImpact.High,
SupportsShouldProcess = true
)]
[OutputType("None")]
public class RemoveADDBObjectCommand : ADDBObjectCommandBase
{
[Parameter(HelpMessage = "TODO")]
public SwitchParameter Force
{
get;
set;
}
protected override bool ReadOnly
{
get
{
return false;
}
}
protected override void BeginProcessing()
{
base.BeginProcessing();
if(! Force.IsPresent)
{
// Do not continue with operation until the user enforces it.
// TODO: Move to resources
var exception = new ArgumentException("This command physically deletes objects from the database. It does not leave tombstones, nor does it remove links and security descriptors associated with this object. As a consequence, database integrity errors and lingering objects will appear. Use at your own risk and never on production databases. To suppress this warning, reissue the command specifying the Force parameter.");
var error = new ErrorRecord(exception, "RemoveADDBObject_ForceRequired", ErrorCategory.InvalidArgument, null);
this.ThrowTerminatingError(error);
}
}
protected override void ProcessRecord()
{
// TODO: Extract message strings as resources
try
{
switch (this.ParameterSetName)
{
case parameterSetByDN:
var dn = new DistinguishedName(this.DistinguishedName);
if (this.ShouldProcess(this.DistinguishedName, "Remove Object from Database"))
{
this.DirectoryAgent.RemoveObject(dn);
}
else
{
// Just test the objects's presence.
this.DirectoryAgent.FindObject(dn);
}
break;
case parameterSetByGuid:
if (this.ShouldProcess(this.ObjectGuid.ToString(), "Remove Object from Database"))
{
this.DirectoryAgent.RemoveObject(this.ObjectGuid);
}
else
{
// Just test the objects's presence.
this.DirectoryAgent.FindObject(this.ObjectGuid);
}
break;
default:
// This should never happen:
throw new PSInvalidOperationException(Resources.InvalidParameterSetMessage);
}
}
catch(DirectoryObjectNotFoundException ex)
{
var error = new ErrorRecord(ex, "RemoveADDBObject_NotFound", ErrorCategory.ObjectNotFound, ex.ObjectIdentifier);
this.WriteError(error);
}
}
}
}