Jump to content

Toggle Lan Connection


Recommended Posts

Hey all,

I'm trying to find a way of reading items from a windows explorer details view.

here's what i have so far:

Opt("WinWaitDelay", 100)
$search_crit = "Network Connections"
_IERunAsCPL()

Func _IERunAsCPL()
    If WinExists($search_crit) Then
        WinClose($search_crit)
    EndIf
    Run(@ComSpec & ' /c ' & '"c:\Program Files\Internet Explorer\iexplore.exe" ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}', '', @SW_HIDE)
    WinWait($search_crit)
    WinActivate($search_crit)
    WinWaitActive($search_crit)
    $bla = StatusbarGetText($search_crit)
    ConsoleWrite(@crlf & "status bar says " & $bla)

EndFunc   ;==>_IERunAsCPL


Func _ListLANConnections()
    
EndFunc   ;==>_ListLANConnections

This is going to be a program that will do a runas and enable/disable network connections from some of our users.

I have not found a successful wayof doing it through the command line.

please advise.

thanks.

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Link to comment
Share on other sites

I'm don't understand your question entierly but maybee you would get clearity if you do a search for 'net use' or try net use /? in a cmd window? A search for +network +share +run would probably be helpfull to.

Link to comment
Share on other sites

thanks for replying.

I actually got my initial question working.

Ultimately what I am trying to do is create an application that can enable/disable certain network connections in the network connections tab. The reason is that we have about 600 users with Tablets that have those connections turn off and they do not have priviledges to turn them on. my script will do a run as and open up the network connections folder, then select the appropriate network connections to be "toggled" on. I am not trying to enable/disable the network card, but the specific network connection.

For example if you open up the network connections folder (with admin privies) and right click on a connection, you will get the option to enable or disable that connection (depending on its curent state).

I have already looked at command line options to do this and I have not found much on it. Some of the suggested solutions would not work such as the following:

Using NETSH to control the interface: This didnt work since it did not accurately display the network connections, it displayed the interfaces. Any LAN based interface by design could not be manipulated with this option.

http://forums1.itrc.hp.com/service/forums/...958397+28353475

Using a Shell Script: Our environment doesnt allow for the use of Cscript and Wscript commands.

If anyone knows how to incorporate the following, i think it might work:

http://groups.google.co.uk/group/microsoft...c93e?&hl=en

Rundll32: I found a way to open up the network connections folder with rundll32, but not actually set anything.

The Net command: This tool doesn't control individual network connections. It controls services

WMI: Currently the only API i know that will do this, but i do not know how to incorporate it into AUtoit, or actually run it from WMIC. Here's the info on how to do it in WMI (first entry):

http://msdn.microsoft.com/library/default...._networking.asp

Anyway here's my code, what I was asking for in relation to my code, is how to select an item from the listview, but i've since figured it out.

Opt("WinWaitDelay", 100)
$search_crit = "Network Connections"
_IERunAsCPL()
_ListLANConnections()
Func _IERunAsCPL()
    If WinExists($search_crit) Then
        WinClose($search_crit)
    EndIf
    Run(@ComSpec & ' /c ' & '"c:\Program Files\Internet Explorer\iexplore.exe" ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}', '', @SW_HIDE)
    WinWait($search_crit)
    WinActivate($search_crit)
    WinActivate($search_crit)
    WinWaitActive($search_crit)
    $bla = StatusbarGetText($search_crit)
    ConsoleWrite(@crlf & "status bar says " & $bla)
    ConsoleWrite(@crlf &ControlListView($search_crit, "", "SysListView321", "FindItem","Bluetooth")&@crlf )
    
;~  $getclass = WinGetClassList($search_crit)
;~  MsgBox(0, "", $getclass)
EndFunc   ;==>_IERunAsCPL


Func _ListLANConnections()
    $item_loc = ControlListView($search_crit, "", "SysListView321", "FindItem","Bluetooth")
    ConsoleWrite(@crlf & $item_loc&@crlf )

    ControlListView($search_crit, "", "SysListView321", "Select",$item_loc)
    ControlListView($search_crit, "", "SysListView321", "Select",0)
    
