### Get Harddisk Serial Number

Some thoughts on how to get the serial number of a harddisk using systems commands
Of course other solutions possible.
System Commands to obtain the manufacturers serial number
Windows
Solution 1: Run "wmic diskdrive get serialnumber"
Solution 2: Run "wmic path win32_physicalmedia get SerialNumber"
Example Output:
SerialNumber
23A8P2B8T
Linux
Run "hdparm -i /dev/hda"
Max OSX
Run "ioreg -l"
B4J example using jShell Library (1)
'Show the SerialNumber in default browser as HTML.
'Example getting serial number of a diskdrive under windows 8.1 by invoking:
'"wmic /output:serialnumber.html diskdrive get serialnumber /format:htable"
Declare a Global var
Private SerialNumberFile As String = "serialnumber.html"
'Run the shell via a button
`Sub buttonTest_Action
Dim shl As Shell
'Delete the html file
File.Delete(File.DirApp, SerialNumberFile )
'Run the shell with disabled timeout (Parameter -1)
shl.Initialize("shl", "cmd.exe", Array As String("/c", "wmic /output:'" & SerialNumberFile & "' diskdrive get serialnumber /format:htable"))
Try
shl.Run(-1)
Catch
Log(LastException.Message)
End Try
End Sub`
'Handle shell completion. If success then show html file in default browser
`Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success AND ExitCode = 0 Then
Log("Shell Process completed successfully.")
fx.ShowExternalDocument(File.GetUri(File.DirApp, SerialNumberFile))
Else
Log("Shell Process NOT completed."& CRLF & "Error: " & CRLF & StdErr)
End If
End Sub`
B4J example using jShell Library (1)
'Get the manufacturers serialnumber for the first physical drive using a vbs script:
'strComputer = "."
'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PhysicalMedia WHERE Tag = '\\\\.\\PHYSICALDRIVE0'")
'For Each objItem In colItems
' Wscript.Echo objItem.SerialNumber
'Next
`Sub GetSerialNumber
Dim shl As Shell
Dim lstParams As List: lstParams.Initialize
Dim strWSHFile As String = "serialnumber.vbs"
Try
'run the script via shell members
lstParams.Add("//nologo")
lstParams.Add(strWSHFile)
shl.Initialize("shl", "cscript.exe", lstParams)
shl.WorkingDirectory = File.DirApp
shl.Run(-1)
Catch
Log(LastException.Message)
End Try
End Sub`
'Handle shell completion. If success then show html file in default browser
`Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success AND ExitCode = 0 Then
Log("Shell Process completed successfully.")
Log(StdOut.Trim)
Log("---")
Else
Log("Shell Process NOT completed."& CRLF & "Error: " & CRLF & StdErr)
End If
End Sub`
Example Output
Program started.
Shell Process completed successfully.
23A8P2B8T
--

Changed: , Category: Application, File: T6C159M1161.htm, ID: 1161