Sign in to follow this  
Followers 0
botanic

Network name from adaptor name

9 posts in this topic

I am having problems with the $NetWorkName = $name

How can I get the networkname from the adapter name?

Global $WinShell= ObjCreate("shell.application"), $ControlPanel = 3, _
$NetConTitle    = 'Network Connections', _    ; Name of the window you see whenever you open "Network Connections" from the Control Panel
$NetWorkName    = 'Wireless Network Connection'; Name of the connection you'd wish to invoke a function on



Func RepairConnection()
    For $ControlPanelItem in $WinShell.NameSpace($ControlPanel).Items
        If $ControlPanelItem.Name = $NetConTitle Then
            For $NetConnection In $ControlPanelItem.GetFolder.Items
                If $NetConnection.Name = $NetWorkName Then ExitLoop(2)
            Next
        EndIf
    Next
    $NetConnection.InvokeVerb('Re&pair')                                    ;Invoke the function
    If WinWaitActive('Repair ' & $NetworkName, 'finished repairing', 45) Then _;As soon as the repairment took place or 45 seconds have passed
        Send('{TAB}{ENTER}')                                                ;Close the window by sending {ALT}{ENTER}
EndFunc

#RequireAdmin

DirCreate(@ProgramFilesDir & "\Easy Network Switcher")
$networkini = @ProgramFilesDir & "\Easy Network Switcher\Adaptor List.ini"

global $1, $b12, $b11, $var, $disc, $servicename,$tmm , $tmp,$cur = "", $ck, $GUID, $TMP, $name,  $current, $del 

if FileExists($networkini) then 
    $del = IniReadSectionNames($networkini)
    For $1 = 1 to $del[0]
    IniDelete($networkini, $del[$1])
    Next

EndIf

    
For $i= 1 to 99999
    $var = RegEnumKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards", $i)
    If @error <> 0 then ExitLoop
    $disc = StringReplace(RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" & $var , "Description"),"/","")
    $servicename = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" & $var , "ServiceName")
    IniWrite($networkini, $disc, "GUID", $servicename)
    IniWrite($networkini, $disc, "NID", $var)
Next

global $hCombo

#include <C:\Program Files\AutoIt3\Include\GUIConstants.au3>
#include <C:\Program Files\AutoIt3\Include\GuiComboBoxEx.au3>

Func _placevalue($name, $guid)
Local   $msg = -1
local $hCombo2 , $b111
$hGUI2 = GUICreate($cur, 400, 130)
$b111 = GUICtrlCreateButton("Apply Setting Profile",5,103)
$b22 = GUICtrlCreateButton("Remove",124, 103)
$b12 = GUICtrlCreateButton("Return",324, 103)
$hCombo2 = _GUICtrlComboBoxEx_Create ($hGUI2, "", 2, 2, 394, 100, BitOR($CBS_SIMPLE, $WS_VSCROLL, $WS_BORDER))
GUISetState()

$ini = IniReadSectionNames(@ProgramFilesDir & "\Easy Network Switcher\Profiles.ini")
if $ini <> @error Then
For $1 = 1 to $ini[0]
_GUICtrlComboBoxex_AddString($hCombo2, $ini[$1])
Next
Else
    MsgBox(48, "Error", "There are no saved profiles to restore")
                            GUIDelete($hGUI2)
                        _start()
EndIf

while 1
    $msg = GUIGetMsg()
    $current = _GUICtrlComboBoxEx_GetCurSel($hCombo2)
    $tmm = IniReadSectionNames(@ProgramFilesDir & "\Easy Network Switcher\" & $name)
    if $tmm <> @error Then $current = $tmm[$current+1]
            Select
                    Case $msg = $GUI_EVENT_CLOSE
                        GUIDelete($hGUI2)
                        _start()
                    Case $msg = $b22
                        IniDelete(@ProgramFilesDir & "\Easy Network Switcher\" & $name, $current)
                    GUIDelete($hGUI2)
                        _placevalue($name, $guid)
                    Case $msg = $b111
                        $NetWorkName = $name
                        $var = IniRead(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "EnableDHCP", "1");1
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "EnableDHCP", "REG_DWORD", $var)
                        $var = IniRead(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "IPAddress", "0.0.0.0");1
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "IPAddress", "REG_MULTI_SZ", $var)
                        $var = IniRead(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "SubnetMask", "0.0.0.0");1
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "SubnetMask", "REG_MULTI_SZ", $var)
                        $var = IniRead(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "DefaultGateway", "");1
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "DefaultGateway", "REG_MULTI_SZ", $var)
                        $var = IniRead(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "DefaultGatewayMetric", "")
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "DefaultGatewayMetric", "REG_MULTI_SZ", $var)
                        $var = IniRead(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "NameServer", "")   
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "NameServer", "REG_SZ",$var)
                        RepairConnection()  
                        MsgBox(48, "Apply", "Settings were applied")
                        GUIDelete($hGUI2)
                        _start()
Case $msg = $b12
                        GUIDelete($hGUI2)
                        _start()
        EndSelect
WEnd
EndFunc
_start()
Func _start()
    Local $msg
$hGUI = GUICreate("Network Easy Switcher", 400, 130)
$b1 = GUICtrlCreateButton("Save Current Settings",5,103)
$b2 = GUICtrlCreateButton("Apply Custom Settings",124, 103)
$b3 = GUICtrlCreateButton("Reset Default Settings",245, 103)
$hCombo = _GUICtrlComboBoxEx_Create ($hGUI, "", 2, 2, 394, 100, BitOR($CBS_SIMPLE, $WS_VSCROLL, $WS_BORDER))
GUISetState()
    
