Question

here is a vbscript which shows all drives' volume serial numbers. But I need to customize to return only the volume serial number of the drive which the script is running from.

How to do it?

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
str = ""
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk")
For Each objItem In colItems
   str = str & objItem.Name & " SerialNumber: " & objItem.VolumeSerialNumber & vbCrlf & vbCrlf
Next
MsgBox str
Was it helpful?

Solution

This should do what you need:

' Get the drive designator...
With CreateObject("Scripting.FileSystemObject")
    strDrive = .GetDriveName(WScript.ScriptFullName)
End With

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceId='" & strDrive & "'")

' There should only be one item in the collection...
For Each objItem In colItems
    MsgBox objItem.Name & " SerialNumber: " & objItem.VolumeSerialNumber
Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top