I currently have a function that returns the hard drive serial of a virtual machine using calls via WMI, which works great when run on an actual physical hard drive. However, when I run the function on a virtual machine with a virtual disk, the hard drive serial always comes back as the same series of 1’s and 0’s. I am trying to use this technique to identify a specific machine. Is there a more reliable way to retrieve some sort of identifier which identifies the hardware used in a (virtual) machine that will likely not change?
As a note, I have had the MAC Address given to me as a suggestion, but I do not want my software to break if the NIC it is bound to has to be replaced.
I am also concerned with what might return on a system hard drive which is configured via RAID, as this serial needs to be consistent with every call. I do not have a RAID configured system to test this on, however, so I am unsure of what will even be returned in the first place.
EDIT I have figured out a reliable way to lock our software to a virtual machine even if the serial number might not be unique, so the VM portion is no longer an issue. However, I still am unsure of how this might return on with certain RAID configurations, and as stated above, I do not have the luxury of a RAID configured machine to test on, much less several configurations to test. Any assistance on this is very much appreciated.
Here is the HD serial function:
string Win32_Class = string.Empty;
string Win32_Property = string.Empty;
string systemDrive = null;
try
{
systemDrive = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System).Substring(0, 2);
Win32_Class = "Win32_LogicalDisk";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(string.Format("SELECT * FROM {1} WHERE DeviceID='{0}'", systemDrive, Win32_Class)))
{
foreach (ManagementObject logicalDisk in searcher.Get())
{
Win32_Class = "Win32_DiskPartition";
foreach (ManagementObject partition in logicalDisk.GetRelated(Win32_Class))
{
Win32_Class = "Win32_DiskDrive";
foreach (ManagementObject diskDrive in partition.GetRelated(Win32_Class))
{
Win32_Class = "Win32_PhysicalMedia";
foreach (ManagementObject diskMedia in diskDrive.GetRelated(Win32_Class))
{
Win32_Property = "SerialNumber";
mySystemDeviceSerial = diskMedia[Win32_Property].ToString().Trim();
}
}
}
}
}
}
You could use the serial number of the ‘logical’ disk. This will change if the disk is repartitioned. If one drive of a redundant RAID setup is changed it won’t change. This is something stored at the block level so it won’t matter what the actual storage setup is.
You want the
VolumeSerialNumberproperty of aWin32_LogicalDiskfor the installation volume.http://msdn.microsoft.com/en-us/library/windows/desktop/aa394173(v=vs.85).aspx