DSInternals/Src/DSInternals.PowerShell/Commands/GetADDBAccountCommand.cs
2015-12-26 23:44:43 +01:00

96 lines
2.9 KiB
C#

using DSInternals.Common;
using DSInternals.Common.Cryptography;
using DSInternals.Common.Data;
using DSInternals.DataStore;
using DSInternals.PowerShell.Properties;
using System;
using System.Management.Automation;
namespace DSInternals.PowerShell.Commands
{
[Cmdlet(VerbsCommon.Get, "ADDBAccount")]
// TODO: output type
// TODO: Accept *
[OutputType(typeof(DSAccount))]
public class GetADDBAccountCommand : ADDBPrincipalCommandBase
{
protected const string parameterSetAll = "All";
[Parameter(
Mandatory = true,
HelpMessage = "TODO",
ParameterSetName = parameterSetAll
)]
[Alias("AllAccounts", "ReturnAllAccounts")]
public SwitchParameter All
{
get;
set;
}
[Parameter(
Mandatory = false,
HelpMessage = "TODO"
)]
[ValidateNotNullOrEmpty]
[ValidateHexString(BootKeyRetriever.BootKeyLength)]
[Alias("key", "SysKey")]
public string BootKey
{
get;
set;
}
protected override void ProcessRecord()
{
byte[] binaryBootKey = this.BootKey.HexToBinary();
// TODO: Exception handling: Object not found, malformed DN, ...
// TODO: Map DSAccount to transfer object
if(this.ParameterSetName == parameterSetAll)
{
this.ReturnAllAccounts(binaryBootKey);
}
else
{
this.ReturnSingleAccount(binaryBootKey);
}
}
private void ReturnAllAccounts(byte[] bootKey)
{
foreach (var account in this.DirectoryAgent.GetAccounts(bootKey))
{
this.WriteObject(account);
}
}
private void ReturnSingleAccount(byte[] bootKey)
{
DSAccount account;
switch (this.ParameterSetName)
{
case parameterSetByDN:
var dn = new DistinguishedName(this.DistinguishedName);
account = this.DirectoryAgent.GetAccount(dn, bootKey);
break;
case parameterSetByName:
account = this.DirectoryAgent.GetAccount(this.SamAccountName, bootKey);
break;
case parameterSetByGuid:
account = this.DirectoryAgent.GetAccount(this.ObjectGuid, bootKey);
break;
case parameterSetBySid:
account = this.DirectoryAgent.GetAccount(this.ObjectSid, bootKey);
break;
default:
// This should never happen:
throw new PSInvalidOperationException(Resources.InvalidParameterSetMessage);
}
this.WriteObject(account);
}
}
}