Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if any USB devices connected on Logout?? 1

Status
Not open for further replies.

Sharpharp

IS-IT--Management
Jun 9, 2009
29
0
0
GB
Hi all,

I've been tasked to find out if there is a way of warning users that they have left a USB device or memory card left inserted in a USB card reader, when the USER logs out.

We have a lot of users to leave usb memory sticks plugged in or cards in the usb card readers. This is in a public PC area, so I was wondering if its possible to build a VBScript that runs on Logout that can detect if any USB Memory Sticks or Cards are left inserted on a PC.

Any help would be appreciated, i've googled a few articles, but couldn't find anything on the subject.

Thanks in advance
Sharpy
 
The script by markdmac in thread329-1547420 might be a good script for you to start having a look at.

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I love this idea. Can't tell you how many times I have left my USB drive behind.

Code:
strComputer = "."
On Error Resume Next
intCount = 0
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_DiskDrive WHERE InterfaceType='USB'",,48)
For Each drive In colItems
	intCount = intCount + 1
Next
If intCount > 0 Then
	MsgBox "Don't forget to remove your thumb drive!"
End If

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks for your replies.

markdmac, I've tried your script, but it pops up a message regardless of whether a USB device is connected or not.

We have Internal USB Card readers connected to the PCs, so i think your script seeing that as a USB device.

Can the scripy be changed to only recognise EXTERNALLY conncted USB Devices, or ignore internal devices which are permenantly connected?

I apprecuate your help, if i can crack this dilemma I would be well pleased

Cheers
 
The card readers must be throwing it off because I tested that script fully.

Try running this to find out what it is detecting:
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_DiskDrive WHERE InterfaceType='USB'",,48)
For Each drive In colItems
    MsgBox drive.name & vbCrLf & drive.mediaType
Next

Report back and we can code to ignore those. Make sure the name is the same on all of them.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Hi markdmac,

Ran your detection script, and its reporting:

\\.\PHYSICALDRIVE1
\\.\PHYSICALDRIVE3
\\.\PHYSICALDRIVE4
\\.\PHYSICALDRIVE2

Cheers
 
Also, I don't want the script to ignore the Card Reader, because a User may potentially have left a Memory card (SD/MMC etc etc) in the Card Reader, so I would ideally need the script to detect if any card is also left inserted (as well as the usual external USB sticks)
 
So what are those drives in reality? Need you to help identify them more.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
I found a system with USB SD Card readers. Even with nothing in them the drives were detected. So I dug a little deeper and found that the mediaType was not populated unless there was actually media in the drive. Armed with that I have tweaked the code for you.

Code:
strComputer = "."
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_DiskDrive WHERE InterfaceType='USB'",,48)
intCount = 0
For Each drive In colItems
	If drive.mediaType <> "" Then
    	intCount = intCount + 1
    End If
Next
If intCount > 0 Then
    MsgBox "Don't forget to remove your thumb drive or Smart Card!"
End If

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Hi mark,

Thats superb, I've tried it on a Dell 755 with Internal USB card reader, and the script pops up a message when an SD card was left inserted, and also works great with a USb memory stick attached.

I'm aiming to put this as a Logout Script in Active Directory, so when a user logs out they get this message.

I tried it on a few select pc's, and noticed users have logged out and walked away from the pc before it logged out.
So was wondering whether we could introduce a sound using the system speaker "beep" to grab their attention??? The sound is disabled on the pc's, so either a system beep or flashing graphics to grab the attention perhaps?

Also, is it possible to slightly tweak the message box to have a countdown counter of say 10 seconds, before it closes? - This is so that if the user logs out and walks away, the message box won't prevent the pc from logging off.

By the way, I really appreciate your continuing help with this, so many thanks.

 
Would this line added after the "If intCount > 0 Then" statement work? - Any ideas how to make the beep last longer or loop about 3 times?

set wshShell = Wscript.CreateObject("wscript.Shell")
beep = chr(007)
WshShell.Run "cmd /c @echo " & beep, 0
 
You could certainly add that code to make a beep, I really like that idea. The only way you could add the time effect that I know of would be to launch an IE window, but that will totally hold up the logoff process.

There comes a point where the technology can't replace user training. People need to wait for their session to logoff before leaving a PC.

With your beep added:
Code:
strComputer = "."
On Error Resume Next
Set WshShell = CreateObject("Wscript.Shell")
beep = chr(007)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_DiskDrive WHERE InterfaceType='USB'",,48)
intCount = 0
For Each drive In colItems
	If drive.mediaType <> "" Then
    	intCount = intCount + 1
    End If
Next
If intCount > 0 Then
	WshShell.Run "cmd /c @echo " & beep, 0 
    MsgBox "Don't forget to remove your thumb drive!"
End If

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thats spot on Mark, great work.

The message box -i'll have a look at that and see if I can add a 10-sec countdown timer to it.

