【问题标题】:Reset Network Adapter重置网络适配器
【发布时间】:2016-02-07 03:09:31
【问题描述】:

我在这里有一个 VBScript 来“切换”我的网络适配器(根据 Internet 上的脚本构建)。

我想知道是否有人可以帮助我将其转换为可以“重置”(禁用,然后重新启用)我的无线适配器的脚本,而不是切换它。

'~ Toggle a SPECIFIED NIC on or off
Option Explicit

Const NETWORK_CONNECTIONS = &H31&

Dim objShell, objFolder, objFolderItem, objEnable, objDisable
Dim folder_Object, target_NIC
Dim NIC, clsVerb
Dim str_NIC_Name, strEnable, strDisable
Dim bEnabled, bDisabled

'NIC name goes here VVV
str_NIC_Name = "Wi-Fi"

strEnable = "En&able"
strDisable = "Disa&ble"

' create objects and get items
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(NETWORK_CONNECTIONS)
Set objFolderItem = objFolder.Self
Set folder_Object = objFolderItem.GetFolder

Set target_NIC = Nothing

' look at each NIC and match to the chosen name
For Each NIC In folder_Object.Items
    If LCase(NIC.Name) = LCase(str_NIC_Name) Then
        ' proper NIC is found, get it
        Set target_NIC = NIC
    End If
Next

bEnabled = True
Set objEnable = Nothing
Set objDisable = Nothing

For Each clsVerb In target_NIC.Verbs
    '~ Wscript.Echo clsVerb
    If clsVerb.Name = strEnable Then
        Set objEnable = clsVerb
        bEnabled = False
    End If
    If clsVerb.Name = strDisable Then
        Set objDisable = clsVerb
    End If
Next

If bEnabled Then
    objDisable.DoIt
Else
    objEnable.DoIt
End If

'~ Give the connection time to change
WScript.Sleep 5000

【问题讨论】:

  • 运行脚本两次。这不是代码编写服务。

标签: vbscript wmi


【解决方案1】:

我认为最好使用 WMI(Windows 管理规范)。

这是一个示例脚本:

Option Explicit

dim ComputerName
dim WMIService, NIC
dim ConnectionName

ComputerName = "."
ConnectionName = "Ethernet"

if not IsElevated then 
    WScript.Echo "Please run this script with administrative rights!"
    WScript.Quit
end if

On Error Resume Next

Set WMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2")
Set NIC = WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = '" & ConnectionName & "'").ItemIndex(0)

if NIC is nothing then
    WScript.Echo "NIC not found!"
    WScript.quit
end if

WScript.Echo "NIC Current status: " & iif(NIC.NetEnabled, "Enabled", "Disabled") & vbcrlf

if NIC.NetEnabled then
    WScript.Echo "Disabling NIC..."
    NIC.Disable
    WScript.Sleep 1000
    WScript.Echo "Enabling NIC..."
    NIC.Enable
end if

function iif(cond, truepart, falsepart)
    if cond then iif=truepart else cond=falsepart
end function

function IsElevated()

    On Error Resume Next
    CreateObject("WScript.Shell").RegRead("HKEY_USERS\s-1-5-19\")
    IsElevated = (err.number = 0)

end function

【讨论】:

  • 您应该使用管理权限运行它。不幸的是,如果您在非提升提示符下运行它,则不会显示错误。我在开头添加了一个管理权限检查。
  • 你太棒了,谢谢。一切正常。
猜你喜欢
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-04
相关资源
最近更新 更多