Advertisement
opexxx

wireless1.vbs

May 5th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. '==========================================================================
  2. '
  3. ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2009
  4. '
  5. ' NAME:
  6. '
  7. ' AUTHOR: Mohammed Alyafae ,
  8. ' DATE : 9/12/2011
  9. '
  10. ' COMMENT: this script disable wireless connection if the LAN is Connected
  11. ' and vice versa
  12. '==========================================================================
  13. On Error Resume Next
  14. Dim strComputer
  15. Dim objWMIService
  16. Dim colLAN
  17. Dim objWifi,objLAN
  18. Dim state
  19. Dim wireStatus
  20. Dim wifiStatus
  21.  
  22. state=""
  23. wireStatus=""
  24. wifiStatus=""
  25.  
  26. Do While True
  27.  
  28. strComputer = "."
  29. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
  30. Set colLAN = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID like 'Local Area Connection' and PhysicalAdapter='True'" )
  31. Set colWiFi=objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionID =" & "'" &GetWirlessName & "'" & "and PhysicalAdapter='True' ")
  32.  
  33. For Each objWifi In colWiFi
  34. If objWifi.Netconnectionstatus=2 Then
  35. wifiStatus=True
  36. Else
  37. wifiStatus=False
  38. End If
  39. Next
  40.  
  41. For Each objLAN in colLAN
  42.  
  43. If objLAN.Netconnectionstatus=2 Then
  44. wireStatus=True
  45. state=False ' this is very importnat variable to determine when to enable or disbale wireless connection
  46. Else
  47. wireStatus=False
  48. End If
  49. Next
  50.  
  51.  
  52. If True Then
  53. If state <> False Then
  54. If wifiStatus = False Then
  55. EnableWireless GetWirlessName
  56. End If
  57. Else
  58. If wifiStatus = True Then
  59. DisableWireless GetWirlessName
  60. End If
  61. End If
  62.  
  63. End If
  64.  
  65.  
  66. state=""
  67. wireStatus=""
  68. wifiStatus=""
  69.  
  70. WScript.Sleep 60000
  71.  
  72. Loop
  73.  
  74.  
  75. ' Function to get wireless adapter name from the registery
  76. Function GetWirlessName
  77.  
  78. Dim strKeyPath
  79. Dim strComputer
  80. Dim objReg
  81. Dim arrSubKeys
  82. Dim SubKey
  83. Dim strValueName
  84. Dim dwValue
  85. Dim strValue
  86. Const HKLM=&H80000002
  87.  
  88.  
  89. strKeyPath="SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
  90. strComputer="."
  91.  
  92. Set objReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
  93. objReg.Enumkey HKLM ,strKeyPath,arrSubKeys
  94.  
  95. For Each SubKey In arrSubKeys
  96. strValueName="MediaSubType"
  97. objReg.GetDWORDValue HKLM,strKeyPath & "\" & subkey & "\" & "Connection" ,strValueName,dwValue
  98. If dwValue=2 Then
  99. strValueName = "Name"
  100. objReg.GetStringValue HKLM,strKeyPath & "\" & subkey & "\" & "Connection" ,strValueName,strValue
  101. Exit For
  102. End If
  103. Next
  104.  
  105. GetWirlessName=strValue
  106.  
  107. End Function
  108.  
  109.  
  110.  
  111. ' Subroutine to disable wireless connection
  112. Sub DisableWireless (strNetConn)
  113.  
  114. Dim oConnections
  115. dim objShell
  116. Dim objConnections,objConn
  117. Dim strDisable
  118. Dim objNetwork
  119. Dim objDisable
  120. Dim objVerb
  121.  
  122. Const NETWORK_CONNECTIONS = &H31&
  123.  
  124. strDisable = "Disa&ble"
  125.  
  126. Set objShell = CreateObject("Shell.Application")
  127. Set objConnections = objShell.Namespace(NETWORK_CONNECTIONS)
  128.  
  129. For Each objConn In objConnections.Items
  130. If objConn.Name = strNetConn Then
  131. Set objNetwork = objConn
  132. Exit For
  133. End If
  134. Next
  135. Set objDisable = Nothing
  136.  
  137. For Each objVerb in objNetwork.verbs
  138. If objVerb.name = strDisable Then
  139. Set objDisable = objVerb
  140. Exit For
  141. End If
  142.  
  143. Next
  144. objDisable.DoIt
  145. WScript.Sleep 1000
  146. End Sub
  147.  
  148. 'Function to enable wireless connection , you can combone these two subtoutines into one
  149. ' but I prefer to seperate them just for simplicity
  150. Sub EnableWireless (strNetConn)
  151. Dim oConnections
  152. dim objShell
  153. Dim objConnections,objConn
  154. Dim strEnable
  155. Dim objNetwork
  156. Dim objEnable
  157. Dim objVerb
  158.  
  159. Const NETWORK_CONNECTIONS = &H31&
  160.  
  161. strEnable = "En&able"
  162.  
  163. Set objShell = CreateObject("Shell.Application")
  164. Set objConnections = objShell.Namespace(NETWORK_CONNECTIONS)
  165.  
  166.  
  167. For Each objConn In objConnections.Items
  168. If objConn.Name = strNetConn Then
  169. Set objNetwork = objConn
  170. Exit For
  171. End If
  172. Next
  173. Set objEnable = Nothing
  174.  
  175. ' Enable NIC
  176. For Each objVerb in objNetwork.verbs
  177. If objVerb.name = strEnable Then
  178. Set objEnable = objVerb
  179. Exit For
  180. End If
  181.  
  182. Next
  183.  
  184. objEnable.DoIt
  185. WScript.Sleep 1000
  186. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement