2016-02-07 142 views
1

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

运行脚本两次。这不是一个代码写入服务。 –

回答

2

我认为使用WMI会更好。

这里是一个示例脚本:

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 
+0

您应该使用管理权限运行它。不幸的是,如果您在非提升的提示符下运行它,则不会显示错误。我在开始时添加了一个管理权限检查。 – Disti

+0

你的美好,谢谢。一切工作都应该如此。 – ApatheticEuphoria