Sign in to follow this  
Followers 0
Beilby

Get OEM device serial number from USB memory stick

12 posts in this topic

Hi,

I would like to be able to identify the OEM device serial number from a USB memory stick if it exists, I have found some code written in c# here;

http://www.cfdan.com/posts/Retrieving_Non-...ing_C_Sharp.cfm

Could anyone help convert this code to Autoit please?

CODE
using System;

using System.Collections.Generic;

using System.Text;

using System.Management;

namespace USBDriveSerialNumber {

public class USBSerialNumber {

string _serialNumber;

string _driveLetter;

public string getSerialNumberFromDriveLetter(string driveLetter) {

this._driveLetter = driveLetter.ToUpper();

if(!this._driveLetter.Contains(":")) {

this._driveLetter += ":";

}

matchDriveLetterWithSerial();

return this._serialNumber;

}

private void matchDriveLetterWithSerial() {

string[] diskArray;

string driveNumber;

string driveLetter;

ManagementObjectSearcher searcher1 = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");

foreach (ManagementObject dm in searcher1.Get()) {

diskArray = null;

driveLetter = getValueInQuotes(dm["Dependent"].ToString());

diskArray = getValueInQuotes(dm["Antecedent"].ToString()).Split(',');

driveNumber = diskArray[0].Remove(0, 6).Trim();

if(driveLetter==this._driveLetter){

/* This is where we get the drive serial */

ManagementObjectSearcher disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

foreach (ManagementObject disk in disks.Get()) {

if (disk["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE" + driveNumber) & disk["InterfaceType"].ToString() == "USB") {

this._serialNumber = parseSerialFromDeviceID(disk["PNPDeviceID"].ToString());

}

}

}

}

}

private string parseSerialFromDeviceID(string deviceId) {

string[] splitDeviceId = deviceId.Split('\\');

string[] serialArray;

string serial;

int arrayLen = splitDeviceId.Length-1;

serialArray = splitDeviceId[arrayLen].Split('&');

serial = serialArray[0];

return serial;

}

private string getValueInQuotes(string inValue) {

string parsedValue = "";

int posFoundStart = 0;

int posFoundEnd = 0;

posFoundStart = inValue.IndexOf("\"");

posFoundEnd = inValue.IndexOf("\"", posFoundStart + 1);

parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd - posFoundStart) - 1);

return parsedValue;

}

}

}

Share this post


Link to post
Share on other sites



Hi,

I would like to be able to identify the OEM device serial number from a USB memory stick if it exists, I have found some code written in c# here;

http://www.cfdan.com/posts/Retrieving_Non-...ing_C_Sharp.cfm

Could anyone help convert this code to Autoit please?

CODE
using System;

using System.Collections.Generic;

using System.Text;

using System.Management;

namespace USBDriveSerialNumber {

public class USBSerialNumber {

string _serialNumber;

string _driveLetter;

public string getSerialNumberFromDriveLetter(string driveLetter) {

this._driveLetter = driveLetter.ToUpper();

if(!this._driveLetter.Contains(":")) {

this._driveLetter += ":";

}

matchDriveLetterWithSerial();

return this._serialNumber;

}

private void matchDriveLetterWithSerial() {

string[] diskArray;

string driveNumber;

string driveLetter;

ManagementObjectSearcher searcher1 = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");

foreach (ManagementObject dm in searcher1.Get()) {

diskArray = null;

driveLetter = getValueInQuotes(dm["Dependent"].ToString());

diskArray = getValueInQuotes(dm["Antecedent"].ToString()).Split(',');

driveNumber = diskArray[0].Remove(0, 6).Trim();

if(driveLetter==this._driveLetter){

/* This is where we get the drive serial */

ManagementObjectSearcher disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

foreach (ManagementObject disk in disks.Get()) {

if (disk["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE" + driveNumber) & disk["InterfaceType"].ToString() == "USB") {

this._serialNumber = parseSerialFromDeviceID(disk["PNPDeviceID"].ToString());

}

}

}

}

}

private string parseSerialFromDeviceID(string deviceId) {

string[] splitDeviceId = deviceId.Split('\\');

string[] serialArray;

string serial;

int arrayLen = splitDeviceId.Length-1;

serialArray = splitDeviceId[arrayLen].Split('&');

serial = serialArray[0];

return serial;

}

private string getValueInQuotes(string inValue) {

string parsedValue = "";

int posFoundStart = 0;

int posFoundEnd = 0;

posFoundStart = inValue.IndexOf("\"");

posFoundEnd = inValue.IndexOf("\"", posFoundStart + 1);

parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd - posFoundStart) - 1);

return parsedValue;

}

}

}

#include <Array.au3>

$ComputerName = "."
$wmiServices  = ObjGet ( _
    "winmgmts:{impersonationLevel=Impersonate}!//" _
    & $ComputerName)
$wmiDiskDrives =  $wmiServices.InstancesOf("Win32_DiskDrive")
Dim $List[1][4] , $i = 0
$List[0][0] = 0
$List[0][1] = "Model"
$List[0][2] = "InterfaceType"
$List[0][3] = "SerialNumber"
For $wmiDiskDrive In $wmiDiskDrives
ReDim $List[$List[0][0] + 2][4]
$List[$List[0][0] + 1][1] = $wmiDiskDrive.Model
$List[$List[0][0] + 1][2] = $wmiDiskDrive.InterfaceType
$List[0][0] = $List[0][0] + 1
Next 
$wmiDiskDrives =  $wmiServices.InstancesOf("Win32_PhysicalMedia")
For $wmiDiskDrive In $wmiDiskDrives
$i = $i + 1
$List[$i][3] = $wmiDiskDrive.SerialNumber
Next 

_ArrayDisplay($List, "Class List")

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites

It would appear that the serial number for a USB stick is actually in PNPDeviceID I have altered the code to show this in column 3;

CODE
#include <Array.au3>

$ComputerName = "."

$wmiServices = ObjGet ( _

"winmgmts:{impersonationLevel=Impersonate}!//" _

& $ComputerName)

$wmiDiskDrives = $wmiServices.InstancesOf("Win32_DiskDrive")

Dim $List[1][4] , $i = 0

$List[0][0] = 0

$List[0][1] = "Model"

$List[0][2] = "InterfaceType"

$List[0][3] = "SerialNumber"

For $wmiDiskDrive In $wmiDiskDrives

ReDim $List[$List[0][0] + 2][4]

$List[$List[0][0] + 1][1] = $wmiDiskDrive.Model

$List[$List[0][0] + 1][2] = $wmiDiskDrive.InterfaceType

$List[0][0] = $List[0][0] + 1

Next

$wmiDiskDrives = $wmiServices.InstancesOf("Win32_DiskDrive")

For $wmiDiskDrive In $wmiDiskDrives

$i = $i + 1

$List[$i][3] = $wmiDiskDrive.PNPDeviceID

Next

_ArrayDisplay($List, "Class List")

However, what I need to be able to do is choose the drive by drive letter and just output the PNPDeviceID for the drive letter chosen upon further research. Thanks for your help so far...

Share this post


Link to post
Share on other sites

Simplification of helpful code gives me the information I need but for all drives found;

CODE
$i = 0

$ComputerName = "."

$wmiServices = ObjGet ( _

"winmgmts:{impersonationLevel=Impersonate}!//" _

& $ComputerName)

$wmiDiskDrives = $wmiServices.InstancesOf("Win32_DiskDrive")

For $wmiDiskDrive In $wmiDiskDrives

$i = $i + 1

$List = $wmiDiskDrive.PNPDeviceID

MsgBox (0,"PNPDeviceID",$List)

Next

Please could someone help me get this to work only for given drive letter or some other method of choosing drive i.e. pick list.

Share this post


Link to post
Share on other sites

Or, from original code just display PNPDeviceID for InterfaceType USB.

Share this post


Link to post
Share on other sites

Hi,

