Get serial number of all hard disks and partitions in it

1.9k views Asked by At

I want to get all physical hard disk serial number(not volume serial number of drives) and partitions present in them.

Actually I used implementation from DISKID32 to get all hard disks serial number , but this will not give partitions in the hard disk.So I planned to use some other method.

Below code gives get serial number of physical hard disk and also find partitions in the each harddisks.

ComputerName = "."
Set wmiServices  = GetObject ( _
    "winmgmts:{impersonationLevel=Impersonate}!//" _
    & ComputerName)
' Get physical disk drive
Set wmiDiskDrives =  wmiServices.ExecQuery ( _
    "SELECT * FROM Win32_DiskDrive")

For Each wmiDiskDrive In wmiDiskDrives
    MsgBox "Disk drive Caption: " _
        & wmiDiskDrive.Caption _
        & VbNewLine & "DeviceID: " _
        & " (" & wmiDiskDrive.DeviceID & ")"
    MsgBox  "Serial number" _
                & wmiDiskDrive.SerialNumber
    'Use the disk drive device id to
    ' find associated partition
    query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
        & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
    Set wmiDiskPartitions = wmiServices.ExecQuery(query)

    For Each wmiDiskPartition In wmiDiskPartitions
        'Use partition device id to find logical disk
        Set wmiLogicalDisks = wmiServices.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
             & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")

        For Each wmiLogicalDisk In wmiLogicalDisks
            MsgBox  "Drive letter associated" _
                & " with disk drive = " _
                & wmiDiskDrive.Caption _
                & wmiDiskDrive.DeviceID _
                & VbNewLine & " Partition = " _
                & wmiDiskPartition.DeviceID _
                & VbNewLine & " is " _
                & wmiLogicalDisk.DeviceID

        Next
    Next
Next

It works perfectly in windows 8. But when I test in windows XP pc I got error while getting serial number i.e wmiDiskDrive.SerialNumber . All other objects are properly working.

Then I found that this property is not available in windows XP,windows server 2003 etc. Now from above code I can get harddisk model number and the partitions in it,but I want serial number.

So how I can get hard disk serial number and their partitions( should work in all windows OS)? Any idea?

1

There are 1 answers

1
Arifuzzaman Pranto On

This should solve your problem..

Public Function GetDriveSerialNumber(DrvL As String) As Long

         Dim fso As Object, Drv As Object
        Dim driveletter As String
        Dim DriveSerial As String
         Set fso = CreateObject("Scripting.FileSystemObject")
        driveletter = DrvL


             Set Drv = fso.GetDrive(driveletter)


         With Drv
             If .IsReady Then
                 DriveSerial = Abs(.SerialNumber)
             Else
                 DriveSerial = -1
             End If
         End With


         Set Drv = Nothing
         Set fso = Nothing

         GetDriveSerialNumber = DriveSerial

     End Function