Powershell and VBScript Comparision... USB Monitor

Here was a little challenge....

I needed to write a script which i could invoke and leave run in the background and what it will do is monitor if a usb device is plugged in and out and alert you... the big thing for me is i needed to do this with no external code it all had to be within the platform

they are not perfect by any means but i think its a nice comparision on how you can achieve something in powershell and vbscript... personally i found the powershell one easier it took me about 10 mins to acheive similar results. in vbscript it took me a while longer :) but thats because i am weak with vbscript at best...

 

Powershell script

 

cls
[array]$wmiquery = $null
$x = 0

 

while ($x -ne 1 )
{
 
 $wmiquery = get-wmiobject Win32_PNPENtity | where {$_.Description -like "*usb mass storage device"}
 
 
  if ($wmiquery -ne $null)
  {
   
   foreach ($w in $wmiquery)
   {
    if ($msg -eq 1)
    {
     #this is meant to have no code :) 
    }
    else
    {
     Write-Host ("Please Talk to IT about USB Device")
     $msg = 1
    }
    
   }
   
  }
  else
  {
   $msg = 0
  }
 

}

 

VBScript

'Forcing Good Practice :)

option explicit

'Here are some of the variables used in the script
DIM objwmiservice, objitem, colitems,strcomputer,colstr,colstr1,collecteditem,index,msgdisplayed,test

'Initialize the relevant variables
index = 1
msgdisplayed = 0
strcomputer = "."
colstr = "USB Mass Storage Device"

'Main Script - Never Ending Loop
do

 
 'Set the WMI Path and Collection the information required, we parse on USB MAss Storage Device

 set objwmiservice = getobject ("winmgmts:\\" & strcomputer & "\root\cimv2")
 set colitems = objwmiservice.execquery( _
  "Select * From Win32_PNPEntity where Name = 'USB Mass Storage Device' ")
 
 
    
   'Test what we have collected, if we have displayed the message once dont display again..

   for each collecteditem in colitems
    
    
    if msgdisplayed = 0 then
     
         
     colstr1 = collecteditem.description

     if colstr1 = colstr then

      msgbox ("PLease talk to IT regarding your USB Storage Device")

      msgdisplayed = 1

     end if
    

    end if

   next

 
 'Wait 10 mins before clearing the display variable and prompting them again..
 
 wscript.sleep 600000 'in msec 10mins in real terms :)
 msgdisplayed = 0

Loop until index = 2