protected override void ExecuteTask()
{
using (DiscoDataContext database = new DiscoDataContext())
{
Status.UpdateStatus(0, "Updating Active Directory Device Descriptions", "Reading Devices");
// Devices
var devices = database.Devices.Where(d => d.DeviceDomainId != null)
.ToList();
int failedTotal = 0;
int notFoundTotal = 0;
int completedTotal = 0;
// Refine valid devices
devices = devices.Where(d => ActiveDirectory.IsValidDomainAccountId(d.DeviceDomainId)).ToList();
foreach (var domainGroup in devices.GroupBy(d => d.ComputerDomainName).ToList())
{
ADDomain domain;
if (domainGroup.Key != null && ActiveDirectory.Context.TryGetDomainByNetBiosName(domainGroup.Key, out domain))
{
var controller = domain.GetAvailableDomainController(RequireWritable: true);
foreach (var device in domainGroup)
{
completedTotal++;
if ((completedTotal % 10) == 0)
{
Status.UpdateStatus((100D / devices.Count) * completedTotal, $"Processing: {device.DeviceDomainId} ({device.SerialNumber})");
}
try
{
var adAccount = device.ActiveDirectoryAccount();
if (adAccount == null)
{
notFoundTotal++;
if (!device.DecommissionedDate.HasValue)
{
Status.LogWarning($"Unable to locate [{device.DeviceDomainId}] for commissioned device [{device.SerialNumber}] in the domain");
}
}
else
{
adAccount.SetDescription(controller, device);
}
}
catch (Exception ex)
{
failedTotal++;
Status.LogWarning($"Error when setting description of computer account [{device.DeviceDomainId}] for device [{device.SerialNumber}]: [{ex.GetType().Name}] {ex.Message}");
}
}
}
}
// Finished - Remove Placeholder File
var requiredFilePath = RequiredFilePath(database);
if (requiredFilePath != null && File.Exists(requiredFilePath))
File.Delete(requiredFilePath);
Status.SetFinishedMessage($"Finished updating device descriptions for {devices.Count:N0}. {notFoundTotal:N0} were not found. {failedTotal:N0} failed.");
Status.LogInformation(Status.FinishedMessage);
Status.Finished();
}
}