?
Solved

Script to Find USB drives

Posted on 2009-04-16
14
Medium Priority
?
5,672 Views
Last Modified: 2012-06-27
I have found pieces of information on how to query a computer to determine if a USB drive is inserted or not.  The script works fine until I added some code to show when there was no device added.

This code loops through every device and displays the no device found message.  If possible, I would like someone to review the code and make sure I have all the information correct as to what each step does and then help me in solving the display issue.  As a second part, if this works out, then I would like to look at taking the script to browse through a larger group of computers.

But first, just need some explanation on the script provided.

thanks,
dale

sNode = InputBox("Enter the name of the computer to Search for USB devices:") 
 
On Error Resume Next 'If error, resume next
'query the WMI service for the computer name
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
'select the information
Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 'display the connected computer   
 WScript.Echo "You have connected to computer " & sNode 
'connect to the File System Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Drives = FSO.Drives
 
'setup the messages to display
strMessage = "USB drives are not allowed!" & vbcr & vbcr & "Please unplug your USB Drive now!" & vbcr & vbcr & "Failure to do so can lead to disciplinary action!" _
	& vbcr & vbcr & "PLEASE REMOVE NOW!"
strMessage2 = " No devices found at this time "
' For next loop...go through all the drives in the system to search for USB drive
For Each DiskDrive In Drives
'If this is a USB drive and inserted display strMbox from below
	If DiskDrive.DriveType = "1" And DiskDrive.IsReady = "True" Then
 
		    strMbox = MsgBox(strMessage ,48)
		    'otherwise show no devices are availabel and display strMessage2
		    Else strMbox2 = MsgBox(strMessage2 ,48)
		End If
 Next

Open in new window

0
Comment
Question by:dgore1
  • 8
  • 6
14 Comments
 
LVL 31

Expert Comment

by:merowinger
ID: 24165480
The current script cannot work, as you write the input of the user in the variable sNode which is not used from the script to perform the actions.
Also if you want to check computer from remote you cannot use the filesystemobecjt, as this is only running from local...so the option is WMI Query.
Also a problem is, that wscript.,echo messages are only shown this computer which is executing the script and not on the remote computer.
Better would be if you execute the script on each computer local (maybe a scheduled task which runs each 5 minutes and executes the script?)
Below are two scripts...on with user input and on with a textfile containing all computername which are getting checked...



'Script with user input
On Error Resume Next
strComputer = InputBox("Enter the name of the computer to Search for USB devices:")
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colItems = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk WHERE DriveType = '2'")
 
For Each objItem in colItems
	WScript.Echo "On the computer " &strComputer &" there is connected a removable disk - drive letter is: " & objItem.Name
Next
'***********************************************************************************************************************************
'***********************************************************************************************************************************
'***********************************************************************************************************************************
'Script with computers out of a textfile
On Error Resume Next
'Connect to FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
'Open Textfile ith all Computers
Set objFile = objFSO.OpenTextFile("D:\Computers.txt")
 
'For each line in the textfile...
Do While Not objFile.AtEndOfStream 
	
	'...get the computername...
	strComputer = objFile.ReadLine
	
	'...connect to wmi...
	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
	
	'...query all removable disks...
	Set colItems = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk WHERE DriveType = '2'")
 
	'and Echo each disk which is connected
	For Each objItem in colItems
		WScript.Echo "On the computer " &strComputer &" there is connected a removable disk - drive letter is: " & objItem.Name
	Next
	
Loop

Open in new window

0
 

Author Comment

by:dgore1
ID: 24166929
I haven't tried the second part of the script but here is what I found...I changed my script to look like the example below, but I have 2 problems:

First I made sure no USB devices were connected.  The script ran just fine but did not display a message saying NO USB devices found.

Then I connected a USB device which it found on the correct drive letter of G...but for some reason it shows that a USB device is connected to Drive A which is my floppy!!

any ideas?
thanks,
dale

On Error Resume Next 'If error, resume next
 
strComputer = InputBox("Enter the name of the computer to Search for USB devices:") 
 
 
'query the WMI service for the computer name
 
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
   
 'display the connected computer   
 WScript.Echo "You have connected to computer " & strComputer 
 
Set colItems = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk WHERE DriveType = '2'")
 