Just one last thing i could really do with if you can help, is that is there anyway of encorporating a check to see if a CD/DVD disk has also been left in the DVD-drive? Its not a USB Device (might be sata) (The PC is a Dell 745 or Dell 755), but using your mediatype not populated idea, is it possible to do a check for that as well?

That way, we can ensure that the user has not left any device connected or left in the card reader, usb or dvd drive???

Many thanks
 
This script is part of my Admin Script Pack. It checks the CD drive and it will eject if a disk is present.

Code:
[green]'==========================================================================
'
' NAME: CheckCDStatusEject.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 5/1/2009
' COPYRIGHT © 2009, All Rights Reserved
'
' COMMENT: Ejects the CD if empty or if present. Supports multiple CD/DVD ROM Drives.
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'[red]This script and many more can be found in the Admin Script Pack 
'[URL unfurl="true"]http://www.thespidersparlor.com/vbscript[/URL][/red]
'==========================================================================[/green]
On Error Resume Next
Const CDROM = 4
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")
For Each objItem in colItems[green]
	'Eject if a CD is left in the drive[/green]
    If objItem.MediaLoaded = True Then[green]
    'Use the below line instead of above to eject if empty
    'If objItem.MediaLoaded = False Then[/green]
    	Eject objItem.Drive & "\"
	End If
Next

Sub Eject(CDROM)
  Dim ssfDrives 
  ssfDrives = 17
  CreateObject("Shell.Application")_
  .Namespace(ssfDrives).ParseName(CDROM).InvokeVerb("E&ject")
End Sub

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Mark,

Very helpful post, particularly the way you responded to additional requirements. Have a star!

Rick
 
Hi there,

Tried the Eject CD script on its own, but it doesn't eject the CD/DVD. I've put a few wscript.echo commands into the script and it runs through the script, but just doesn't eject the CD.

I have a CD in the drive, when i run the script, the cd spins up, and i also get the HELLO and HELLO2 messages, but the CD/DVD drive won't eject. Anything missing mark?

Thanks

On Error Resume Next
Const CDROM = 4
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")
For Each objItem in colItems
'Eject if a CD is left in the drive
If objItem.MediaLoaded = True Then
wscript.echo "Hello"
'Use the below line instead of above to eject if empty
'If objItem.MediaLoaded = False Then
Eject objItem.Drive & "\"
End If
Next

wscript.echo "Hello2"

Sub Eject(CDROM)
Dim ssfDrives
ssfDrives = 17
CreateObject("Shell.Application")_
.Namespace(ssfDrives).ParseName(CDROM).InvokeVerb("E&ject")
End Sub
 
Ok,

Managed to get it working so it will eject the CD, here is the modified script for anyone else needing it :)

On Error Resume Next
Const CDROM = 4
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")
For Each objItem in colItems
'Eject if a CD is left in the drive
If objItem.MediaLoaded = True Then

Dim WMP: Set WMP = CreateObject("WMPlayer.ocx")
Dim colCDROMS: Set colCDROMS = WMP.CDROMCollection

If colCDROMS.Count > -1 Then
For i = 0 to colCDROMS.Count - 1
colCDROMS.Item(i).Eject
Next
End If

'Use the below line instead of above to eject if empty
'If objItem.MediaLoaded = False Then
Eject objItem.Drive & "\"
End If
Next

Sub Eject(CDROM)
Dim ssfDrives
ssfDrives = 17
CreateObject("Shell.Application")_
.Namespace(ssfDrives).ParseName(CDROM).InvokeVerb("E&ject")
End Sub
 
Aside from having an extra End If in your posted code, from what I can see it will eject all CD/DVDs regardless of having a drive in or not.

My original code does not appear to work with DVD drives, so thanks for pointing that out. I've written the following update that will only eject drives with media in them. This will also close the drive as well after pausing to allow the user to remove the media.

Code:
On Error Resume Next
dim oWMP, colCDROMs, i, strComputer, objWMIService, colItems, objItem

Const CDROM = 4
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")
For Each objItem in colItems
    If objItem.MediaLoaded <> 0 Then
    'Use the below line instead of above to eject if empty
    'If objItem.MediaLoaded = 0 Then
        Eject objItem.Drive 
    End If
Next

Sub Eject(Drive)
 Set oWMP = Wscript.CreateObject("WMPlayer.OCX.7")
 Set colCDROMs = oWMP.cdromCollection

 if colCDROMs.Count >= 1 then

 For i = 0 to colCDROMs.Count - 1
 	If oWMP.cdromCollection.item(index).driveSpecifier = Drive Then
		colCDROMs.Item(i).Eject : colCDROMs.Item(i).Eject
	End If
 Next

 End If
 WScript.Sleep 7000
 oWMP.close
 set colCDROMs = nothing
 set oWMP = nothing
 set i = nothing
End Sub

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Mark,

Tried that, and the script does indeed eject a DVD, however the tray closes as soon as it as opened fully, without any pause.

I've tried altering the sleep time, but no joy..

Sharpy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top