how to get diskdrive serial Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
vanheartnet098
Posts: 63
Joined: 01 Sep 2020, 09:49

how to get diskdrive serial

20 Aug 2023, 09:37

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
}
Last edited by vanheartnet098 on 22 Aug 2023, 08:59, edited 1 time in total.
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: how to get diskdrive serial

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
vanheartnet098
Posts: 63
Joined: 01 Sep 2020, 09:49

Re: how to get diskdrive serial

22 Aug 2023, 08:54

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)
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: how to get diskdrive serial

22 Aug 2023, 09:15

You can change the drive letter as you wish.
RussF
Posts: 1440
Joined: 05 Aug 2021, 06:36

Re: how to get diskdrive serial

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
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: how to get diskdrive serial

22 Aug 2023, 10:01

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.
RussF
Posts: 1440
Joined: 05 Aug 2021, 06:36

Re: how to get diskdrive serial

22 Aug 2023, 12:08

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
vanheartnet098
Posts: 63
Joined: 01 Sep 2020, 09:49

Re: how to get diskdrive serial

24 Aug 2023, 07:10

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.
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: how to get diskdrive serial  Topic is solved

25 Aug 2023, 01:46

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)
}
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
vanheartnet098
Posts: 63
Joined: 01 Sep 2020, 09:49

Re: how to get diskdrive serial

04 Sep 2023, 08:20

@jNizM thank a lot this is what im looking for, :clap: :clap: :clap:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, mikeyww, vanove and 156 guests