Skip to content

Instantly share code, notes, and snippets.

@lwjef
Forked from simply-coded/Disable_Wi-Fi.vbs
Last active December 3, 2018 14:40
Show Gist options
  • Save lwjef/ac585c55dd8e8fd62057f9665d4603b9 to your computer and use it in GitHub Desktop.
Save lwjef/ac585c55dd8e8fd62057f9665d4603b9 to your computer and use it in GitHub Desktop.
Use VBScript to enable, disable, or toggle a connection like your Wi-Fi on and off.
'https://gist.github.com/simply-coded/510c789d575e6bff12a3d455c2f5d1e9
'https://bennettadelson.wordpress.com/2012/02/27/confirming-basic-domain-controller-connectivity-through-vbscript/
Option Explicit
Dim interface, interfaceName, interfaceTarget, available, verb, strHostName
Dim objApp : Set objApp = CreateObject("Shell.Application")
Dim objFolder : Set objFolder = objApp.Namespace(&H31&).Self.GetFolder
Dim WshShell : Set WshShell=CreateObject("Wscript.Shell")
WshShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\AdapterUP",WScript.ScriptFullName,"REG_SZ"
WScript.Sleep 30000
interfaceName = "本地连接"
strHostName = "192.168.1.1"
Function Ping( myHostName )
Dim colPingResults, objPingResult, strQuery
strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & myHostName & "'"
Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )
For Each objPingResult In colPingResults
If Not IsObject( objPingResult ) Then
Ping = False
ElseIf objPingResult.StatusCode = 0 Then
Ping = True
Else
Ping = False
End If
Next
Set colPingResults = Nothing
End Function
If Ping(strHostName) = True Then
WScript.Quit
End If
For Each interface In objFolder.Items
If interface.Name = interfaceName Then
Set interfaceTarget = interface
End If
available = available & interface.Name & vbLf
Next
For Each verb In interfaceTarget.Verbs
If verb.Name = "禁用(&B)" Then
verb.DoIt
WScript.Sleep 1000
End If
Next
WScript.Sleep 3000
For Each verb In interfaceTarget.Verbs
If verb.Name = "启用(&A)" Then
verb.DoIt
WScript.Sleep 1000
End If
Next
'************************
'Name: Disable Connection
'Author: Jeremy England
'Company: SimplyCoded
'Date: 10/01/2016
'************************
Option Explicit
Dim interface, interfaceName, interfaceTarget, available, verb
'Pick the Interface Name you want to disable
interfaceName = "Wi-Fi"
'Find available Names by running this script or in cmd
'>> netsh interface show interface [enter]
'Set up required objects
Dim objApp : Set objApp = CreateObject("Shell.Application")
Dim objFolder : Set objFolder = objApp.Namespace(&H31&).Self.GetFolder
'Check if Network Connections folder exists
If objFolder Is Nothing Then
MsgBox "Network Connections folder not found. Check the location: ""C:\Windows\System32\ncpa.cpl""", vbCritical
WScript.Quit
End If
'Make sure interface exists
Set interfaceTarget = Nothing
'Interface exists
For Each interface In objFolder.Items
If LCase(interface.Name) = LCase(interfaceName) Then
Set interfaceTarget = interface
End If
available = available & interface.Name & vbLf
Next
'Interface Doesn't exist
If interfaceTarget Is Nothing Then
MsgBox "Interface Name: """ & interfaceName & """ not found. " &_
"Available Interface Names: " & vbLf & vbLf & available, vbCritical
WScript.Quit
End If
'Interface Enable / Disable
Dim success : success = False
For Each verb In interfaceTarget.Verbs
If verb.Name = "Disa&ble" Then
verb.DoIt
WScript.Sleep 1000
MsgBox "Disabled: """ & interfaceName & """", vbInformation
success = True
Exit For
End If
Next
If Not success Then
MsgBox "Already disabled : """ & interfaceName & """", vbInformation
End If
'***********************
'Name: Enable Connection
'Author: Jeremy England
'Company: SimplyCoded
'Date: 10/01/2016
'***********************
Option Explicit
Dim interface, interfaceName, interfaceTarget, available, verb
'Pick the Interface Name you want to enable
interfaceName = "Wi-Fi"
'Find available Names by running this script or in cmd
'>> netsh interface show interface [enter]
'Set up required objects
Dim objApp : Set objApp = CreateObject("Shell.Application")
Dim objFolder : Set objFolder = objApp.Namespace(&H31&).Self.GetFolder
'Check if Network Connections folder exists
If objFolder Is Nothing Then
MsgBox "Network Connections folder not found. Check the location: ""C:\Windows\System32\ncpa.cpl""", vbCritical
WScript.Quit
End If
'Make sure interface exists
Set interfaceTarget = Nothing
'Interface exists
For Each interface In objFolder.Items
If LCase(interface.Name) = LCase(interfaceName) Then
Set interfaceTarget = interface
End If
available = available & interface.Name & vbLf
Next
'Interface Doesn't exist
If interfaceTarget Is Nothing Then
MsgBox "Interface Name: """ & interfaceName & """ not found. " &_
"Available Interface Names: " & vbLf & vbLf & available, vbCritical
WScript.Quit
End If
'Interface Enable / Disable
Dim success : success = False
For Each verb In interfaceTarget.Verbs
If verb.Name = "En&able" Then
verb.DoIt
WScript.Sleep 1000
MsgBox "Enabled: """ & interfaceName & """", vbInformation
success = True
Exit For
End If
Next
If Not success Then
MsgBox "Already enabled : """ & interfaceName & """", vbInformation
End If
'***********************
'Name: Toggle Connection
'Author: Jeremy England
'Company: SimplyCoded
'Date: 10/01/2016
'***********************
Option Explicit
Dim interface, interfaceName, interfaceTarget, available, verb
'Pick the Interface Name you want to disable/enable
interfaceName = "Wi-Fi"
'Find available Names by running this script or in cmd
'>> netsh interface show interface [enter]
'Set up required objects
Dim objApp : Set objApp = CreateObject("Shell.Application")
Dim objFolder : Set objFolder = objApp.Namespace(&H31&).Self.GetFolder
'Check if Network Connections folder exists
If objFolder Is Nothing Then
MsgBox "Network Connections folder not found. Check the location: ""C:\Windows\System32\ncpa.cpl""", vbCritical
WScript.Quit
End If
'Make sure interface exists
Set interfaceTarget = Nothing
'Interface exists
For Each interface In objFolder.Items
If LCase(interface.Name) = LCase(interfaceName) Then
Set interfaceTarget = interface
End If
available = available & interface.Name & vbLf
Next
'Interface Doesn't exist
If interfaceTarget Is Nothing Then
MsgBox "Interface Name: """ & interfaceName & """ not found. " &_
"Available Interface Names: " & vbLf & vbLf & available, vbCritical
WScript.Quit
End If
'Interface Enable / Disable
For Each verb In interfaceTarget.Verbs
If verb.Name = "En&able" Then
verb.DoIt
WScript.Sleep 1000
MsgBox "Enabled: """ & interfaceName & """", vbInformation
ElseIf verb.Name = "Disa&ble" Then
verb.DoIt
WScript.Sleep 1000
MsgBox "Disabled: """ & interfaceName & """", vbInformation
End If
Next