I would like to be able to identify the OEM device serial number from a USB memory stick if it exists

Try attached. It is a mix of code I already had and code translate from the C# sample that you posted. It will return the serial number of both USB drives and of regular hard drives.

serno.au3

Share this post


Link to post
Share on other sites

Try attached. It is a mix of code I already had and code translate from the C# sample that you posted. It will return the serial number of both USB drives and of regular hard drives.

Here's my solution.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

MsgBox(0,"",getSerialNumberFromDriveLetter())

Func getSerialNumberFromDriveLetter($DRIVR = "C:")
Dim $I = 0 , $X = 0 ,$ComputerName = '.'
$wmiServices  = ObjGet ("winmgmts:{impersonationLevel=Impersonate}!//" & $ComputerName)
$wmiDiskDrives =  $wmiServices.InstancesOf("Win32_DiskDrive")

For $wmiDiskDrive In $wmiDiskDrives
$I =  $I + 1
$query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
        & $wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"    
$wmiDiskPartitions = $wmiServices.ExecQuery($query)
For $wmiDiskPartition In $wmiDiskPartitions
$wmiLogicalDisks = $wmiServices.ExecQuery ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& $wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 
For $wmiLogicalDisk In $wmiLogicalDisks
IF StringUpper($wmiLogicalDisk.DeviceID) = StringUpper($DRIVR) Then $X = $I
Next
Next
Next
$I = 0
$PhysicalMedia =  $wmiServices.InstancesOf("Win32_PhysicalMedia")
For $Item In $PhysicalMedia
$I = $I + 1
IF $I = $X Then Return $Item.SerialNumber
Next
EndFunc

Edited by wolf9228

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites

Try attached. It is a mix of code I already had and code translate from the C# sample that you posted. It will return the serial number of both USB drives and of regular hard drives.

How do you pass the Drive letter?

Share this post


Link to post
Share on other sites

Here's my solution.

This works well however how could we strip &0 from the right hand end?

Share this post


Link to post
Share on other sites

MsgBox(0,"",getSerialNumberFromDriveLetter())

Func getSerialNumberFromDriveLetter($DRIVR = "C:")
Dim $I = 0 , $X = 0 ,$ComputerName = '.'
$wmiServices  = ObjGet ("winmgmts:{impersonationLevel=Impersonate}!//" & $ComputerName)
$wmiDiskDrives =  $wmiServices.InstancesOf("Win32_DiskDrive")

For $wmiDiskDrive In $wmiDiskDrives
$I =  $I + 1
$query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
        & $wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"    
$wmiDiskPartitions = $wmiServices.ExecQuery($query)
For $wmiDiskPartition In $wmiDiskPartitions
$wmiLogicalDisks = $wmiServices.ExecQuery ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& $wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 
For $wmiLogicalDisk In $wmiLogicalDisks
IF StringUpper($wmiLogicalDisk.DeviceID) = StringUpper($DRIVR) Then $X = $I
Next
Next
Next
$I = 0
$PhysicalMedia =  $wmiServices.InstancesOf("Win32_PhysicalMedia")
For $Item In $PhysicalMedia
$I = $I + 1
IF $I = $X Then Return $Item.SerialNumber
Next
EndFunc

This works for HDD but not for USB

Share this post


Link to post
Share on other sites

Code that I currently have working gets string with Serial number from USB memory sticks.

CODE
$i = 0

$ComputerName = "."

$wmiServices = ObjGet ( _

"winmgmts:{impersonationLevel=Impersonate}!//" _

& $ComputerName)

$wmiDiskDrives = $wmiServices.InstancesOf("Win32_DiskDrive")

For $wmiDiskDrive In $wmiDiskDrives

$i = $i + 1

If $wmiDiskDrive.InterfaceType = "USB" Then

$DeviceID = $wmiDiskDrive.PNPDeviceID

MsgBox (0,"PNPDeviceID",$DeviceID)

EndIf

Next

However, I would also like to enumerate all USB drives and offer the choice of drive letter to get serial number from if there is more than one USB removable drive

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0