Page 1 of 1
how to get diskdrive serial
Posted: 20 Aug 2023, 09:37
by vanheartnet098
I found this function and used to retrieve Serial numbers of diskdrive
But i want to get Only the drive where the script is running.
Is there any way I can get the return value using drive letter?
Note:
This Function Returns the Hardware Serial not The Volume Serial
Code: Select all
Get_Hardware_Disk_Serial(){
objWMIService := ComObjGet("winmgmts:\\.\root\cimv2")
colItems := objWMIService.ExecQuery("SELECT * FROM Win32_PhysicalMedia")._NewEnum
while colItems[objItem]
HDDInfo.= objItem.SerialNumber "`n"
return HDDInfo
}
Re: how to get diskdrive serial
Posted: 20 Aug 2023, 14:03
by mikeyww
Code: Select all
#Requires AutoHotkey v1.1.33
DriveGet sn, Serial, % drive := SubStr(A_ScriptDir, 1, 2)
MsgBox 64, Serial number, % drive "`n`n" sn
Re: how to get diskdrive serial
Posted: 22 Aug 2023, 08:54
by vanheartnet098
mikeyww wrote: ↑20 Aug 2023, 14:03
Code: Select all
#Requires AutoHotkey v1.1.33
DriveGet sn, Serial, % drive := SubStr(A_ScriptDir, 1, 2)
MsgBox 64, Serial number, % drive "`n`n" sn
Sorry I should've been specific to my post,
I want to get the serial number of the Hardisk (Hardware) Im using not the volume serial (Drive or partition)
Re: how to get diskdrive serial
Posted: 22 Aug 2023, 09:15
by mikeyww
You can change the drive letter as you wish.
Re: how to get diskdrive serial
Posted: 22 Aug 2023, 09:52
by RussF
@mikeyww, the OP is looking for the serial number assigned to the physical drive by the manufacturer. This can be viewed in utilities such as HWiNFO. The serial number that AHK returns is a logical volume serial created (randomly, I assume) whenever a volume is formatted. If you have multiple volumes on one physical drive (C:, D:, etc.), each volume will have its own unique serial number and is the one AHK returns, however none of them is the same as the physical serial number of the drive.
That said, I don't know how to go about retrieving the phys. serial no. with AHK. There may be some obscure DLL call, but more talented people than I would need to figure it out.
Russ
Re: how to get diskdrive serial
Posted: 22 Aug 2023, 10:01
by mikeyww
Thanks for clarifying. So need to identify drive from letter and then SN from drive. Could be NAS, flash, SSD, HDD, other. Not sure, but that info may help. I think that Disk Management does enable step 1, so there should be a way for that part at least.
Re: how to get diskdrive serial
Posted: 22 Aug 2023, 12:08
by RussF
It is possible to get the serial number(s) of your drive(s) using the following line in a CMD window:
wmic diskdrive get model,serialNumber,size,mediaType
Or in Powershell:
Get-PhysicalDisk | Select-Object FriendlyName,SerialNumber
The output of these commands could be redirected to a text file which would then be read by an AHK script. Unfortunately, I couldn't find a way to specify a drive by it's volume name (C:, d:, etc), so if you have multiple drives, you will get them all.
Another anomaly I noticed was that the serial number for my spinning rust hard drive was exactly the same in wmic, powershell and HWiNFO, however the one for my NVME SSD differed in HWiNFO from those reported by wmic and PS. Which one is correct? I'd have to pull the physical drive and look at the sticker and I'm sorry folks, that ain't happenin'.
That's about all I can do to help - I wish you luck.
Russ
Re: how to get diskdrive serial
Posted: 24 Aug 2023, 07:10
by vanheartnet098
RussF wrote: ↑22 Aug 2023, 09:52
@mikeyww, the OP is looking for the serial number assigned to the
physical drive by the manufacturer. This can be viewed in utilities such as HWiNFO. The serial number that AHK returns is a
logical volume serial created (randomly, I assume) whenever a volume is formatted. If you have multiple volumes on one physical drive (C:, D:, etc.), each volume will have its own unique serial number and is the one AHK returns, however none of them is the same as the physical serial number of the drive.
That said, I don't know how to go about retrieving the phys. serial no. with AHK. There may be some obscure DLL call, but more talented people than I would need to figure it out.
Russ
Thanks actually the code i posted above is already working, but it returns all the serial for all the hardisk installed. the problem is i have multiple hardisk installed in my pc, i want the script to identify only the hardisk that the script is running and return its hardware serialnumber.
Re: how to get diskdrive serial Topic is solved
Posted: 25 Aug 2023, 01:46
by jNizM
Something like this:
Code: Select all
MsgBox % GetScriptDiskSerialNumber()
GetScriptDiskSerialNumber()
{
static OPEN_EXISTING := 3
static METHOD_BUFFERED := 0
static FILE_ANY_ACCESS := 0
static FILE_DEVICE_MASS_STORAGE := 0x0000002d
static IOCTL_STORAGE_BASE := FILE_DEVICE_MASS_STORAGE
static IOCTL_STORAGE_GET_DEVICE_NUMBER := CTL_CODE(IOCTL_STORAGE_BASE, 0x0420, METHOD_BUFFERED, FILE_ANY_ACCESS)
static SCRIPT_DIR := "\\.\" SubStr(A_ScriptDir, 1, 2)
hFile := DllCall("CreateFile", "Str", SCRIPT_DIR, "UInt", 0, "UInt", 0, "Ptr", 0, "UInt", OPEN_EXISTING, "UInt", 0, "Ptr", 0, "Ptr")
if (hFile = -1)
{
DllCall("CloseHandle", "Ptr", hFile)
return False
}
BufSize := VarSetCapacity(Buf, 12)
STORAGE_DEVICE_NUMBER := DllCall("DeviceIoControl", "Ptr", hFile, "UInt", IOCTL_STORAGE_GET_DEVICE_NUMBER, "Ptr", 0, "UInt", 0, "Ptr", &Buf, "UInt", BufSize, "UInt*", SizeOut, "Ptr", 0)
if (STORAGE_DEVICE_NUMBER = 0)
{
DllCall("CloseHandle", "Ptr", hFile)
return False
}
DeviceNumber := NumGet(Buf, 4, "Uint")
DllCall("CloseHandle", "Ptr", hFile)
for objItem in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_DiskDrive")
{
if (objItem.Index = DeviceNumber)
SerialNumber := objItem.SerialNumber
}
return SerialNumber
}
CTL_CODE(DeviceType, Function, Method, Access)
{
return ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)
}
Re: how to get diskdrive serial
Posted: 04 Sep 2023, 08:20
by vanheartnet098