遍歷控制面板「網絡連接」資料夾,按名稱匹配目標連接(如「區域連接」或「無線網絡連接」),調用其 Verb 執行「啟用」或「禁用」操作。
Const ssfCONTROLS = 3
sConnectionName = "區域連接" ' 目標連接名稱,可修改
sEnableVerb = "啟用(&A)" ' XP/Win7 顯示文本
sDisableVerb = "禁用(&B)" ' XP 中為 "停用(&B)"
Set shellApp = CreateObject("Shell.Application")
Set oControlPanel = shellApp.Namespace(ssfCONTROLS)
Set oNetConnections = Nothing
For Each folderItem In oControlPanel.Items
If folderItem.Name = "網絡連接" Then
Set oNetConnections = folderItem.GetFolder
Exit For
End If
Next
If oNetConnections Is Nothing Then
MsgBox "未找到網絡連接資料夾"
WScript.Quit
End If
Set oLanConnection = Nothing
For Each folderItem In oNetConnections.Items
If LCase(folderItem.Name) = LCase(sConnectionName) Then
Set oLanConnection = folderItem
Exit For
End If
Next
If oLanConnection Is Nothing Then
MsgBox "未找到連接: " & sConnectionName
WScript.Quit
End If
bEnabled = True
Set oEnableVerb = Nothing
Set oDisableVerb = Nothing
For Each verb In oLanConnection.Verbs
If verb.Name = sEnableVerb Then
Set oEnableVerb = verb
bEnabled = False
End If
If verb.Name = sDisableVerb Then
Set oDisableVerb = verb
End If
Next
If bEnabled Then
oDisableVerb.DoIt ' 當前啟用狀態 -> 執行禁用
Else
oEnableVerb.DoIt ' 當前禁用狀態 -> 執行啟用
End If
WScript.Sleep 400 ' 等待操作生效
Shell.Application:Windows Shell 自動化對象,提供命名空間訪問。Namespace(ssfCONTROLS):ssfCONTROLS=3 對應控制面板虛擬文件夾。FolderItem.Verbs:右鍵菜單動作集合,每個 Verb 對應一個操作(啟用、禁用、屬性等)。Verb.DoIt():執行該動作,等同於用戶右鍵點擊選擇。netsh interface set interface "名稱" admin=enable/disable 或 PowerShell Enable-NetAdapter / Disable-NetAdapter。