#Include <Crypt.au3>
ConsoleWrite(_UniqueUSBFlashID('G:') & @CR)
Func _UniqueUSBFlashID($sDrive)
Local $oService, $oItems, $Hash, $Disk = '', $Part = '', $Text = ''
$oService = ObjGet('winmgmts:\\.\root\cimv2')
If Not IsObj($oService) Then
Return SetError(1, 0, '')
EndIf
$oItems = $oService.ExecQuery('SELECT * FROM Win32_LogicalDiskToPartition')
If Not IsObj($oItems) Then
Return SetError(2, 0, '')
EndIf
For $Property In $oItems
If StringInStr($Property.Dependent, '"' & $sDrive & '"') Then
$Part = StringTrimLeft($Property.Antecedent, StringInStr($Property.Antecedent, '='))
ExitLoop
EndIf
Next
If Not $Part Then
Return SetError(3, 0, '')
EndIf
$oItems = $oService.ExecQuery('SELECT * FROM Win32_DiskDriveToDiskPartition')
If Not IsObj($oItems) Then
Return SetError(2, 0, '')
EndIf
For $Property In $oItems
If StringInStr($Property.Dependent, $Part) Then
$Disk = StringTrimLeft($Property.Antecedent, StringInStr($Property.Antecedent, '='))
ExitLoop
EndIf
Next
If Not $Disk Then
Return SetError(3, 0, '')
EndIf
$oItems = $oService.ExecQuery('SELECT * FROM Win32_DiskDrive WHERE DeviceID = ' & $Disk)
If Not IsObj($oItems) Then
Return SetError(2, 0, '')
EndIf
For $Property In $oItems
If $Property.InterfaceType = 'USB' Then
$Text = StringTrimLeft($Property.PnPDeviceID, StringInStr($Property.PnPDeviceID, '&VEN_'))
$Text = StringStripWS(StringLeft($Text, StringInStr($Text, '\', 0, -1) - 1), 8)
$Text &= DriveGetFileSystem($sDrive) & DriveGetSerial($sDrive)
$Text &= $Property.TotalCylinders
$Text &= $Property.TotalHeads
$Text &= $Property.TotalSectors
$Text &= $Property.TotalTracks
ExitLoop
EndIf
Next
If Not $Text Then
Return SetError(3, 0, '')
EndIf
$Hash = StringTrimLeft(_Crypt_HashData($Text, $CALG_MD5), 2)
If Not $Hash Then
Return SetError(4, 0, '')
EndIf
Return '{' & StringMid($Hash, 1, 8) & '-' & StringMid($Hash, 9, 4) & '-' & StringMid($Hash, 13, 4) & '-' & StringMid($Hash, 17, 4) & '-' & StringMid($Hash, 21, 12) & '}'
EndFunc ;==>_UniqueUSBFlashID