Wednesday, January 27, 2010

As part of a job interview, I was asked to program a VB script.

The goal was to:
"Disable LAN,WAN,Bluetooth devices as soon as any user (except shopAdmin) makes a login to the Computer (VB).
Hint this is only possible with the extra software piece called Devcon"


How do I start?
  1. search on the internet to see if someone else did it. If I judge it's well done, why reinvent what has already been invented?
  2. search in my scripts if I've already done it or part of it. Most likely, I will reuse generic scripts I made.

In this case, the "hint" meant that since a command line program would be used, some string parsing would be involved. A few Google searches turned up some ideas but nothing as complete as what was requested.

'Assuming devcon is the WINDOWS folder
'Assuming the logged on user has the right to disable devices.

LANadapter = "Broadcom Loca Network Adapter"
BTadapter = "BT adapter"
WLANadapter = "Dell Wireless 1470 Dual Band WLAN Mini-PCI Card"

Set objShell = WScript.CreateObject("WScript.Shell")

Set objNet = CreateObject("WScript.NetWork")

If Not objNet.UserName = "shopAdmin" Then
Set objExecObject = objShell.Exec("cmd /c devcon listclass Net")

Do While Not objExecObject.StdOut.AtEndOfStream

strText = objExecObject.StdOut.ReadLine()

If Instr(strText,LANadapter)>0 Then
ID = Split(strText,"\",3)
Set objExecObject = objShell.Exec("cmd /c devcon.exe disable " & ID(0) & "\" & ID(1) )
WScript.Echo "LAN disabled"

ElseIf Instr(strText,BTadapter )>0 Then
ID = Split(strText,"\",3)
Set objExecObject = objShell.Exec("cmd /c devcon.exe disable " & ID(0) & "\" & ID(1) )
WScript.Echo "Bluetooth disabled"

ElseIf Instr(strText,WLANadapter)>0 Then
ID = Split(strText,"\",3)
Set objExecObject = objShell.Exec("cmd /c devcon.exe disable " & ID(0) & "\" & ID(1) )
WScript.Echo "WLAN disabled"
End If

Loop
End If