EndFunc   ;==>_ListLANConnections

Thanks for reading this blab.

Edited by blademonkey

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Link to comment
Share on other sites

Nice to here you have solved your issues. :P

I totally misunderstod what you tried to achive :">

Link to comment
Share on other sites

I solved my minor issue. the expected results have not been achieved yet.

now what i'm trying to do is go to the File menu and select enable once the item is select, the problem WinMenuSelectItem doesnt work.

When i looked at the AutoitSpy the menu came up as a toolbar and not a menu, which is probably the reason.

anyone know how to select menu items from a Toolbar class control?

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Link to comment
Share on other sites

Well since no one responded i had to go with the sloppy Send function. which works but is not the most optimal.

if anyone can give me some insight, it would be greatly appreciated.

Here it is.

Opt("WinWaitDelay", 10)
$search_crit = "Network Connections"
IERunAsCPL()
EnableConnection("Bluetooth")

Winclose($search_crit)

Func IERunAsCPL()
    If WinExists($search_crit) Then
        WinClose($search_crit)
    EndIf
    Run(@ComSpec & ' /c ' & '"c:\Program Files\Internet Explorer\iexplore.exe" ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}', '', @SW_HIDE)
    
    WinWait($search_crit)
    WinSetState($search_crit,"",@SW_HIDE)
    WinActivate($search_crit)
    WinActivate($search_crit)
    WinWaitActive($search_crit)
    ConsoleWrite(@crlf &ControlListView($search_crit, "", "SysListView321", "FindItem","Bluetooth")&@crlf )

EndFunc   ;==>IERunAsCPL


Func EnableConnection($input)
    $item_loc = ControlListView($search_crit, "", "SysListView321", "FindItem",$input)
    $status= ControlListView($search_crit, "", "SysListView321", "GetText",$item_loc,2) 
        
    If $status = "Disabled" Then
        ControlListView($search_crit, "", "SysListView321", "Select",$item_loc)
        Send("!f")
        Send("a")
    EndIf
EndFunc   ;==> EnableConnection
Edited by blademonkey

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Link to comment
Share on other sites

You mention that the script will runas adminitrator, so you can query the register to get the network connection names.

#include <GUIConstants.au3>

Dim $i, $base, $key, $name

; == GUI generated with Koda ==
$Form1 = GUICreate("Nic", 176, 89, -1, -1, -1, $WS_EX_TOOLWINDOW)
GUICtrlCreateLabel("Pick Network Connection", 8, 8, 157, 17, $SS_CENTER)
$Combo1 = GUICtrlCreateCombo("", 8, 24, 161, 21)
$Button1 = GUICtrlCreateButton("Enable", 8, 56, 75, 25)
$Button2 = GUICtrlCreateButton("Disable", 96, 56, 75, 25)

