重置网络适配器 [英] Reset Network Adapter

查看:15
本文介绍了重置网络适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个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

推荐答案

我认为最好使用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 & "
ootcimv2")
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_USERSs-1-5-19")
    IsElevated = (err.number = 0)

end function

这篇关于重置网络适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