Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Ruby & WMI To Detect A USB Drive

DZone's Guide to

Using Ruby & WMI To Detect A USB Drive

·
Free Resource
From the Ruby on Windows blog.

How to use Windows Management Instrumentation (WMI) to determine if a "USB Mass Storage Device" is inserted.

require 'win32ole'

wmi = WIN32OLE.connect("winmgmts://")

devices = wmi.ExecQuery("Select * From Win32_USBControllerDevice")
for device in devices do
    device_name = device.Dependent.gsub('"', '').split('=')[1]
    usb_devices = wmi.ExecQuery("Select * From Win32_PnPEntity Where DeviceID = '#{device_name}'")
    for usb_device in usb_devices do
        puts usb_device.Description
        if usb_device.Description == 'USB Mass Storage Device'
            # DO SOMETHING HERE
        end
    end
end

Further details can be found here.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}