Fix: Client WMI Serial Number

Potential bug where a NULL serial number could cause an unexpected
error.
See: http://discoict.com.au/forum/support/2013/2/client-error.aspx
This commit is contained in:
Gary Sharp
2013-02-25 11:59:04 +11:00
parent 69c61a9b7d
commit 7b93c17317
+198 -180
View File
@@ -1,180 +1,198 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Management; using System.Management;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Disco.Client.Interop namespace Disco.Client.Interop
{ {
public static class SystemAudit public static class SystemAudit
{ {
public static string DeviceSerialNumber { get; private set; } public static string DeviceSerialNumber { get; private set; }
public static string DeviceSMBIOSVersion { get; private set; } public static string DeviceSMBIOSVersion { get; private set; }
public static string DeviceManufacturer { get; private set; } public static string DeviceManufacturer { get; private set; }
public static string DeviceModel { get; private set; } public static string DeviceModel { get; private set; }
public static string DeviceType { get; private set; } public static string DeviceType { get; private set; }
public static string DeviceUUID { get; private set; } public static string DeviceUUID { get; private set; }
public static bool DeviceIsPartOfDomain { get; private set; } public static bool DeviceIsPartOfDomain { get; private set; }
static SystemAudit() static SystemAudit()
{ {
// Get BIOS Information // Get BIOS Information
try try
{ {
using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT SerialNumber, SMBIOSBIOSVersion FROM Win32_BIOS WHERE PrimaryBios=true")) using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT SerialNumber, SMBIOSBIOSVersion FROM Win32_BIOS WHERE PrimaryBios=true"))
{ {
using (ManagementObjectCollection mResults = mSearcher.Get()) using (ManagementObjectCollection mResults = mSearcher.Get())
{ {
using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault()) using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
{ {
if (mItem != null) if (mItem != null)
{ {
DeviceSerialNumber = mItem.GetPropertyValue("SerialNumber").ToString().Trim(); DeviceSerialNumber = mItem.GetPropertyValue("SerialNumber") as string;
ErrorReporting.DeviceIdentifier = DeviceSerialNumber; if (!string.IsNullOrEmpty(DeviceSerialNumber))
DeviceSMBIOSVersion = mItem.GetPropertyValue("SMBIOSBIOSVersion").ToString().Trim(); DeviceSerialNumber = DeviceSerialNumber.Trim();
}
else ErrorReporting.DeviceIdentifier = DeviceSerialNumber;
{ DeviceSMBIOSVersion = mItem.GetPropertyValue("SMBIOSBIOSVersion") as string;
throw new Exception("No Win32_BIOS WHERE PrimaryBios=true was found"); if (!string.IsNullOrEmpty(DeviceSMBIOSVersion))
} DeviceSMBIOSVersion = DeviceSMBIOSVersion.Trim();
} }
} else
} {
} throw new Exception("No Win32_BIOS WHERE PrimaryBios=true was found");
catch (Exception ex) }
{ }
throw new Exception("Disco Client was unable to retrieve BIOS information from WMI", ex); }
} }
}
// Get System Information catch (Exception ex)
try {
{ throw new Exception("Disco Client was unable to retrieve BIOS information from WMI", ex);
using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT Manufacturer, Model, PartOfDomain, PCSystemType FROM Win32_ComputerSystem")) }
{
using (ManagementObjectCollection mResults = mSearcher.Get()) // Get System Information
{ try
using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault()) {
{ using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT Manufacturer, Model, PartOfDomain, PCSystemType FROM Win32_ComputerSystem"))
if (mItem != null) {
{ using (ManagementObjectCollection mResults = mSearcher.Get())
DeviceManufacturer = mItem.GetPropertyValue("Manufacturer").ToString().Trim(); {
DeviceModel = mItem.GetPropertyValue("Model").ToString().Trim(); using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
DeviceIsPartOfDomain = bool.Parse(mItem.GetPropertyValue("PartOfDomain").ToString()); {
DeviceType = PCSystemTypeToString((UInt16)mItem.GetPropertyValue("PCSystemType")); if (mItem != null)
} {
else DeviceManufacturer = mItem.GetPropertyValue("Manufacturer") as string;
{ if (!string.IsNullOrEmpty(DeviceManufacturer))
throw new Exception("No Win32_ComputerSystem was found"); DeviceManufacturer = DeviceManufacturer.Trim();
}
} DeviceModel = mItem.GetPropertyValue("Model") as string;
} if (!string.IsNullOrEmpty(DeviceModel))
} DeviceModel = DeviceModel.Trim();
}
catch (Exception ex) DeviceIsPartOfDomain = (bool)mItem.GetPropertyValue("PartOfDomain");
{ DeviceType = PCSystemTypeToString((UInt16)mItem.GetPropertyValue("PCSystemType"));
throw new Exception("Disco Client was unable to retrieve ComputerSystem information from WMI", ex); }
} else
{
// Get System Product Information throw new Exception("No Win32_ComputerSystem was found");
string ComputerSystemProductSerialNumber; }
try }
{ }
using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT IdentifyingNumber, UUID FROM Win32_ComputerSystemProduct")) }
{ }
using (ManagementObjectCollection mResults = mSearcher.Get()) catch (Exception ex)
{ {
using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault()) throw new Exception("Disco Client was unable to retrieve ComputerSystem information from WMI", ex);
{ }
if (mItem != null)
{ // Get System Product Information
ComputerSystemProductSerialNumber = mItem.GetPropertyValue("IdentifyingNumber").ToString().Trim(); string ComputerSystemProductSerialNumber;
DeviceUUID = mItem.GetPropertyValue("UUID").ToString().Trim(); try
} {
else using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT IdentifyingNumber, UUID FROM Win32_ComputerSystemProduct"))
{ {
throw new Exception("No Win32_ComputerSystemProduct was found"); using (ManagementObjectCollection mResults = mSearcher.Get())
} {
} using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
} {
} if (mItem != null)
} {
catch (Exception ex) ComputerSystemProductSerialNumber = mItem.GetPropertyValue("IdentifyingNumber") as string;
{ if (!string.IsNullOrEmpty(ComputerSystemProductSerialNumber))
throw new Exception("Disco Client was unable to retrieve ComputerSystemProduct information from WMI", ex); ComputerSystemProductSerialNumber = ComputerSystemProductSerialNumber.Trim();
}
DeviceUUID = mItem.GetPropertyValue("UUID") as string;
// Added 2012-11-22 G# - Lenovo IdeaPad Serial SHIM if (!string.IsNullOrEmpty(DeviceUUID))
// http://www.discoict.com.au/forum/feature-requests/2012/11/serial-number-detection-on-ideapads.aspx DeviceUUID = DeviceUUID.Trim();
if (string.IsNullOrWhiteSpace(DeviceSerialNumber) || }
(DeviceManufacturer.Equals("LENOVO", StringComparison.InvariantCultureIgnoreCase) && else
(DeviceModel.Equals("S10-3", StringComparison.InvariantCultureIgnoreCase) // S10-3 {
|| DeviceModel.Equals("2957", StringComparison.InvariantCultureIgnoreCase)))) // S10-2 throw new Exception("No Win32_ComputerSystemProduct was found");
{ }
try }
{ }
using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard")) }
{ }
using (ManagementObjectCollection mResults = mSearcher.Get()) catch (Exception ex)
{ {
using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault()) throw new Exception("Disco Client was unable to retrieve ComputerSystemProduct information from WMI", ex);
{ }
if (mItem != null)
{ // Added 2012-11-22 G# - Lenovo IdeaPad Serial SHIM
DeviceSerialNumber = mItem.GetPropertyValue("SerialNumber").ToString().Trim(); // http://www.discoict.com.au/forum/feature-requests/2012/11/serial-number-detection-on-ideapads.aspx
} if (string.IsNullOrWhiteSpace(DeviceSerialNumber) ||
else (DeviceManufacturer.Equals("LENOVO", StringComparison.InvariantCultureIgnoreCase) &&
{ (DeviceModel.Equals("S10-3", StringComparison.InvariantCultureIgnoreCase) // S10-3
throw new Exception("No Win32_BaseBoard was found"); || DeviceModel.Equals("2957", StringComparison.InvariantCultureIgnoreCase)))) // S10-2
} {
} try
} {
} using (ManagementObjectSearcher mSearcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard"))
} {
catch (Exception ex) using (ManagementObjectCollection mResults = mSearcher.Get())
{ {
throw new Exception("Disco Client was unable to retrieve BaseBoard information from WMI", ex); using (var mItem = mResults.Cast<ManagementObject>().FirstOrDefault())
} {
if (string.IsNullOrWhiteSpace(DeviceSerialNumber)) if (mItem != null)
DeviceSerialNumber = ComputerSystemProductSerialNumber; {
} DeviceSerialNumber = mItem.GetPropertyValue("SerialNumber") as string;
// End Added 2012-11-22 G# if (!string.IsNullOrEmpty(DeviceSerialNumber))
DeviceSerialNumber = DeviceSerialNumber.Trim();
ErrorReporting.DeviceIdentifier = DeviceSerialNumber; }
else
// Validate Device 'State' {
if (string.IsNullOrWhiteSpace(DeviceSerialNumber)) throw new Exception("No Win32_BaseBoard was found");
throw new Exception("This device has no serial number stored in BIOS or BaseBoard"); }
if (DeviceSerialNumber.Length > 60) }
throw new Exception(string.Format("The serial number reported by this device is over 60 characters long:{0}{1}", Environment.NewLine, DeviceSerialNumber)); }
} }
}
private static string PCSystemTypeToString(UInt16 PCSystemType) catch (Exception ex)
{ {
switch (PCSystemType) throw new Exception("Disco Client was unable to retrieve BaseBoard information from WMI", ex);
{ }
case 0: if (string.IsNullOrWhiteSpace(DeviceSerialNumber))
return "Unknown"; DeviceSerialNumber = ComputerSystemProductSerialNumber;
case 1: }
return "Desktop"; // End Added 2012-11-22 G#
case 2:
return "Mobile"; ErrorReporting.DeviceIdentifier = DeviceSerialNumber;
case 3:
return "Workstation"; // Validate Device 'State'
case 4: if (string.IsNullOrWhiteSpace(DeviceSerialNumber))
return "EnterpriseServer"; throw new Exception("This device has no serial number stored in BIOS or BaseBoard");
case 5: if (DeviceSerialNumber.Length > 60)
return "SmallOfficeAndHomeOfficeServer"; throw new Exception(string.Format("The serial number reported by this device is over 60 characters long:{0}{1}", Environment.NewLine, DeviceSerialNumber));
case 6: }
return "AppliancePC";
case 7: private static string PCSystemTypeToString(UInt16 PCSystemType)
return "PerformanceServer"; {
case 8: switch (PCSystemType)
return "Maximum"; {
default: case 0:
return "Unknown"; return "Unknown";
} case 1:
} return "Desktop";
} case 2:
} return "Mobile";
case 3:
return "Workstation";
case 4:
return "EnterpriseServer";
case 5:
return "SmallOfficeAndHomeOfficeServer";
case 6:
return "AppliancePC";
case 7:
return "PerformanceServer";
case 8:
return "Maximum";
default:
return "Unknown";
}
}
}
}