How to connect to a network using VBScript

Using VBScript, we can easily work with automating network connections such as: Connect to wifi , Disable a firewall etc.

VBScript comes with inbuilt capability to handle network connections and so many other options in windows extremely well that you will be amazed..

In this page we will have a complete solution of disable network, connect network, disconnect a network, close a firewall using vbscript. 

How to connect to a network using VBScript – Example code
'The following code can be used to connect disable and enable network (name may need to be modified according to the actual conduct):

Const ssfCONTROLS = 3
sConnectionName = "The wireless network connection"

sEnableVerb = "Enable(&A)"
sDisableVerb = "Disable(&B)"

set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)

set oNetConnections = nothing
for each folderitem in oControlPanel.items
 if folderitem.name = "The network connection" then
  set oNetConnections = folderitem.getfolder: exit for
 end if
next

if oNetConnections is nothing then
 msgbox "Network and dial up Connections folder was not found"
 wscript.quit
end if

set oLanConnection = nothing
for each folderitem in oNetConnections.items
 if lcase(folderitem.name) = lcase(sConnectionName) then
  set oLanConnection = folderitem: exit for
 end if
next

if oLanConnection is nothing then
 msgbox "Not found '" & sConnectionName & "' item"
 wscript.quit
end if

bEnabled = true
set oEnableVerb = nothing
set oDisableVerb = nothing
s = "Verbs: " & vbcrlf
for each verb in oLanConnection.verbs
 s = s & vbcrlf & verb.name
 if verb.name = sEnableVerb then
  set oEnableVerb = verb
  bEnabled = false
 end if
 if verb.name = sDisableVerb then
  set oDisableVerb = verb
 end if
next


if bEnabled then
' oLanConnection.invokeverb sDisableVerb
oDisableVerb.DoIt
else
' oLanConnection.invokeverb sEnableVerb
oEnableVerb.DoIt
end if


'
wscript.sleep 400

You may also read

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.