Files
PatchProbe-Server/PatchProbe.Cli/Collectors/HotfixCollector.cs
2026-05-25 10:29:38 +08:00

49 lines
1.7 KiB
C#

using System.Management;
using Microsoft.Extensions.Logging;
using PatchProbe.Shared.Contracts;
using PatchProbe.Shared.Models;
namespace PatchProbe.Cli.Collectors;
public sealed class HotfixCollector(ILogger<HotfixCollector> logger) : ICollector<List<InstalledHotfix>>
{
public Task<List<InstalledHotfix>> CollectAsync(CancellationToken cancellationToken = default)
{
logger.LogInformation("Collecting installed hotfixes via Win32_QuickFixEngineering");
var hotfixes = new List<InstalledHotfix>();
try
{
using var searcher = new ManagementObjectSearcher(
new ManagementScope(@"root\CIMv2"),
new ObjectQuery("SELECT * FROM Win32_QuickFixEngineering"));
using var results = searcher.Get();
foreach (ManagementBaseObject obj in results)
{
DateTimeOffset? installedOn = null;
var dateRaw = obj["InstalledOn"]?.ToString();
if (!string.IsNullOrEmpty(dateRaw) && DateTime.TryParse(dateRaw, out var dt))
installedOn = new DateTimeOffset(dt, TimeSpan.Zero);
hotfixes.Add(new InstalledHotfix
{
HotFixId = obj["HotFixID"]?.ToString()?.Trim(),
Description = obj["Description"]?.ToString()?.Trim(),
InstalledBy = obj["InstalledBy"]?.ToString()?.Trim(),
InstalledOn = installedOn,
});
}
logger.LogInformation("Collected {Count} installed hotfixes", hotfixes.Count);
}
catch (Exception ex)
{
logger.LogWarning(ex, "Hotfix collection failed");
}
return Task.FromResult(hotfixes);
}
}