For Each objItem in colItems
	WScript.Echo "The computer " &strComputer &" has a removable USB drive connected with drive letter: " & objItem.Name
	WScript.Echo " Please remove this device from " &strComputer &" to comply with Department policies " 
Next

Open in new window

0
 

Author Comment

by:dgore1
ID: 24167277
By the way, I realize that A is considered a removeable drive, but it had no floppy in the drive when I ran the software, so it should not have reported the drive letter...I think...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:merowinger
ID: 24179097
OK now i have excluded the floppies
On Error Resume Next 'If error, resume Next
 
bolDeviceFound = False 
strComputer = InputBox("Enter the name of the computer to Search for USB devices:") 
 
 
'query the WMI service for the computer name
 
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
   
 'display the connected computer   
 WScript.Echo "You have connected to computer " & strComputer 
 
Set colItems = objWMIService.ExecQuery ("SELECT * from Win32_LogicalDisk WHERE DriveType = 2 AND MediaType = 11")
 
For Each objItem in colItems
	WScript.Echo "The computer " &strComputer &" has a removable USB drive connected with drive letter: " & objItem.Name
	WScript.Echo " Please remove this device from " &strComputer &" to comply with Department policies " 
	bolDeviceFound = True
Next
 
If bolDeviceFound = False Then
	WScript.Echo "NO USB devices found"
End If

Open in new window

0
 

Author Comment

by:dgore1
ID: 24184151
ran the script but it misses the USB drive completely.  Even if no drive is inserted, it echoes this line:

WScript.Echo " Please remove this device from " &strComputer &" to comply with Department policies " 

If I take the MediaType out, it finds the USB device....if I remove the USB device, it never process the if bolDeviceFound to indicate no device found...

any ideas?
0
 
LVL 31

Expert Comment

by:merowinger
ID: 24186518
ok strange :)
Could you please run the below listed script and post the output here....we need to determine you usb disk configuration!
strServer = "."
 
Set objWMI = GetObject("winmgmts://" & strServer & "/root\cimv2")
Set objInstances = objWMI.InstancesOf("Win32_LogicalDisk",48)
 
On Error Resume Next
For Each objInstance in objInstances
    With objInstance
        WScript.Echo .Access
        WScript.Echo .Availability
        WScript.Echo .BlockSize
        WScript.Echo .Caption
        WScript.Echo .Compressed
        WScript.Echo .ConfigManagerErrorCode
        WScript.Echo .ConfigManagerUserConfig
        WScript.Echo .CreationClassName
        WScript.Echo .Description
        WScript.Echo .DeviceID
        WScript.Echo .DriveType
        WScript.Echo .ErrorCleared
        WScript.Echo .ErrorDescription
        WScript.Echo .ErrorMethodology
        WScript.Echo .FileSystem
        WScript.Echo .FreeSpace
        WScript.Echo .InstallDate
        WScript.Echo .LastErrorCode
        WScript.Echo .MaximumComponentLength
        WScript.Echo .MediaType
        WScript.Echo .Name
        WScript.Echo .NumberOfBlocks
        WScript.Echo .PNPDeviceID
        WScript.Echo Join(.PowerManagementCapabilities, ", ")
        WScript.Echo .PowerManagementSupported
        WScript.Echo .ProviderName
        WScript.Echo .Purpose
        WScript.Echo .QuotasDisabled
        WScript.Echo .QuotasIncomplete
        WScript.Echo .QuotasRebuilding
        WScript.Echo .Size
        WScript.Echo .Status
        WScript.Echo .StatusInfo
        WScript.Echo .SupportsDiskQuotas
        WScript.Echo .SupportsFileBasedCompression
        WScript.Echo .SystemCreationClassName
        WScript.Echo .SystemName
        WScript.Echo .VolumeDirty
        WScript.Echo .VolumeName
        WScript.Echo .VolumeSerialNumber
    End With
On Error Goto 0
Next

Open in new window

0
 

Author Comment

by:dgore1
ID: 24193572
Ran the script but it only sends the information to the screen, no text file so I can post output too!!

Have to push OK about 20 times before I finally get a script error..

so, need the script above modified to print to text file so I can paste info :)
0
 
LVL 31

Expert Comment

by:merowinger
ID: 24193693
you could run it in command prompt with cscript.exe...but its ok i've customized it....
strServer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("D:\output.txt") 
Set objWMI = GetObject("winmgmts://" & strServer & "/root\cimv2")
Set objInstances = objWMI.InstancesOf("Win32_LogicalDisk",48)
 
