Automatically Disabling Wifi on LAN Connection with Task Scheduler

Last Updated on

By default windows doesn’t manage multi-homed systems in the most organized fashion.  When working with a laptop I always prefer to use a LAN connection when my machine is docked….sure Lenovo and some other OEMs offer a utility to do this, but these utilties can be flaky and a bit difficult to configure.  This script can be triggered via task scheduler to disable wifi on the presence of a LAN connection and vice versa.

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
On Error Resume Next
Dim strComputer
Dim objWMIService
Dim colLAN
Dim objWifi,objLAN
Dim state
Dim wireStatus
Dim wifiStatus

state=""
wireStatus=""
wifiStatus=""

Do While True

strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\CIMV2")
Set colLAN = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID like 'Local Area Connection' and PhysicalAdapter='True'" )
Set colWiFi=objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionID =" & "'" &GetWirlessName & "'" & "and PhysicalAdapter='True' ")

For Each objWifi In colWiFi
If objWifi.Netconnectionstatus=2 Then
wifiStatus=True
Else
wifiStatus=False
End If
Next

For Each objLAN in colLAN

If objLAN.Netconnectionstatus=2 Then
wireStatus=True
state=False ' this is very importnat variable to determine when to enable or disbale wireless connection
Else
wireStatus=False
End If
Next

If True Then
If state <> False Then
If wifiStatus = False Then
EnableWireless GetWirlessName
End If
Else
If wifiStatus = True Then
DisableWireless GetWirlessName
End If
End If

End If

state=""
wireStatus=""
wifiStatus=""

WScript.Sleep 60000

Loop

' Function to get wireless adapter name from the registery
Function GetWirlessName

Dim strKeyPath
Dim strComputer
Dim objReg
Dim arrSubKeys
Dim SubKey
Dim strValueName
Dim dwValue
Dim strValue
Const HKLM=&H80000002

strKeyPath="SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
strComputer="."

Set objReg=GetObject("winmgmts:\" & strComputer & "\root\default:StdRegProv")
objReg.Enumkey HKLM ,strKeyPath,arrSubKeys

For Each SubKey In arrSubKeys
strValueName="MediaSubType"
objReg.GetDWORDValue HKLM,strKeyPath & "" & subkey & "" & "Connection" ,strValueName,dwValue
If dwValue=2 Then
strValueName = "Name"
objReg.GetStringValue HKLM,strKeyPath & "" & subkey & "" & "Connection" ,strValueName,strValue
Exit For
End If
Next

GetWirlessName=strValue

End Function

' Subroutine to disable wireless connection
Sub DisableWireless (strNetConn)

Dim oConnections
dim objShell
Dim objConnections,objConn
Dim strDisable
Dim objNetwork
Dim objDisable
Dim objVerb

Const NETWORK_CONNECTIONS = &H31&

strDisable = "Disa&ble"

Set objShell = CreateObject("Shell.Application")
Set objConnections = objShell.Namespace(NETWORK_CONNECTIONS)

For Each objConn In objConnections.Items
If objConn.Name = strNetConn Then
Set objNetwork = objConn
Exit For
End If
Next
Set objDisable = Nothing

For Each objVerb in objNetwork.verbs
If objVerb.name = strDisable Then
Set objDisable = objVerb
Exit For
End If

Next
objDisable.DoIt
WScript.Sleep 1000
End Sub

'Function to enable wireless connection , you can combone these two subtoutines into one
' but I prefer to seperate them just for simplicity
Sub EnableWireless (strNetConn)
Dim oConnections
dim objShell
Dim objConnections,objConn
Dim strEnable
Dim objNetwork
Dim objEnable
Dim objVerb

Const NETWORK_CONNECTIONS = &H31&

strEnable = "En&able"

Set objShell = CreateObject("Shell.Application")
Set objConnections = objShell.Namespace(NETWORK_CONNECTIONS)

For Each objConn In objConnections.Items
If objConn.Name = strNetConn Then
Set objNetwork = objConn
Exit For
End If
Next
Set objEnable = Nothing

' Enable NIC
For Each objVerb in objNetwork.verbs
If objVerb.name = strEnable Then
Set objEnable = objVerb
Exit For
End If

Next

objEnable.DoIt
WScript.Sleep 1000
End Sub
Lima

About the author

Lima is the visual nautical indicator for "stop instantly."

Leave a Reply