Downgrade to C# 5 syntax for .NET Framework MSBuild compatibility
This commit is contained in:
@@ -17,15 +17,10 @@ namespace Disco.Plugins.ADCompare.Features
|
|||||||
this.database = database;
|
this.database = database;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Compare all Disco devices' assigned users against the AD computer managedBy field.
|
|
||||||
/// Only considers active (not decommissioned) devices with a domain ID.
|
|
||||||
/// </summary>
|
|
||||||
public DeviceComparisonSummary CompareAllDevices()
|
public DeviceComparisonSummary CompareAllDevices()
|
||||||
{
|
{
|
||||||
var summary = new DeviceComparisonSummary();
|
var summary = new DeviceComparisonSummary();
|
||||||
|
|
||||||
// Load active devices that have a domain computer account
|
|
||||||
var devices = database.Devices
|
var devices = database.Devices
|
||||||
.Include("AssignedUser")
|
.Include("AssignedUser")
|
||||||
.Where(d => d.DeviceDomainId != null && d.DecommissionedDate == null)
|
.Where(d => d.DeviceDomainId != null && d.DecommissionedDate == null)
|
||||||
@@ -50,24 +45,18 @@ namespace Disco.Plugins.ADCompare.Features
|
|||||||
return summary;
|
return summary;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Compare a single device's Disco assignment against AD managedBy.
|
|
||||||
/// </summary>
|
|
||||||
public DeviceComparisonResult CompareDevice(Device device)
|
public DeviceComparisonResult CompareDevice(Device device)
|
||||||
{
|
{
|
||||||
var result = new DeviceComparisonResult
|
var result = new DeviceComparisonResult();
|
||||||
{
|
result.SerialNumber = device.SerialNumber;
|
||||||
SerialNumber = device.SerialNumber,
|
result.DeviceDomainId = device.DeviceDomainId;
|
||||||
DeviceDomainId = device.DeviceDomainId,
|
result.ComputerName = device.ComputerName;
|
||||||
ComputerName = device.ComputerName,
|
result.DiscoAssignedUserId = device.AssignedUserId;
|
||||||
DiscoAssignedUserId = device.AssignedUserId,
|
result.DiscoAssignedUserDisplayName = device.AssignedUser != null ? device.AssignedUser.DisplayName : null;
|
||||||
DiscoAssignedUserDisplayName = device.AssignedUser?.DisplayName,
|
result.HasAssignment = !string.IsNullOrEmpty(device.AssignedUserId);
|
||||||
HasAssignment = !string.IsNullOrEmpty(device.AssignedUserId)
|
|
||||||
};
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Look up the computer in AD, requesting the managedBy attribute
|
|
||||||
var adAccount = ActiveDirectory.RetrieveADMachineAccount(device.DeviceDomainId, new[] { "managedBy" });
|
var adAccount = ActiveDirectory.RetrieveADMachineAccount(device.DeviceDomainId, new[] { "managedBy" });
|
||||||
|
|
||||||
if (adAccount == null)
|
if (adAccount == null)
|
||||||
@@ -80,12 +69,10 @@ namespace Disco.Plugins.ADCompare.Features
|
|||||||
result.FoundInAD = true;
|
result.FoundInAD = true;
|
||||||
result.ADAccountDisabled = adAccount.IsDisabled;
|
result.ADAccountDisabled = adAccount.IsDisabled;
|
||||||
|
|
||||||
// Get the managedBy DN from AD
|
|
||||||
var managedByDN = adAccount.GetPropertyValue<string>("managedBy");
|
var managedByDN = adAccount.GetPropertyValue<string>("managedBy");
|
||||||
result.ADManagedByDN = managedByDN;
|
result.ADManagedByDN = managedByDN;
|
||||||
result.HasManagedBy = !string.IsNullOrEmpty(managedByDN);
|
result.HasManagedBy = !string.IsNullOrEmpty(managedByDN);
|
||||||
|
|
||||||
// Resolve managedBy DN to a DOMAIN\username
|
|
||||||
if (result.HasManagedBy)
|
if (result.HasManagedBy)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -98,17 +85,15 @@ namespace Disco.Plugins.ADCompare.Features
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result.ADManagedByUserId = managedByDN; // fallback to DN
|
result.ADManagedByUserId = managedByDN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// If we can't resolve the DN, store it raw
|
|
||||||
result.ADManagedByUserId = managedByDN;
|
result.ADManagedByUserId = managedByDN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now compare
|
|
||||||
result.IsMatch = DetermineMatch(result);
|
result.IsMatch = DetermineMatch(result);
|
||||||
if (!result.IsMatch)
|
if (!result.IsMatch)
|
||||||
{
|
{
|
||||||
@@ -118,35 +103,26 @@ namespace Disco.Plugins.ADCompare.Features
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
result.FoundInAD = false;
|
result.FoundInAD = false;
|
||||||
result.MismatchReason = $"AD lookup error: {ex.Message}";
|
result.MismatchReason = "AD lookup error: " + ex.Message;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determine if the Disco assignment matches the AD managedBy.
|
|
||||||
/// </summary>
|
|
||||||
private bool DetermineMatch(DeviceComparisonResult result)
|
private bool DetermineMatch(DeviceComparisonResult result)
|
||||||
{
|
{
|
||||||
// Both empty = match (neither has an assignment)
|
|
||||||
if (!result.HasAssignment && !result.HasManagedBy)
|
if (!result.HasAssignment && !result.HasManagedBy)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// One has assignment, other doesn't = mismatch
|
|
||||||
if (result.HasAssignment != result.HasManagedBy)
|
if (result.HasAssignment != result.HasManagedBy)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Both have values - compare the user IDs (case-insensitive)
|
|
||||||
return string.Equals(
|
return string.Equals(
|
||||||
result.DiscoAssignedUserId,
|
result.DiscoAssignedUserId,
|
||||||
result.ADManagedByUserId,
|
result.ADManagedByUserId,
|
||||||
StringComparison.OrdinalIgnoreCase);
|
StringComparison.OrdinalIgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generate a human-readable reason for the mismatch.
|
|
||||||
/// </summary>
|
|
||||||
private string DetermineMismatchReason(DeviceComparisonResult result)
|
private string DetermineMismatchReason(DeviceComparisonResult result)
|
||||||
{
|
{
|
||||||
if (!result.FoundInAD)
|
if (!result.FoundInAD)
|
||||||
@@ -159,7 +135,7 @@ namespace Disco.Plugins.ADCompare.Features
|
|||||||
return "Not assigned in Disco but AD managedBy is set";
|
return "Not assigned in Disco but AD managedBy is set";
|
||||||
|
|
||||||
if (result.HasAssignment && result.HasManagedBy)
|
if (result.HasAssignment && result.HasManagedBy)
|
||||||
return $"Different users: Disco={result.DiscoAssignedUserId}, AD managedBy={result.ADManagedByUserId}";
|
return string.Format("Different users: Disco={0}, AD managedBy={1}", result.DiscoAssignedUserId, result.ADManagedByUserId);
|
||||||
|
|
||||||
return "Unknown mismatch";
|
return "Unknown mismatch";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user