For Each objInstance in objInstances
    With objInstance
    	On Error Resume Next
        objFile.Writeline .Availability
        objFile.Writeline .Caption
        objFile.Writeline .Description
        objFile.Writeline .DeviceID
        objFile.Writeline .DriveType
        objFile.Writeline .FileSystem
        objFile.Writeline .FreeSpace
        objFile.Writeline .MediaType
        objFile.Writeline .Name
        objFile.Writeline .PNPDeviceID
        objFile.Writeline .Size
        objFile.Writeline .Status
        objFile.Writeline .StatusInfo
        objFile.Writeline .SystemName
        objFile.Writeline .VolumeDirty
        objFile.Writeline .VolumeName
        objFile.Writeline .VolumeSerialNumber
        objFile.Writeline "************************"
    End With
On Error Goto 0
Next

Open in new window

0
 

Author Comment

by:dgore1
ID: 24203503
OK...output number 1 is without any USB device attached:

A:
3 1/2 Inch Floppy Drive
A:
2
5
A:
WSCI575C901
************************
C:
Local Fixed Disk
C:
3
NTFS
145128955904
12
C:
159940567040
WSCI575C901
False

ECC7507E
************************
D:
CD-ROM Disc
D:
5
CDFS
0
11
D:
533530624
WSCI575C901
False
GettingStarted
4B395FC3
************************
E:
Local Fixed Disk
E:
3
NTFS
206160699392
12
E:
250056704000
WSCI575C901
False
BACKUP
3656C94E
************************
M:
Network Connection
M:
4
NTFS
420467093504
0
M:
438481416192
WSCI575C901
User Data
9811E7EC
************************


OUTPUT # 2 is with a USB drive attached:

A:
3 1/2 Inch Floppy Drive
A:
2
5
A:
WSCI575C901
************************
C:
Local Fixed Disk
C:
3
NTFS
145128947712
12
C:
159940567040
WSCI575C901
False

ECC7507E
************************
D:
CD-ROM Disc
D:
5
CDFS
0
11
D:
533530624
WSCI575C901
False
GettingStarted
4B395FC3
************************
E:
Local Fixed Disk
E:
3
NTFS
206160699392
12
E:
250056704000
WSCI575C901
False
BACKUP
3656C94E
************************
F:
CD-ROM Disc
F:
5
CDFS
0
11
F:
6991872
WSCI575C901
False
U3 System
977A2C8C
************************
G:
Removable Disk
G:
2
FAT32
7442563072
G:
8025866240
WSCI575C901
True

34B2B852
************************
M:
Network Connection
M:
4
NTFS
420467093504
0
M:
438481416192
WSCI575C901
User Data
9811E7EC
************************
0
 
LVL 31

Accepted Solution

by:
merowinger earned 2000 total points
ID: 24203845
ok this should work....it queries all devices for the keyword "removable disk"
bolDeviceFound = False 
strComputer = InputBox("Enter the name of the computer to Search for USB devices:") 
 
 
'query the WMI service for the computer name
 
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
   
 'display the connected computer   
 WScript.Echo "You have connected to computer " & strComputer 
 
Set colItems = objWMIService.ExecQuery ("SELECT * from Win32_LogicalDisk WHERE Description = 'Removable Disk'")
 
For Each objItem in colItems
	WScript.Echo "The computer " &strComputer &" has a removable USB drive connected with drive letter: " & objItem.Name
	WScript.Echo " Please remove this device from " &strComputer &" to comply with Department policies " 
	bolDeviceFound = True
Next
 
If bolDeviceFound = False Then
	WScript.Echo "NO USB devices found"
End If

Open in new window

0
 

Author Comment

by:dgore1
ID: 24204768
wow...works great!!!

this is fantastic!!!
0
 

Author Closing Comment

by:dgore1
ID: 31571039
this works great!!
0
 

Author Comment

by:dgore1
ID: 24204841
meant to ask you what does bolDeviceFound do exactly?
0
 
LVL 31

Expert Comment

by:merowinger
ID: 24204875
On startup its set to false
If a USB device is found, then it will be set to true
At the end of the script it is gettings analyzed and if its still false, there is no usb device
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question