feat: add software inventory and insecure TLS option
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
using System.Management;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace NetBoxWindowsAgent;
|
||||
|
||||
internal static class HardwareCollector
|
||||
{
|
||||
public static HardwareInfo Collect()
|
||||
public static HardwareInfo Collect(bool collectSoftware = true, int maxSoftwareEntries = 250)
|
||||
{
|
||||
var computer = First("Win32_ComputerSystem", "Manufacturer", "Model", "TotalPhysicalMemory");
|
||||
var product = First("Win32_ComputerSystemProduct", "UUID");
|
||||
@@ -21,7 +22,29 @@ internal static class HardwareCollector
|
||||
|
||||
return new HardwareInfo(Environment.MachineName, S(computer, "Manufacturer"), S(computer, "Model"),
|
||||
S(bios, "SerialNumber"), S(product, "UUID"), S(os, "Caption"), S(os, "Version"),
|
||||
S(os, "OSArchitecture"), S(cpu, "Name"), U(computer, "TotalPhysicalMemory"), disks, networks);
|
||||
S(os, "OSArchitecture"), S(cpu, "Name"), U(computer, "TotalPhysicalMemory"), disks, networks,
|
||||
collectSoftware ? CollectSoftware(maxSoftwareEntries) : []);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<SoftwareInfo> CollectSoftware(int maximum)
|
||||
{
|
||||
var software = new Dictionary<string, SoftwareInfo>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var view in new[] { RegistryView.Registry64, RegistryView.Registry32 })
|
||||
{
|
||||
using var hive = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, view);
|
||||
using var uninstall = hive.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
|
||||
if (uninstall is null) continue;
|
||||
foreach (var subKeyName in uninstall.GetSubKeyNames())
|
||||
{
|
||||
using var entry = uninstall.OpenSubKey(subKeyName);
|
||||
var name = Convert.ToString(entry?.GetValue("DisplayName"))?.Trim();
|
||||
if (string.IsNullOrWhiteSpace(name) || Convert.ToInt32(entry?.GetValue("SystemComponent", 0)) == 1) continue;
|
||||
var version = Convert.ToString(entry?.GetValue("DisplayVersion"))?.Trim() ?? "";
|
||||
var publisher = Convert.ToString(entry?.GetValue("Publisher"))?.Trim() ?? "";
|
||||
software.TryAdd($"{name}\0{version}", new SoftwareInfo(name, version, publisher));
|
||||
}
|
||||
}
|
||||
return software.Values.OrderBy(x => x.Name, StringComparer.OrdinalIgnoreCase).Take(Math.Clamp(maximum, 0, 1000)).ToList();
|
||||
}
|
||||
|
||||
private static ManagementBaseObject First(string cls, params string[] fields) => Query(cls, fields).First();
|
||||
|
||||
Reference in New Issue
Block a user