$ini = IniReadSectionNames($networkini)
For $1 = 1 to $ini[0]
_GUICtrlComboBoxex_AddString($hCombo, $ini[$1])
Next

While 1
    $msg = GUIGetMsg()

    if $msg = $b1 or $msg = $b2 or $msg = $b3 or $msg = $gui_event_Close then
        $var = _GUICtrlComboBoxEx_GetCurSel($hCombo)
            
        If $var <> -1 Then
                Select
                    Case $msg = $GUI_EVENT_CLOSE
                        Exit
                    Case $msg = $b1
                                                $cur = $ini[$var+1]
                        $name = $cur
                        $GUID = IniRead($networkini, $name, "GUID", "")
                            $name = "Profiles.ini"
                        $current = InputBox("Name Settings", "Please type a name for these settings.", "")
                    if $current <> "" and $current <> "Default" Then
                        $var = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "EnableDHCP")
                        IniWrite(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "EnableDHCP", $var);1
                        $var = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "IPAddress")
                        IniWrite(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "IPAddress", $var);1
                        $var = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "SubnetMask")
                        IniWrite(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "SubnetMask", $var);1
                        $var = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "DefaultGateway")
                        IniWrite(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "DefaultGateway", $var);1
                        $var = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "DefaultGatewayMetric")
                        IniWrite(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "DefaultGatewayMetric", $var)
                        $var = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "NameServer")
                        IniWrite(@ProgramFilesDir & "\Easy Network Switcher\" & $name , $current, "NameServer", $var)
                    ElseIf $current = "" then
                        MsgBox(48, "Error", "You must type a name for these settings.")
                    Else
                        MsgBox(48, "Error", "The name 'Default' is reserved by the system.")                    
                    EndIf
                    
                Case $msg = $b2
                        $cur = $ini[$var+1]
                        $name = $cur
                        $GUID = IniRead($networkini, $name, "GUID", "")
                        GUIDelete()
                        GUICtrlSetState($hCombo, @SW_hide)
                        $name = "Profiles.ini"
                        _placevalue($name , $GUID)
                        ExitLoop
                    Case $msg = $b3
                                                $cur = $ini[$var+1]
                        $name = $cur
                        
                        $GUID = IniRead($networkini, $name, "GUID", "")
                        $name = "Profiles.ini"
                        $NetWorkName = $name
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "EnableDHCP", "REG_DWORD", "1")
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "IPAddress", "REG_MULTI_SZ", "0.0.0.0")
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "SubnetMask", "REG_MULTI_SZ", "0.0.0.0")
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "DefaultGateway", "REG_MULTI_SZ", "")
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "DefaultGatewayMetric", "REG_MULTI_SZ", "")
                        $var = RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"& $GUID, "NameServer", "REG_SZ","")
                        RepairConnection()  
                        MsgBox(48, "Default", "Windows Default restored.")
                EndSelect
            Else
            if $msg = $GUI_EVENT_CLOSE then Exit
            MsgBox(48, "OPPS!!!", "Please select a Network Card")
            $msg = 0
        EndIf
    EndIf
    
Wend

EndFunc

Share this post


Link to post
Share on other sites



Is this what you mean?

$objWMIService = ObjGet("winmgmts:\\.\root\cimv2")
$objClass = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapter")

For $obj in $objClass
    msgbox(1,"","Device: " & $obj.Name & @CRLF & _
                "Name: " & $obj.NetConnectionID)
Next

Share this post


Link to post
Share on other sites

possibly i have no idea what that is ;/

Share this post


Link to post
Share on other sites

It maps Network name to Adapter name, like you asked for.... Did I miss something? Is that not what you wanted?

Share this post


Link to post
Share on other sites

yes that is what I wanted how do i use it tho i dont understand how it works ;/

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I just gave you the code to map the two. How you integrate that into your script is up to you. The first thing I'm hoping you did was paste that code into a new script and run it to see what it does. ;-)

I'm not entirely sure how you want to use the information in your script. Here's an example though. It lists all the network devices in a listbox. Click on each one, and it will display the associated network name in the label at the top, if there is one. Hopefully this gives you some ideas.

#include <GuiConstants.au3>
#include <GuiListBox.au3>

$hWnd = GuiCreate("")
$hLabel = GUICtrlCreateLabel("Network Name",5,5, 390)
$hList = GUICtrlCreateList("",5,40,390,300)
GUISetState()

$objWMIService = ObjGet("winmgmts:\\.\root\cimv2")
$objClass = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapter")

For $obj in $objClass
    _GUICtrlListBox_AddString($hList,$obj.Name)
Next

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $hList
            For $obj in $objClass
                If _GUICtrlListBox_GetText($hList, _GUICtrlListBox_GetCurSel($hList)) = $obj.Name Then
                    GUICtrlSetData($hLabel,$obj.NetConnectionID)
                    If $obj.NetConnectionID = "" Then GUICtrlSetData($hLabel,"Nothing associated with this device")
                    ExitLoop
                EndIf
            Next
    EndSwitch
WEnd

And if it's the whole WMI scripting thing that's throwing you off. Google up some tutorials on WMI scripting. Or just ask about it here and I, or someone, will give you the basics.

Edited by covaks

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I tried to run it and got a listbox full of nothing -.-

and the first one displayed no msgbox's

Edited by botanic

Share this post


Link to post
Share on other sites

Do you have admin rights on the machine you are running this on? A regular user can't even see network card I believe, at least on XP Pro SP2.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0