Advertisement
Guest User

WiFi - Lan Adapter Controller Script - VBScript

a guest
Sep 12th, 2013
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.83 KB | None | 0 0
  1.     '*********************************************
  2.     '* Net Switcher - Watches one network card   *
  3.     '* for connectivity, and toggles another     *
  4.     '* http://www.intelliadmin.com               *
  5.     '*********************************************
  6.  
  7.     Sub EnableAdapter( sAdapterName, bStatus )
  8.         Dim objWMIService, colItems
  9.         Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  10.         Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
  11.         For Each objItem in colItems
  12.             if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
  13.                 if (bStatus) then
  14.                     objItem.Enable
  15.                 else       
  16.                     objItem.Disable
  17.                 end if
  18.             end if
  19.         Next
  20.     End Sub
  21.    
  22.    
  23.     Function GetNetworkAdapterNames_LAN()
  24.         Dim sConnexionLan, objWMIService
  25.         Dim aConnexionLan
  26.         Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  27.         REM WScript.Echo ("GetNetworkAdapterNames_LAN")
  28.         Set aConnexionLan = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID like '%local'",,48)
  29.         For Each objItem in aConnexionLan
  30.             REM WScript.Echo ( "NetConnectionID : " & objItem.NetConnectionID )
  31.             sConnexionLan = objItem.NetConnectionID
  32.         Next
  33.     GetNetworkAdapterNames_LAN = sConnexionLan
  34.     End Function
  35.    
  36.    
  37.     Function GetNetworkAdapterNames_WiFi()
  38.         Dim sConnexionWifi, objWMIService
  39.         Dim aConnexionWifi 'As New List(Of String)
  40.         Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  41.         REM WScript.Echo ("GetNetworkAdapterNames_WiFi")    
  42.         Set aConnexionWifi = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID like '%sans%'",,48)
  43.         For Each objItem in aConnexionWifi
  44.             REM WScript.Echo ( "NetConnectionID : " & objItem.NetConnectionID )
  45.             sConnexionWifi = objItem.NetConnectionID
  46.         Next
  47.     GetNetworkAdapterNames_WiFi = sConnexionWifi
  48.     End Function
  49.    
  50.     Function AdapterStatus( sAdapterName )
  51.         Dim objWMIService, colItems
  52.         Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  53.         Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
  54.         REM WScript.Echo ("AdapterStatus")
  55.         AdapterStatus = false
  56.         For Each objItem in colItems
  57.             if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
  58.                 AdapterStatus = (objItem.NetConnectionStatus=2)
  59.             end if
  60.         Next
  61.     End Function
  62.    
  63.     Function AdapterExists( sAdapterName )
  64.         Dim objWMIService, colItems
  65.         Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  66.         Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
  67.         REM WScript.Echo ("AdapterExists")
  68.         AdapterExists = false
  69.         For Each objItem in colItems
  70.             if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
  71.                 AdapterExists = True
  72.             end if
  73.         Next
  74.     End Function
  75.    
  76.    
  77.     Dim bCurrentStatus
  78.     Dim bChanged
  79.     Dim sWatchNetworkCard
  80.     Dim sSwitchNetworkCard
  81.    
  82.     REM if (WScript.Arguments.Count<2) then
  83.         REM WScript.Echo "*********************************"
  84.         REM WScript.Echo "* IntelliAdmin Net Switcher     *"
  85.         REM WScript.Echo "* http://www.intelliadmin.com   *"
  86.         REM WScript.Echo "*********************************"
  87.         REM WScript.Echo vbCrLf & "Usage: " & vbCRLF
  88.         REM WScript.Echo " NetSwitch.vbs <Card To Watch> <Card To Switch>"
  89.         REM WScript.Echo vbCrLF & "Explanation: " & vbCRLF
  90.         REM WScript.Echo " Net Switcher can be used to make sure your"
  91.         REM WScript.Echo " wireless card is only enabled when no "
  92.         REM WScript.Echo " ethernet connection is available"
  93.         REM WScript.Echo vbCrLF & "Example: " & vbCRLF
  94.         REM WScript.Echo " NetSwitch.vbs " & chr(34) & "Local Area Connection" & chr(34) & " " & _
  95.             REM chr(34) & "Wireless Connection" & Chr(34)
  96.         REM WScript.Quit
  97.        
  98.     REM end if
  99.    
  100.     Dim sWatch, sSwitch
  101.    
  102.     'sWatchNetworkCard = WScript.Arguments(0)
  103.     'sSwitchNetworkCard = WScript.Arguments(1)
  104.     sWatchNetworkCard = GetNetworkAdapterNames_LAN
  105.     sSwitchNetworkCard = GetNetworkAdapterNames_WiFi
  106.     REM WScript.Echo ("GetNetworkAdapterNames_Lan & WiFi")
  107.     REM GetNetworkAdapterNames_LAN()
  108.     REM GetNetworkAdapterNames_WiFi()
  109.    
  110.     if (Not(AdapterExists(sWatchNetworkCard))) then
  111.      WScript.Echo "Error: Could not find the adapter (" & sWatchNetworkCard & ")"
  112.      WScript.Quit
  113.      REM WScript.Echo ("if_AdapterExists_Watch")
  114.     end if
  115.    
  116.     if (Not(AdapterExists(sSwitchNetworkCard))) then
  117.      WScript.Echo "Error: Could not find the adapter (" & sSwitchNetworkCard & ")"
  118.      WScript.Quit
  119.      REM WScript.Echo ("ifAdapterExists_Switch")
  120.     end if
  121.    
  122.     bChanged=TRUE
  123.    
  124.     while(True)
  125.     REM WScript.Echo ("main")
  126.         if (bChanged) then
  127.             bChanged=FALSE
  128.             bCurrentStatus = AdapterStatus(sWatchNetworkCard)
  129.             if (bCurrentStatus) then
  130.                 EnableAdapter sSwitchNetworkCard,False
  131.             else
  132.                 EnableAdapter sSwitchNetworkCard,True
  133.             end if
  134.         end if
  135.         WScript.Sleep(1000)
  136.         if (bCurrentStatus<>AdapterStatus(sWatchNetworkCard)) then
  137.             bChanged=TRUE
  138.         end if
  139.     wend
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement