This is a mirror of official site: http://jasper-net.blogspot.com/

Hey, Scripting Guy! How Can I Be Notified When a USB Drive Is Plugged into My Computer?

| Sunday, May 2, 2010
Hey, Scripting Guy! I would like to be notified when I plug in a USB drive into my computer. I have some cool ideas for using that, but I do not know how to detect when a USB drive is plugged in. I know I can run a script that loops through looking for all drives, and compares them with a previous collection of drives and then looks for differences. I also know I can use the Compare-Object cmdlet to make the comparison, so it would not be too terribly hard, but it seems inefficient. Is there a better way to do this?

-- SP

Hey, Scripting Guy! AnswerHello SP,

Microsoft Scripting Guy Ed Wilson here. We are getting closer and closer to the 2010 Scripting Games. They begin on April 26 and will run for two weeks. Registration has been open for a week or so, and we encourage you to sign up. It takes less than a minute because you can use your Windows Live ID. Craig has rounded up some really cool prizes (Scripting Editor: I don’t know who this Craig guy is, but he sounds really awesome), and some of the Windows PowerShell user groups are even offering additional prizes for their members who participate.

SP, what you need to do is investigate WMI events. We have lots of Hey, Scripting Guy! Blog posts that talk about using WMI events from within VBScript. We also have several Hey, Scripting Guy! posts that deal with WMI events from Windows PowerShell.

There are two types of WMI event classes that can be used. The first are intrinsic event classes, which are WMI classes that are designed to monitor for events. All these classes do is monitor for WMI events. An example of an intrinsic WMI event class is Win32_ProcessStartTrace. Because this is an intrinsic WMI class, you cannot use the Get-WmiObject (gwmi alias) cmdlet and pass the WMI class to the Get-Member (gm alias) cmdlet and see its properties and methods. An error is generated that states that no object has been supplied to the Get-Member cmdlet. This is true because the Win32_ProcessStartTrace WMI class is created when a new process is generated and WMI is configured to watch for new processes to start up.

PS C:\> gwmi win32_processStartTrace | gm
Get-Member : No object has been specified to the Get-Member cmdlet.
At line:1 char:34
+ gwmi win32_processStartTrace | gm <<<<
   + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationException
   + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

PS C:\>

To use a “regular” WMI class to monitor for WMI events, it is necessary to use a generic WMI event class such as __InstanceCreationEvent. The thing that is cool about using the __InstanceCreationEvent WMI class is that it returns a complete copy of the WMI object that created the event.

Read more: Hey, Scripting Guy!

Posted via email from jasper22's posterous

0 comments: