Inside Man
Inside Man

Reputation: 4297

Getting Volume Serial Number Of Script Location

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

Upvotes: 0

Views: 607

Answers (1)

Bond
Bond

Reputation: 16311

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

Upvotes: 1

Related Questions