1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
   | 	'*********************************************
	'* 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 | 
Partager