Home > Enterprise >  Get hard disk serial number on Windows Server with VBA
Get hard disk serial number on Windows Server with VBA

Time:02-04

I want to get hard disk serial number on Windows Server with VBA.

My code works just perfect on regular Windows but I get a Run-Time error 1004 with the message - Invalid number of arguments - when i'm trying to run it on a Windows Server OS.

This is the code:

Dim oWMI As Object
Dim oItems As Object
Dim oItem As Object
Dim IndexNumber As Integer

IndexNumber = 0
  
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
 
Set oItems = oWMI.ExecQuery("Select * from Win32_DiskDrive")
For Each oItem In oItems
    If oItem.Index = IndexNumber Then
        Msgbox Application.Trim(oItem.SerialNumber)
        Exit For
    End If
Next

Any ideas how to make it work ?

CodePudding user response:

One alternative to try:

Sub Tester()
    Dim s As String, arr, e
    s = ExecShellCmd("wmic diskdrive get serialnumber")
    Debug.Print "----------------------------"
    Debug.Print s
    arr = Split(s, vbLf)
    Debug.Print "----------------------------"
    Debug.Print arr(1)
End Sub

Public Function ExecShellCmd(FuncExec As String) As String
    ExecShellCmd = VBA.CreateObject("WScript.Shell") _
                    .exec("cmd.exe /c " & FuncExec).stdout.readall
End Function
  •  Tags:  
  • Related