;Get Network Connection Names
$base = "HKLM\SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
While 1
    $i += 1
    $key = RegEnumKey($base, $i)
    If @error <> 0 Then ExitLoop
    $name = RegRead($base & "\" & $key & "\Connection", "Name")
    If StringLeft($key, 1) = "{" Then GUICtrlSetData($Combo1, $name & "|")
WEnd

GUISetState(@SW_SHOW)
While 1
    $msg = GuiGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        ExitLoop
    Case $Button1
        If GUICtrlRead($Combo1) <> '' Then NicToggle(1, GUICtrlRead($Combo1))
    Case $Button2
        If GUICtrlRead($Combo1) <> '' Then NicToggle(0, GUICtrlRead($Combo1))
    Case Else
        ;;;;;;;
    EndSwitch
WEnd
Exit

Func NicToggle($Toggle, $sConnectionName = "Local Area Connection", $sNetworkFolder = "Network Connections")    
    $ssfCONTROLS = 3
    $sEnableVerb = "En&able"
    $sDisableVerb = "Disa&ble"
    $shellApp = ObjCreate("shell.application")
    $oControlPanel = $shellApp.Namespace ($ssfCONTROLS)
    $oNetConnections = "nothing"
    
    For $folderitem In $oControlPanel.items
        If $folderitem.name = $sNetworkFolder Then
            $oNetConnections = $folderitem.getfolder
            ExitLoop
        EndIf
    Next
    If $oNetConnections = "nothing" Then
        MsgBox(48, "Error", "Couldn't find " & $sNetworkFolder & " folder")
        Exit
    EndIf
    
    $oLanConnection = "nothing"
    For $folderitem In $oNetConnections.items
        If StringLower($folderitem.name) = StringLower($sConnectionName) Then
            $oLanConnection = $folderitem
            ExitLoop
        EndIf
    Next
    If $oLanConnection = "nothing" Then
        MsgBox(48, "Error", "Couldn't find '" & $sConnectionName & "' item")
        Exit
    EndIf
    
    $bEnabled = True
    $oEnableVerb = "nothing"
    $oDisableVerb = "nothing"
    $s = "Verbs: " & @CRLF
    For $verb In $oLanConnection.verbs
        $s = $s & @CRLF & $verb.name
        
    ;enables
        If $verb.name = $sEnableVerb And $Toggle = 1 Then
            $oEnableVerb = $verb
            $oEnableVerb.DoIt
            ExitLoop
            
        ;disables
        ElseIf $verb.name = $sDisableVerb And $Toggle = 0 Then
            $oDisableVerb = $verb
            $oDisableVerb.DoIt
            ExitLoop
        Else
            MsgBox(48, "Error", "Tried to disable when already disabled" & @CRLF & "or enable when already enabled")
            Exit
        EndIf
    Next
    Sleep(1000)
EndFunc  ;==>NicToggle
The NicToggle UDF I think it was made by Wus you can change the msgbox inside the UDF for return(#) that you wants and remove EXIT.

Hope this will help you....

Edit: fixed typo...

Edited by Danny35d
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

You mention that the script will runas adminitrator, so you can query the register to get the network connection names.

#include <GUIConstants.au3>

Dim $i, $base, $key, $name

; == GUI generated with Koda ==
$Form1 = GUICreate("Nic", 176, 89, -1, -1, -1, $WS_EX_TOOLWINDOW)
GUICtrlCreateLabel("Pick Network Connection", 8, 8, 157, 17, $SS_CENTER)
$Combo1 = GUICtrlCreateCombo("", 8, 24, 161, 21)
$Button1 = GUICtrlCreateButton("Enable", 8, 56, 75, 25)
$Button2 = GUICtrlCreateButton("Disable", 96, 56, 75, 25)

;Get Network Connection Names
$base = "HKLM\SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
While 1
    $i += 1
    $key = RegEnumKey($base, $i)
    If @error <> 0 Then ExitLoop
    $name = RegRead($base & "\" & $key & "\Connection", "Name")
    If StringLeft($key, 1) = "{" Then GUICtrlSetData($Combo1, $name & "|")
WEnd

GUISetState(@SW_SHOW)
While 1
    $msg = GuiGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        ExitLoop
    Case $Button1
        If GUICtrlRead($Combo1) <> '' Then NicToggle(1, GUICtrlRead($Combo1))
    Case $Button2
        If GUICtrlRead($Combo1) <> '' Then NicToggle(0, GUICtrlRead($Combo1))
    Case Else
        ;;;;;;;
    EndSwitch
WEnd
Exit

Func NicToggle($Toggle, $sConnectionName = "Local Area Connection", $sNetworkFolder = "Network Connections")    
    $ssfCONTROLS = 3
    $sEnableVerb = "En&able"
    $sDisableVerb = "Disa&ble"
    $shellApp = ObjCreate("shell.application")
    $oControlPanel = $shellApp.Namespace ($ssfCONTROLS)
    $oNetConnections = "nothing"
    
    For $folderitem In $oControlPanel.items
        If $folderitem.name = $sNetworkFolder Then
            $oNetConnections = $folderitem.getfolder
            ExitLoop
        EndIf
    Next
    If $oNetConnections = "nothing" Then
        MsgBox(48, "Error", "Couldn't find " & $sNetworkFolder & " folder")
        Exit
    EndIf
    
    $oLanConnection = "nothing"
    For $folderitem In $oNetConnections.items
        If StringLower($folderitem.name) = StringLower($sConnectionName) Then
            $oLanConnection = $folderitem
            ExitLoop
        EndIf
    Next
    If $oLanConnection = "nothing" Then
        MsgBox(48, "Error", "Couldn't find '" & $sConnectionName & "' item")
        Exit
    EndIf
    
    $bEnabled = True
    $oEnableVerb = "nothing"
    $oDisableVerb = "nothing"
    $s = "Verbs: " & @CRLF
    For $verb In $oLanConnection.verbs
        $s = $s & @CRLF & $verb.name
        
    ;enables
        If $verb.name = $sEnableVerb And $Toggle = 1 Then
            $oEnableVerb = $verb
            $oEnableVerb.DoIt
            ExitLoop
            
        ;disables
        ElseIf $verb.name = $sDisableVerb And $Toggle = 0 Then
            $oDisableVerb = $verb
            $oDisableVerb.DoIt
            ExitLoop
        Else
            MsgBox(48, "Error", "Tried to disable when already disabled" & @CRLF & "or enable when already enabled")
            Exit
        EndIf
    Next
    Sleep(1000)
EndFunc  ;==>NicToggle
The NicToggle UDF I think it was made by Wus you can change the msgbox inside the UDF for return(#) that you wants and remove EXIT.

Hope this will help you....

Edit: fixed typo...

That's quite the awesomeness, i cut and pasted the script and it did in fact work. I'll have to rewrite it to fit our needs at work.

Thanks so much for posting it, did you write the script yourself? could you give me resources as to how i could learn how to invoke shell scripting withing autoit?

thanks again!

-Blademonkey

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Link to comment
Share on other sites

  • 1 month later...

Try this!

$ret = LanToggle('Local Area Connection')

Select
 Case $ret = 1
  MsgBox(0, '', 'Done!')
 Case Else
  MsgBox(0, '', '"Lan Area Connection" is not available')
EndSelect


Func LanToggle($lanconnection)
 $status = ''
 $status_ = ''
 $objwmiservice = ObjGet('winmgmts:\\localhost\root\CIMV2')
 $colitems = $objWMIService.ExecQuery ('SELECT * FROM Win32_NetworkAdapter', 'WQL', 0x10 + 0x20)
 If IsObj($colitems) Then
  For $objitem In $colitems
   If String($objitem.netconnectionid) = $lanconnection Then
    $status = Not (Not ($objitem.netconnectionstatus))
    ExitLoop
   EndIf
  Next
 EndIf
 $shellapp = ObjCreate('shell.application')
 $ocontrolpanel = $shellApp.Namespace (3)
 For $folderitem In $ocontrolpanel.items
  If $folderitem.name = 'Network Connections' Then
   For $folderitem In $folderitem.getfolder.items
    If StringLower($folderitem.name) = StringLower($lanconnection) Then
     For $verb In $folderitem.verbs
      If $verb.name = 'En&able' Or $verb.name = 'Disa&ble' Then
       $verb.doit
       $exitlantoggle = 0
       While $exitlantoggle = 0
        Sleep(1)
        $colitems = $objWMIService.ExecQuery ('SELECT * FROM Win32_NetworkAdapter', 'WQL', 0x10 + 0x20)
        If IsObj($colitems) Then
         For $objitem In $colitems
          If String($objitem.netconnectionid) = $lanconnection Then $status_ = Not (Not ($objitem.netconnectionstatus))
          If $status = Not ($status_) Then
           $exitlantoggle = 1
           ExitLoop
          EndIf
         Next
        EndIf
       WEnd
       Return 1
      EndIf
     Next
    EndIf
   Next
  EndIf
 Next
 Return 0
EndFunc   ;==>LanToggle
Link to comment
Share on other sites

  • 1 year later...

Hey Guys,

thank you very much for your great efforts. I hope being able to motivate you for once again getting into this topic...because: have you noticed that non of these fine pieces of code work under Vista or Server 2008?

It would be great if you could look over your codes to adapt them to work under the new OSses as well.

Best Regards,

Chris

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...