Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '*********************************************
- '* Net Switcher - Watches one network card *
- '* for connectivity, and toggles another *
- '* http://www.intelliadmin.com *
- '*********************************************
- Sub EnableAdapter( sAdapterName, bStatus )
- Dim objWMIService, colItems
- Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
- Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
- For Each objItem in colItems
- if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
- if (bStatus) then
- objItem.Enable
- else
- objItem.Disable
- end if
- end if
- Next
- End Sub
- Function GetNetworkAdapterNames_LAN()
- Dim sConnexionLan, objWMIService
- Dim aConnexionLan
- Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
- REM WScript.Echo ("GetNetworkAdapterNames_LAN")
- Set aConnexionLan = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID like '%local'",,48)
- For Each objItem in aConnexionLan
- REM WScript.Echo ( "NetConnectionID : " & objItem.NetConnectionID )
- sConnexionLan = objItem.NetConnectionID
- Next
- GetNetworkAdapterNames_LAN = sConnexionLan
- End Function
- Function GetNetworkAdapterNames_WiFi()
- Dim sConnexionWifi, objWMIService
- Dim aConnexionWifi 'As New List(Of String)
- Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
- REM WScript.Echo ("GetNetworkAdapterNames_WiFi")
- Set aConnexionWifi = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID like '%sans%'",,48)
- For Each objItem in aConnexionWifi
- REM WScript.Echo ( "NetConnectionID : " & objItem.NetConnectionID )
- sConnexionWifi = objItem.NetConnectionID
- Next
- GetNetworkAdapterNames_WiFi = sConnexionWifi
- End Function
- Function AdapterStatus( sAdapterName )
- Dim objWMIService, colItems
- Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
- Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
- REM WScript.Echo ("AdapterStatus")
- AdapterStatus = false
- For Each objItem in colItems
- if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
- AdapterStatus = (objItem.NetConnectionStatus=2)
- end if
- Next
- End Function
- Function AdapterExists( sAdapterName )
- Dim objWMIService, colItems
- Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
- Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
- REM WScript.Echo ("AdapterExists")
- AdapterExists = false
- For Each objItem in colItems
- if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
- AdapterExists = True
- end if
- Next
- End Function
- Dim bCurrentStatus
- Dim bChanged
- Dim sWatchNetworkCard
- Dim sSwitchNetworkCard
- REM if (WScript.Arguments.Count<2) then
- REM WScript.Echo "*********************************"
- REM WScript.Echo "* IntelliAdmin Net Switcher *"
- REM WScript.Echo "* http://www.intelliadmin.com *"
- REM WScript.Echo "*********************************"
- REM WScript.Echo vbCrLf & "Usage: " & vbCRLF
- REM WScript.Echo " NetSwitch.vbs <Card To Watch> <Card To Switch>"
- REM WScript.Echo vbCrLF & "Explanation: " & vbCRLF
- REM WScript.Echo " Net Switcher can be used to make sure your"
- REM WScript.Echo " wireless card is only enabled when no "
- REM WScript.Echo " ethernet connection is available"
- REM WScript.Echo vbCrLF & "Example: " & vbCRLF
- REM WScript.Echo " NetSwitch.vbs " & chr(34) & "Local Area Connection" & chr(34) & " " & _
- REM chr(34) & "Wireless Connection" & Chr(34)
- REM WScript.Quit
- REM end if
- Dim sWatch, sSwitch
- 'sWatchNetworkCard = WScript.Arguments(0)
- 'sSwitchNetworkCard = WScript.Arguments(1)
- sWatchNetworkCard = GetNetworkAdapterNames_LAN
- sSwitchNetworkCard = GetNetworkAdapterNames_WiFi
- REM WScript.Echo ("GetNetworkAdapterNames_Lan & WiFi")
- REM GetNetworkAdapterNames_LAN()
- REM GetNetworkAdapterNames_WiFi()
- if (Not(AdapterExists(sWatchNetworkCard))) then
- WScript.Echo "Error: Could not find the adapter (" & sWatchNetworkCard & ")"
- WScript.Quit
- REM WScript.Echo ("if_AdapterExists_Watch")
- end if
- if (Not(AdapterExists(sSwitchNetworkCard))) then
- WScript.Echo "Error: Could not find the adapter (" & sSwitchNetworkCard & ")"
- WScript.Quit
- REM WScript.Echo ("ifAdapterExists_Switch")
- end if
- bChanged=TRUE
- while(True)
- REM WScript.Echo ("main")
- if (bChanged) then
- bChanged=FALSE
- bCurrentStatus = AdapterStatus(sWatchNetworkCard)
- if (bCurrentStatus) then
- EnableAdapter sSwitchNetworkCard,False
- else
- EnableAdapter sSwitchNetworkCard,True
- end if
- end if
- WScript.Sleep(1000)
- if (bCurrentStatus<>AdapterStatus(sWatchNetworkCard)) then
- bChanged=TRUE
- end if
- wend
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement