DSInternals/Src/DSInternals.PowerShell/Commands/Replication/GetADReplAccountCommand.cs
Michael Grafnetter 85b48b32df - Added the Add-ADReplNgcKey cmdlet
- Added the UserPrincipalName parameter to Get-ADReplAccount cmdlet
- Improved DRS_MSG_GETCHGREQ_V8 delete
- Fixed Set-ADDBBootKey cmdlet output type
- Fixed some formatting inconsistencies
2019-08-24 23:07:57 +02:00

111 lines
3.8 KiB
C#

using System;
using System.Management.Automation;
using System.Security.Principal;
using DSInternals.Common.Data;
using DSInternals.PowerShell.Properties;
using DSInternals.Replication;
using DSInternals.Replication.Model;
namespace DSInternals.PowerShell.Commands
{
[Cmdlet(VerbsCommon.Get, "ADReplAccount")]
[OutputType(typeof(DSAccount))]
public class GetADReplAccountCommand : ADReplPrincipalCommandBase
{
protected const string ParameterSetAll = "All";
[Parameter(Mandatory = true, ParameterSetName = ParameterSetAll)]
[Alias("AllAccounts", "ReturnAllAccounts")]
public SwitchParameter All
{
get;
set;
}
[Parameter(Mandatory = true, ParameterSetName = ParameterSetAll)]
[ValidateNotNullOrEmpty]
[Alias("NC", "DomainNC", "DomainNamingContext")]
public string NamingContext
{
get;
set;
}
protected override void ProcessRecord()
{
if (this.ParameterSetName == ParameterSetAll)
{
this.ReturnAllAccounts();
}
else
{
this.ReturnSingleAccount();
}
}
protected void ReturnAllAccounts()
{
// Write the initial progress
// TODO: Extract strings as resources
var progress = new ProgressRecord(1, "Replication", "Replicating Active Directory objects.");
progress.PercentComplete = 0;
this.WriteProgress(progress);
// Update the progress after each replication cycle
ReplicationProgressHandler progressReporter = (ReplicationCookie cookie, int processedObjectCount, int totalObjectCount) =>
{
int percentComplete = (int)(((double)processedObjectCount / (double)totalObjectCount) * 100);
// AD's object count estimate is sometimes lower than the actual count, so we cap the value to 100%.
progress.PercentComplete = Math.Min(percentComplete, 100);
this.WriteProgress(progress);
};
// Replicate all accounts
foreach (var account in this.ReplicationClient.GetAccounts(this.NamingContext, progressReporter))
{
this.WriteObject(account);
}
// Write progress completed
progress.RecordType = ProgressRecordType.Completed;
this.WriteProgress(progress);
}
protected void ReturnSingleAccount()
{
DSAccount account;
switch (this.ParameterSetName)
{
case ParameterSetByDN:
account = this.ReplicationClient.GetAccount(this.DistinguishedName);
break;
case ParameterSetByName:
this.ValidateDomainName();
var accountName = new NTAccount(this.Domain, this.SamAccountName);
account = this.ReplicationClient.GetAccount(accountName);
break;
case ParameterSetByGuid:
account = this.ReplicationClient.GetAccount(this.ObjectGuid);
break;
case ParameterSetBySid:
account = this.ReplicationClient.GetAccount(this.ObjectSid);
break;
case ParameterSetByUPN:
var upn = new NTAccount(this.UserPrincipalName);
account = this.ReplicationClient.GetAccount(upn);
break;
default:
// This should never happen:
throw new PSInvalidOperationException(Resources.InvalidParameterSetMessage);
}
this.WriteObject(account);
}
}
}