Salut les programmeurs ; )
j'ai besoin de l'aide d'une âme charitable car je ne connais rien en programmation et script . j'ai bien essaye un petit peu mais c'est trop compliqué pour mon ptit cerveau : (
alors je souhaiterai un script qui permet de désactiver/activer automatiquement la carte wifi lorsque un rj45 est branché/débranché du pc.
je suis passé par quelques entreprises ou ils ont cette fonction sur les pc pour soulager les bornes wifi et je souhaiterai mettre en place cette fonction dans une petite entreprise dont le controleur de domaine tourne sous du 2008 server et ne permet donc pas de creer cette GPO...(je crois que c'est possible qu'a partir d'un win 2012 ou 2016)
donc j'ai cherché avec mon amis google mais y a pas foule de resultat pour automatiser cette fonction sur un petit domaine.
j'ai trouvé ce script https://gallery.technet.microsoft.co...contenthttp:// qui semble fonctionner pour certains et pas du tout pour d'autres.
du coup j'ai essaye de faire quelques modif comme indiqué dans les quelques commentaires mais sans plus de résultat.(pour info les pc tournent sous w7 (fr) )
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 '========================================================================== ' ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2009 ' ' NAME: ' ' AUTHOR: Mohammed Alyafae , ' DATE : 9/12/2011 ' ' COMMENT: this script disable wireless connection if the LAN is Connected ' and vice versa '========================================================================== 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
donc j'en suis arrivé ici1°) I check my script encoding to ensure accented characters are correctly interpreted (ANSI from my side)
2°) I change the WMI request :
Set colLAN = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID like 'Connexion au réseau local' and PhysicalAdapter='True'" )
3° I replaced strDisable value by "&Désactiver"
4° I replaced strEnable by "&Activer"
All work fine. Thank you Mohammed.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 '========================================================================== '========================================================================== ' ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2009 ' ' NAME: ' ' AUTHOR: Mohammed Alyafae , ' DATE : 9/12/2011 ' ' COMMENT: this script disable wireless connection if the LAN is Connected ' and vice versa '========================================================================== 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 'Connexion au réseau local' 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 = "&Désactiver" 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 = "&Activer" 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
quelqu'un veut bien m'aider un peu ou m'aiguiller sur la direction a prendre ?
grand merci d'avance messieurs
Partager