#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.8.1
Author: Erlik (Garry Galler)
Script Function: NIC Viewer
Description: Utility for managing network adapters
#ce ----------------------------------------------------------------------------
#NoTrayIcon
#RequireAdmin
#include-once
#include <Constants.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <ComboConstants.au3>
#include <SendMessage.au3>
;===========================================================
#AutoIt3Wrapper_Icon=.\icons\Network.ico
#AutoIt3Wrapper_Outfile=NICViewer.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseUpx=Y
#AutoIt3Wrapper_Res_Comment=-
#AutoIt3Wrapper_Res_Description=NIC Viewer by Garry Galler 2013
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_LegalCopyright=ERLIK
#AutoIt3Wrapper_Res_Language=1049
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#AutoIt3Wrapper_Run_AU3Check=n
;===========================================================
Global Const $tagLVITEM = "uint Mask;int Item;int SubItem;uint State;uint StateMask;ptr Text;int TextMax;int Image;lparam Param;" & _
"int Indent;int GroupID;uint Columns;ptr pColumns"
Global $hMainWnd
Global $hListView
Global $hCombo
Global $ButtonRefresh
Global $ButtonRestart
Global $ButtonOff
Global $ButtonOn
Global $CheckBoxIf
Global $CheckBoxDbg
Global $aAdaptersInfo
Global $strComputer = "localhost"
Global $Compare=''
Global $fDebugMode=False
;===========================================================
;COMMAND LINE: NICViewer.exe [NumberInterface]
; [NumberInterface] - номер интерфейса (DeviceID) или список номеров через запятую: 1 или 1,5,8 или -1 - для всех сразу. Неправильные номера будут проигнорированы.
; Дополнительные опции:
; "all" - применить действия ко всем интерфейсам - физическим и виртуальным(не знаю стоит ли использовать, так как к чему приведут манипуляции с виртуальными адаптерами не знаю), по умолчанию - только к физическим. Физические адаптеры в скрипте определяются по наличию непустой строки в параметре NetConnectionID, у виртуальных адаптеров по моим наблюдениям он пустой. НЕ ГАРАНТИРОВАНО ЧТО ЭТО ТАК ВСЕГДА.
; "test" - тестовый режим: NICViewer.exe NumberInterface "test" или NICViewer.exe NumberInterface "all" "test" - вместо реальных команд выводится MsgBox с текстом.
;;===========================================================
CMDCheck()
Func CMDCheck()
Local $aArgs,$sOutput=''
If $CmdLine[0] > 0 Then
If $CmdLine[0] >= 2 Then
If $CmdLine[2]='all' Then
$Compare=1
EndIf
If $CmdLine[2]='test' Then
$fDebugMode=True
EndIf
If $CmdLine[0] > 2 And $CmdLine[3]='test' Then
$fDebugMode=True
EndIf
EndIf
$aArgs=StringSplit($CmdLine[1],',')
If Not IsArray($aArgs) Then Return
GetAdaptersInfo()
Select
Case $aArgs[0]=1
If $aArgs[1]=-1 Then
For $i=0 To UBound($aAdaptersInfo)-1
If $aAdaptersInfo[$i][5] = '' Then ContinueLoop
If Not $fDebugMode Then
RunWait(@ComSpec & " /C " & "devcon restart " & $aAdaptersInfo[$i][5], "", @SW_HIDE)
EndIf
ConsoleWrite("[" & $i & "]" & $aAdaptersInfo[$i][5] & "|Caption=" & $aAdaptersInfo[$i][0] & @CRLF)
$sOutput&=$aAdaptersInfo[$i][5] & "|Caption=" & $aAdaptersInfo[$i][0] & @CRLF
Next
TestMode($sOutput)
Else
For $i=0 To UBound($aAdaptersInfo)-1
If $aAdaptersInfo[$i][8]=$aArgs[1] Then
If $aAdaptersInfo[$i][5] = '' Then ExitLoop
If Not $fDebugMode Then
RunWait(@ComSpec & " /C " & "devcon restart " & $aAdaptersInfo[$i][5], "", @SW_HIDE)
EndIf
ConsoleWrite($aAdaptersInfo[$i][5] & "|Caption=" & $aAdaptersInfo[$i][0] & @CRLF)
$sOutput&= $aAdaptersInfo[$i][5] & "|Caption=" & $aAdaptersInfo[$i][0] & @CRLF
ExitLoop
EndIf
Next
TestMode($sOutput)
EndIf
Case $aArgs[0]>1
For $i=0 To UBound($aAdaptersInfo)-1
For $a=1 To $aArgs[0]
If $aAdaptersInfo[$i][5] = '' Then ContinueLoop
If $aAdaptersInfo[$i][8]= $aArgs[$a] Then
If Not $fDebugMode Then
RunWait(@ComSpec & " /C " & "devcon restart " & $aAdaptersInfo[$i][5], "", @SW_HIDE)
EndIf
ConsoleWrite("[" & $i & "]" & $aAdaptersInfo[$i][5] & "|Caption=" & $aAdaptersInfo[$i][0] & @CRLF)
$sOutput&=$aAdaptersInfo[$i][5]& "|Caption=" & $aAdaptersInfo[$i][0] & @CRLF
EndIf
Next
Next
TestMode($sOutput)
EndSelect
Exit
Else
GUI()
EndIf
EndFunc
Func GUI()
$hMainWnd = GUICreate("NIC Viewer", 450,250, -1, -1)
$hCombo = GUICtrlCreateCombo("", 2, 2, 350)
$ButtonRefresh = GUICtrlCreateButton("Refresh", 360,2,80,25)
$CheckBoxIf = GUICtrlCreateCheckbox("Show all", 360,27)
$CheckBoxDbg = GUICtrlCreateCheckbox("Test mode", 360,60)
$ButtonRestart = GUICtrlCreateButton("Restart", 360,105,80, 40)
$ButtonOff = GUICtrlCreateButton("Off", 360,155,80, 40)
$ButtonOn = GUICtrlCreateButton("On", 360,205,80, 40)
$hListView = GUICtrlCreateListView('Параметр|Значение',2,25,350,220)
GetAdaptersInfo()
SetDataCombo()
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
GUIDelete()
Exit
Case $ButtonRefresh
SetDataCombo()
SetDataListView()
Case $ButtonOff
OffOnAdapters(0)
Case $ButtonOn
OffOnAdapters(1)
Case $ButtonRestart
OffOnAdapters(2)
Case $CheckBoxIf
If GUICtrlRead($CheckBoxIf)=$GUI_CHECKED Then
$Compare=1
GetAdaptersInfo()
Else
$Compare=''
GetAdaptersInfo()
Endif
Case $CheckBoxDbg
$fDebugMode = Not $fDebugMode
Case $hCombo
SetDataListView()
EndSwitch
Wend
EndFunc
Func OffOnAdapters($iMode)
Local $sAdapterName,$sAdapterCaption,$sHwids
$sAdapterName=_GetItemTextLV($hListView, 0, 1)
If $sAdapterName='' Then Return
Local $aState[3]=['отключение','включение','рестарт']
Local $aCommand[3]=[ _
@ComSpec & " /C " & "devcon disable ", _
@ComSpec & " /C " & "devcon enable ", _
@ComSpec & " /C " & "devcon restart " _
]
Local $iAction = MsgBox(4, "", "Выполнить " & $aState[$iMode] & " адаптера " & $sAdapterName & "?")
If $iAction = 7 Then Return
$sAdapterCaption=GUICtrlRead($hCombo)
For $i=0 To UBound($aAdaptersInfo)-1
If $aAdaptersInfo[$i][0]=$sAdapterCaption Then
$sHwids=$aAdaptersInfo[$i][5]
EndIf
Next
Switch $iMode
Case 0
If Not $fDebugMode Then
RunWait($aCommand[$iMode] & $sHwids, "", @SW_HIDE)
EndIf
TestMode($aCommand[$iMode] & $sHwids)
Case 1
If Not $fDebugMode Then
RunWait($aCommand[$iMode] & $sHwids, "", @SW_HIDE)
EndIf
TestMode($aCommand[$iMode] & $sHwids)
Case 2
If Not $fDebugMode Then
RunWait($aCommand[$iMode] & $sHwids, "", @SW_HIDE)
EndIf
TestMode($aCommand[$iMode] & $sHwids)
EndSwitch
ConsoleWrite($sHwids & @CRLF)
EndFunc
Func SetDataCombo()
GUICtrlSendMsg($hCombo, $CB_RESETCONTENT,0,0)
Local $sAdapters=''
If IsArray($aAdaptersInfo) Then
For $i=0 To UBound($aAdaptersInfo)-1
$sAdapters&=$aAdaptersInfo[$i][0] & "|"
Next
EndIf
GUICtrlSetData($hCombo,$sAdapters)
EndFunc
Func SetDataListView()
;GUICtrlSendMsg($hListView, $LVM_DELETEALLITEMS, 0, 0)
_SendMessage(GUICtrlGetHandle($hListView), $LVM_DELETEALLITEMS)
Local $sAdapterName=GUICtrlRead($hCombo)
For $i=0 To UBound($aAdaptersInfo)-1
If $aAdaptersInfo[$i][0]=$sAdapterName Then
GUICtrlCreateListViewItem('AdapterName|' & $aAdaptersInfo[$i][1], $hListView)
GUICtrlCreateListViewItem('NetConnectionID|' & $aAdaptersInfo[$i][2], $hListView)
GUICtrlCreateListViewItem('MACAddress|' & $aAdaptersInfo[$i][3], $hListView)
GUICtrlCreateListViewItem('AdapterType|' & $aAdaptersInfo[$i][4], $hListView)
GUICtrlCreateListViewItem('PNPDeviceID|' & $aAdaptersInfo[$i][5], $hListView)
GUICtrlCreateListViewItem('NetConnectionStatus|' & $aAdaptersInfo[$i][6], $hListView)
GUICtrlCreateListViewItem('TimeOfLastReset|' & $aAdaptersInfo[$i][7], $hListView)
GUICtrlCreateListViewItem('DeviceID|' & $aAdaptersInfo[$i][8], $hListView)
GUICtrlCreateListViewItem('ProductName|' & $aAdaptersInfo[$i][9], $hListView)
GUICtrlCreateListViewItem('ServiceName|' & $aAdaptersInfo[$i][10], $hListView)
GUICtrlCreateListViewItem('Manufacturer|' & $aAdaptersInfo[$i][11], $hListView)
GUICtrlCreateListViewItem('Availability|' & $aAdaptersInfo[$i][12], $hListView)
EndIf
Next
_SetColumnWidthLV($hListView, -2)
EndFunc
Func GetAdaptersInfo()
Dim $aAdaptersInfo[1][13]
Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter")
Local $iCount=0
If IsObj($colItems) Then
For $objItem In $colItems
If $objItem.NetConnectionID <> $Compare Then
If $iCount <> 0 Then Redim $aAdaptersInfo[$iCount+1][13]
$aAdaptersInfo[$iCount][0]=$objItem.Caption
$aAdaptersInfo[$iCount][1]=$objItem.Name
$aAdaptersInfo[$iCount][2]=$objItem.NetConnectionID
$aAdaptersInfo[$iCount][3]=$objItem.MACAddress
$aAdaptersInfo[$iCount][4]=$objItem.AdapterType
$aAdaptersInfo[$iCount][5]=$objItem.PNPDeviceID
$aAdaptersInfo[$iCount][6]=$objItem.NetConnectionStatus
$aAdaptersInfo[$iCount][7]=WMIDateStringToDate($objItem.TimeOfLastReset)
$aAdaptersInfo[$iCount][8]=$objItem.DeviceID
$aAdaptersInfo[$iCount][9]=$objItem.ProductName
$aAdaptersInfo[$iCount][10]=$objItem.ServiceName
$aAdaptersInfo[$iCount][11]=$objItem.Manufacturer
$aAdaptersInfo[$iCount][12]=$objItem.Availability
$iCount+=1
EndIf
Next
EndIf
EndFunc
Func _SetColumnWidthLV($ListView, $iSize, $iStartIndex=0)
Local $iLV_ColumnCount = _SendMessage(GUICtrlSendMsg($ListView, $LVM_GETHEADER, 0, 0), 0x1200)
For $iIndex = $iStartIndex To $iLV_ColumnCount - 1
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, $iIndex,$iSize)
Next
EndFunc
Func _GetItemTextLV($hWnd, $iIndex, $iSubItem = 0)
Local $tBuffer = DllStructCreate("wchar Text[4096]")
Local $pBuffer = DllStructGetPtr($tBuffer)
Local $tItem = DllStructCreate($tagLVITEM)
DllStructSetData($tItem, "SubItem", $iSubItem)
DllStructSetData($tItem, "TextMax", 4096)
Local $pItem = DllStructGetPtr($tItem)
DllStructSetData($tItem, "Text", $pBuffer)
GUICtrlSendMsg($hWnd, $LVM_GETITEMTEXTW, $iIndex, $pItem)
Return DllStructGetData($tBuffer, "Text")
EndFunc
Func WMIDateStringToDate($dtmDate)
Return (StringMid($dtmDate, 5, 2) & "/" & _
StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
& " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc
Func TestMode($sOutput)
If $fDebugMode Then
MsgBox(0,'Test mode',$sOutput)
EndIf
EndFunc