Sign in to follow this  
Followers 0
neonjacob

How to Enable a Particular LAN connection from multiple connections using vb script

14 posts in this topic

I have two Lan Cards and a wireless lan card in my computer. I have automated some tasks in my system using multiple scripts using Schedule Task. But I am facing one problem in fine-tuning the execution of the scripts in the way I would like them to. Everything goes fine if the system runs 24X7 continuously with a short reboot(again by another vb script) at a designated time, but problem occurs if the system hangs and has to be manually restarted.

I just need a script using which (Say if I run That at Every STARTUP) 'm able to enable 'one particular' LAN Connection and disabling all other LAN or Wireless LAN Connection at one go. Can anyone please help me out with this.

Any help would be greatly appreciated.

Thank you.

Share this post


Link to post
Share on other sites



On more thing that I should mention here is that I need something that would work on win2000, win2003 and winXP

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Check out if those help:

#include <Array.au3>

; System data
Local $s_OSver = @OSVersion
Global $s_ComputerName = @ComputerName

; Network subsystem data
Global $s_FolderName = ""
Global $o_FolderObject = ""
Global $s_ConnSelected = ""
Global $a_ConnectionList[1]
Global $s_Enable = "En&able"
Global $s_Disable = "Disa&ble"

; WMI flags
Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20
Global $wbemFlags = $wbemFlagForwardOnly + $wbemFlagReturnImmediately

; Function _NicToggleOLD to Disable and Enable a Network card using 'Shell.Application' - Parameters: $oLanConnection and $iFlag
; ==============================================================================================
;  _NicToggleOLD('local area connection', 0)
;  $iFlag = 0 Disable network interface
;  $iFlag = 1 Enable network interface
Func _NicToggleOLD($s_Networks, $iFlag)
    For $c_Connection In $o_FolderObject.Items
        If $c_Connection.Name = $s_Networks Then
            For $c_Verb In $c_Connection.verbs
                If $iFlag = 0 And $c_Verb.name = $s_Disable Then
                    $c_Verb.DoIt
                    Sleep(200)
                    Return 0
                EndIf
                If $iFlag = 1 And $c_Verb.name = $s_Enable Then
                    $c_Verb.DoIt
                    Sleep(200)
                    Return 1
                EndIf
            Next
        EndIf
    Next
EndFunc   ;==>_NicToggleOLD

; Function _NicToggleNEW to Disable and Enable a Network card using 'Shell.Application' - Parameters: $oLanConnection and $iFlag
; ==============================================================================================
;  _NicToggleNEW('local area connection', 0)
;  $iFlag = 0 Disable network interface
;  $iFlag = 1 Enable network interface
Func _NicToggleNEW($s_Networks, $iFlag)
    $o_WMIService = ObjGet("winmgmts:\\" & $s_ComputerName & "\root\CIMV2")
    $o_FolderObject = $o_WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", $wbemFlags)
    If IsObj($o_FolderObject) Then
        For $c_Connection In $o_FolderObject
            If $c_Connection.NetConnectionID = $s_Networks Then
                If $iFlag = 0 And $c_Connection.NetEnabled = True Then
                    $c_Connection.Disable                   
                ElseIf $iFlag = 1 And $c_Connection.NetEnabled = False Then
                    $c_Connection.Enable
                EndIf
                ExitLoop
            EndIf
        Next
    Else
        MsgBox(16, "D5100 PostInstall", "No WMI Objects Found for class: " & "Win32_NetworkAdapter")
    EndIf
EndFunc   ;==>_NicToggleNEW

; Function _NWConections to find the network connection objects
; ==============================================================================================
; Depending on the version of the local OS a different approach is taken to query the existing
; Local Area Connections on the system: On every OS from including Windows XP "WMI" can be used,
; but due to the fact that on Windows 2000 the "LIKE" operator is not available in WQL and also
; the method "$o_ConnectionItem.NetConnectionID" does not exist, the way via "Shell.Application"
; has to be used. 1st the system folder containing the network connections is queried, then the
; objects within it. Resulting value of the function always is "$a_ConnectionList", an array of
; all the found network connections.
; On every OS from including Windows XP all the hardware and VMware connections are included, on 
; Windows 2000 all connections minus only the "Make New Connection"-wizard.
Func _NWConections($s_OSver)
    Switch $s_OSver
        Case "WIN_2000"
            Local $s_ConnectionItemName
            $o_Shell = ObjCreate("Shell.Application")
            $s_FolderName = "Network and Dial-up Connections"
            $o_ShellNamespace = $o_Shell.Namespace(3)
            For $c_ClassNames In $o_ShellNamespace.Items
                If $c_ClassNames.Name = $s_FolderName Then
                    $o_FolderObject = $c_ClassNames.GetFolder
                EndIf
            Next
            For $o_ConnectionItem In $o_FolderObject.Items
                If StringInStr($o_ConnectionItem.Name, "Make New Connection") = 0 Then
                    $s_ConnectionItemName = $s_ConnectionItemName & $o_ConnectionItem.Name & "|"
                EndIf
            Next
            $s_ConnectionItemName = StringLeft($s_ConnectionItemName, StringLen($s_ConnectionItemName) - 1)
            $a_CollectedConnections = StringSplit($s_ConnectionItemName, "|")
            For $o_ConnectionItem In $a_CollectedConnections
                _ArrayAdd($a_ConnectionList, $o_ConnectionItem)
            Next
            _ArrayDelete($a_ConnectionList, 0)
            _ArrayDelete($a_ConnectionList, 0)
            _ArraySort($a_ConnectionList, 0)
            
        Case "WIN_2003" Or "WIN_XP"
            $o_ShellApplication = ObjCreate("Shell.Application")
            $s_FolderString = "Network Connections"
            $o_ShellApplicationNamespace = $o_ShellApplication.Namespace(3)
            For $c_Connection In $o_ShellApplicationNamespace.Items
                If $c_Connection.Name = $s_FolderString Then 
                $o_FolderObject = $c_Connection.GetFolder
            EndIf
            Next
            
            $o_WMIService = ObjGet("winmgmts:\\" & $s_ComputerName & "\root\CIMV2")
            $a_CollectedConnections = $o_WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID LIKE " & """[Local]%""" & "OR NetConnectionID LIKE " & """[VMware]%""", "WQL", $wbemFlags)
            For $o_ConnectionItem In $a_CollectedConnections
                _ArrayAdd($a_ConnectionList, $o_ConnectionItem.NetConnectionID)
            Next
            _ArrayDelete($a_ConnectionList, 0)
            _ArraySort($a_ConnectionList, 0)
        
        Case "WIN_2008" Or "WIN_VISTA"
            $o_WMIService = ObjGet("winmgmts:\\" & $s_ComputerName & "\root\CIMV2")
            $a_CollectedConnections = $o_WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID LIKE " & """[Local]%""" & "OR NetConnectionID LIKE " & """[VMware]%""", "WQL", $wbemFlags)
            For $o_ConnectionItem In $a_CollectedConnections
                _ArrayAdd($a_ConnectionList, $o_ConnectionItem.NetConnectionID)
            Next
            _ArrayDelete($a_ConnectionList, 0)
            _ArraySort($a_ConnectionList, 0)
    EndSwitch

    Return $a_ConnectionList
EndFunc   ;==>_NWConections

In my environment the functions work, maybe you'll have to tweak a bit. The code above does not contain function calls, but here they are:

CODE
If $s_OSver = "WIN_2000" Or $s_OSver = "WIN_XP" Or $s_OSver = "WIN_2003" Then

_NicToggleOLD($s_ConnSelected, 0)

Else

_NicToggleNEW($s_ConnSelected, 0)

EndIf

and:

CODE
If $s_OSver = "WIN_2000" Or $s_OSver = "WIN_XP" Or $s_OSver = "WIN_2003" Then

Sleep(5000)

_NicToggleOLD($s_ConnSelected, 1)

Else

Sleep(5000)

_NicToggleNEW($s_ConnSelected, 1)

EndIf

You need to replace "$s_ConnSelected" by the name of the Lan connection you want to disable.

Regards,

Chris

Edited by cherdeg

Share this post


Link to post
Share on other sites

Hey: does my code suite your needs? A small piece of feedback would make me quite happy...

Share this post


Link to post
Share on other sites

Hey: does my code suite your needs? A small piece of feedback would make me quite happy...

Thank you very much for your response and a Bigger SORRY for the late feedback from my end. Actually I have a very tight schedule at my workplace and though the above problem was indeed a big one for me, I actually had time only today to see your reply in here.

I am writing this right as soon as I saw your reply. I haven't tested your script yet but I am going to do it right now.

I will surely return with the result.

Thanks Again

Share this post


Link to post
Share on other sites

Hello CHRIS,

First of all THANK YOU VERY MUCH for your response and your script.

I actually forgot to mention one more fact while posting the topic, which is I am not a Good Programmer. I tested your script in my environment but it did not work for meInfact I should say it was rather my inability to get it work.

What I can understand from your reply is that the first code mentioned by you is not complete without the functions calls you have provided after that. But the problem us I don't know where and how to call those functions in the script.

So please help me out on this too.

I have Two LAN card on that on that machine. One connection is named 'LAN' and the other one is named 'NET'. All I want is that irrespective of the condition of the system at the time of last Shut Down (i.e. wether 'LAN' or 'NET' was enabled at that time), when the system starts, Your SCRIPT runs at EVERY STARTUP and see's which one is ENABLED at that particular time and : - SAY

1) if 'LAN' is enabled and 'NET' is disabled the script does nothing

and,

2) if 'NET' is enabled at the time of startup, the scripts DISABLES the 'NET' connection and ENABLES the 'LAN' connection

This is what I am looking for in XP,2000 and 2003 environment.

Can you please help me out with this (i.e- By adding the Functions along with the Script)

Pardon if I sound too ROOKIE, because that is what I am In the field of Programming.

Loads of Thanks again

Abhi :)

Share this post


Link to post
Share on other sites

Hello CHRIS,

First of all THANK YOU VERY MUCH for your response and your script.

I actually forgot to mention one more fact while posting the topic, which is I am not a Good Programmer. I tested your script in my environment but it did not work for meInfact I should say it was rather my inability to get it work.

What I can understand from your reply is that the first code mentioned by you is not complete without the functions calls you have provided after that. But the problem us I don't know where and how to call those functions in the script.

So please help me out on this too.

I have Two LAN card on that on that machine. One connection is named 'LAN' and the other one is named 'NET'. All I want is that irrespective of the condition of the system at the time of last Shut Down (i.e. wether 'LAN' or 'NET' was enabled at that time), when the system starts, Your SCRIPT runs at EVERY STARTUP and see's which one is ENABLED at that particular time and : - SAY

1) if 'LAN' is enabled and 'NET' is disabled the script does nothing

and,

2) if 'NET' is enabled at the time of startup, the scripts DISABLES the 'NET' connection and ENABLES the 'LAN' connection

This is what I am looking for in XP,2000 and 2003 environment.

Can you please help me out with this (i.e- By adding the Functions along with the Script)

Pardon if I sound too ROOKIE, because that is what I am In the field of Programming.

Loads of Thanks again

Abhi :)

Please Tell me if you need more info about my Environment to solve this problem.

Share this post


Link to post
Share on other sites

I actually forgot to mention one more fact while posting the topic, which is I am not a Good Programmer. I tested your script in my environment but it did not work for me. Infact I should say it was rather my inability to get it work.

I'm no good coder also...some guys here probably lol on my code...I finished the piece for you...checkout this code:

#include <Array.au3>

; System data
Local $s_OSver = @OSVersion
Global $s_ComputerName = @ComputerName

; Network subsystem data
Global $s_FolderName = ""
Global $o_FolderObject = ""
Global $s_ConnSelected = ""
Global $s_Enable = "En&able"
Global $s_Disable = "Disa&ble"
Global $a_ConnectionList[1]

; WMI flags
Global $h_wbemFlagReturnImmediately = 0x10
Global $h_wbemFlagForwardOnly = 0x20
Global $h_wbemFlags = $h_wbemFlagForwardOnly + $h_wbemFlagReturnImmediately

; Your configuration data, change to match your environment (only the characters inside the []!)
Global $s_1st_Connection = "LAN"
Global $s_1st_ConnectionFilter = '"[LAN]%"'
Global $s_2nd_Connection = "NET"
Global $s_2nd_ConnectionFilter = '"NET]%"'




; ==============================================================================================
; Here starts the "main" script to "happen"
; ==============================================================================================

; Use the function _NWConnections below to animate the array "$a_Connections" (aka "fill it with values")
$a_Connections = _NWConnections($s_OSver)

; Show the array (these two lines may be deleted)
_ArrayDisplay($a_Connections)

; Check each record of the array once
For $i = 0 To UBound($a_Connections) - 1
    ; If the current record is the 1st important connection,
    If $a_Connections[$i][0] = $s_1st_Connection Then
        ; Check each record of the array once again
        For $j = 0 To UBound($a_Connections) - 1
            ; If the new current record is the 2nd important connection,
            If $a_Connections[$j][0] = $s_2nd_Connection Then
                ; Check if it is online. If it is (2), then
                If $a_Connections[$j][1] = 2 Then
                    MsgBox(64, "neonJACOB Nic-Switcher", $a_Connections[$j][0] & " is online, so I'm gonna switch it OFF.", 5)
                    ; Check for the host's operating system to choose the right Toggle-Function
                    If $s_OSver = "WIN_2000" Or "WIN_XP" Or "WIN_2003" Then
                        ; and switch it off
                        _NicToggleOLD($a_Connections[$j][0], 0)
                        Sleep(5000)
                    Else
                        ; and switch it off
                        _NicToggleNEW($a_Connections[$j][0], 0)
                        Sleep(5000)
                    EndIf
                EndIf
            EndIf
        Next
        ; If the current record (remember, this is the 1st important connection) is offline (0), then
        If $a_Connections[$i][1] = 0 Then
            MsgBox(64, "neonJACOB Nic-Switcher", $a_Connections[$i][0] & " is offline, so I'm gonna switch it ON.", 5)
            ; Check for the host's operating system to choose the right Toggle-Function
            If $s_OSver = "WIN_2000" Or "WIN_XP" Or "WIN_2003" Then
                ; and switch it on
                _NicToggleOLD($a_Connections[$i][0], 1)
                Sleep(10000)
            Else
                ; and switch it on
                _NicToggleNEW($a_Connections[$i][0], 1)
                Sleep(10000)
            EndIf       
        EndIf
    EndIf
Next

; ==============================================================================================
; Here ends the "main" script
; ==============================================================================================




; Function _NicToggleOLD to Disable and Enable a Network card using 'Shell.Application'
; ==============================================================================================
;  _NicToggleOLD('local area connection', 0)
;  $iFlag = 0 Disable network interface
;  $iFlag = 1 Enable network interface
Func _NicToggleOLD($s_Networks, $iFlag)
    $o_ShellApplication = ObjCreate("Shell.Application")
    $s_FolderString = "Network Connections"
    $o_ShellApplicationNamespace = $o_ShellApplication.Namespace(3)
    For $c_Connection In $o_ShellApplicationNamespace.Items
        If $c_Connection.Name = $s_FolderString Then 
        $o_FolderObject = $c_Connection.GetFolder
    EndIf
    Next
    For $c_Connection In $o_FolderObject.Items
        If $c_Connection.Name = $s_Networks Then
            For $c_Verb In $c_Connection.verbs
                If $iFlag = 0 And $c_Verb.name = $s_Disable Then
                    $c_Verb.DoIt
                    Sleep(200)
                    Return 0
                EndIf
                If $iFlag = 1 And $c_Verb.name = $s_Enable Then
                    $c_Verb.DoIt
                    Sleep(200)
                    Return 1
                EndIf
            Next
        EndIf
    Next
EndFunc   ;==>_NicToggleOLD

; Function _NicToggleNEW to Disable and Enable a Network card using WMI on Windows Server 2008 / Vista
; ==============================================================================================
;  _NicToggleNEW('local area connection', 0)
;  $iFlag = 0 Disable network interface
;  $iFlag = 1 Enable network interface
Func _NicToggleNEW($s_Networks, $iFlag)
    $o_WMIService = ObjGet("winmgmts:\\" & $s_ComputerName & "\root\CIMV2")
    $o_FolderObject = $o_WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", $h_wbemFlags)
    If IsObj($o_FolderObject) Then
        For $c_Connection In $o_FolderObject
            If $c_Connection.NetConnectionID = $s_Networks Then
                If $iFlag = 0 And $c_Connection.NetEnabled = True Then
                    $c_Connection.Disable                   
                ElseIf $iFlag = 1 And $c_Connection.NetEnabled = False Then
                    $c_Connection.Enable
                EndIf
                ExitLoop
            EndIf
        Next
;~     Else
;~         MsgBox(16, "D5100 PostInstall", "No WMI Objects Found for class: " & "Win32_NetworkAdapter")
    EndIf
EndFunc   ;==>_NicToggleNEW

; Function _NWConnections to find the folder containing the network connection objects
; ==============================================================================================
; Depending on the version of the local OS a different approach is taken to query the existing
; Local Area Connections on the system: On every OS from including Windows XP "WMI" can be used,
; but due to the fact that on Windows 2000 the "LIKE" operator is not available in WQL and also
; the method "$o_ConnectionItem.NetConnectionID" does not exist, the way via "Shell.Application"
; has to be used. 1st the system folder containing the network connections is queried, then the
; objects within it. Resulting value of the function always is "$a_ConnectionList", an array of
; all the found network connections.
Func _NWConnections($s_OSver)
    Switch $s_OSver
        Case "WIN_2000"
            Local $s_ConnectionItemName
            $o_Shell = ObjCreate("Shell.Application")
            $s_FolderName = "Network and Dial-up Connections"
            $o_ShellNamespace = $o_Shell.Namespace(3)
            For $c_ClassNames In $o_ShellNamespace.Items
                If $c_ClassNames.Name = $s_FolderName Then
                    $o_FolderObject = $c_ClassNames.GetFolder
                EndIf
            Next
            For $o_ConnectionItem In $o_FolderObject.Items
                If StringInStr($o_ConnectionItem.Name, "Make New Connection") = 0 Then
                    $s_ConnectionItemName = $s_ConnectionItemName & $o_ConnectionItem.Name & "|"
                EndIf
            Next
            $s_ConnectionItemName = StringLeft($s_ConnectionItemName, StringLen($s_ConnectionItemName) - 1)
            $a_CollectedConnections = StringSplit($s_ConnectionItemName, "|")
            For $o_ConnectionItem In $a_CollectedConnections
                _ArrayAdd($a_ConnectionList, $o_ConnectionItem)
            Next
            _ArrayDelete($a_ConnectionList, 0)
            _ArrayDelete($a_ConnectionList, 0)
            _ArraySort($a_ConnectionList, 0)
            
        Case Else
            ; Retrieve a reference to a COM object from an existing WMI-Object
            $o_WMIService = ObjGet("winmgmts:\\" & $s_ComputerName & "\root\CIMV2")
            ; On this reference execute a query for a specific WMI-Class, Win32_NetworkAdapter (retrieve all objects belonging to this class)
            $a_CollectedConnections = $o_WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", $h_wbemFlags)
            ; Get the number of the objects retrieved to be able to create a 2-dimensional array (a spreadsheet consisting of N lines and 2 columns)
            Local $i_Counter = 0
            For $o_ConnectionItem In $a_CollectedConnections
                $i_Counter = $i_Counter + 1
            Next
            Local $a_TmpConnections[$i_Counter][2]
            
            ; On the reference from above execute the same query again 'coz the objects from the 1st query are kinda "expired"
            ; Add each object's name and status to the 2-dimensional array created above
            $a_CollectedConnections = $o_WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", $h_wbemFlags)
            ; ...or execute a query only looking up the matching connections (the ones defined above)
;~          $a_CollectedConnections = $o_WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID LIKE " & $s_1st_ConnectionFilter & " OR NetConnectionID LIKE " & $s_2nd_ConnectionFilter, "WQL", $h_wbemFlags)
            Local $i = 0
            For $o_ConnectionItem In $a_CollectedConnections
                If $o_ConnectionItem.NetConnectionID <> "" Then
                    $a_TmpConnections[$i][0] = $o_ConnectionItem.NetConnectionID
                    $a_TmpConnections[$i][1] = $o_ConnectionItem.NetConnectionStatus
                    $i = $i + 1
                EndIf
            Next
            
            ; Reversely check which record of the array contains data and delete the empty ones; finally sort the array alphabetically 
            For $i = UBound($a_TmpConnections) -1 to 0 step -1
                If $a_TmpConnections[$i][0] = "" Then _ArrayDelete($a_TmpConnections, $i)
            Next                    
            _ArraySort($a_TmpConnections, 0)

    EndSwitch

    ; Return the array from this function to the calling "main" script
    Return $a_TmpConnections
EndFunc   ;==>_NWConnections

I tried to comment it as much as I am capable of. But in regard to the fact that the code for 2K, XP and 2K3 is not mine, I can't perfectly explain it - so I didn't try. On my machines from 2K up to 2K8/Vista the script works: If it runs (e.g. if the user logs on to a domain where e.g. a login batch calls this file), the defined DSL- or WLAN-connection is taken offline and the Local Area Connection goes online.

Best Regards,

Chris

Share this post


Link to post
Share on other sites

Thank you CHRIS, for all your time.

I will test it right away.

Share this post


Link to post
Share on other sites

Hi CHRIS,

Thank you very much for the time you took out for giving me the CODE.

Now here's what I did. As I said I am not A programmer and was looking for some BAT or VB Script to do the job (For me that would have been easy), I initially thought your script to be a VB Script and just tried to save the script as .VBS and run the code. And as it should have been, it didn't work.

Then I opened the script and looked at the first line of your script and saw this '#include <Array.au3>'

From my limited knowledge of programming I understood what mistake I was doing so I Immediately dowloaded AutoIt v3 and installed it. I renamed the file to .au3 and ran the script from AutoIT v3 and your script worked like a charm.

This is what I was looking for so long. Your code was perfect.

Your code was running fine but I didn't wanted the List of Connection and the Message Boxes to be displayed because that would need closing of the Connection list and Clicking OK in the Message Box manually and i wanted it to run without manual intervention or confirmation. So I changed the code a bit. This is what I did.

I just deleted the following Lines from your code:-

Line 37: _ArrayDisplay($a_Connections)

Line 49: MsgBox(64, "neonJACOB Nic-Switcher", $a_Connections[$j][0] & " is online, so I'm gonna switch it OFF.", 5)

Line 65: MsgBox(64, "neonJACOB Nic-Switcher", $a_Connections[$i][0] & " is offline, so I'm gonna switch it ON.", 5)

This removed the Listing and the Message boxes and the Script ran without them perfectly.

(I did this on a Hit and Trial basis without knowing the outcome and it did work like I wanted it to. I hope I did it correct???)

:) Thank you :) again CHRIS, you saved me a lot of headache.

Regards

Abhi.

Share this post


Link to post
Share on other sites

@cherdog, Thank you for sharing your code.

Can Anyone Verify that the "_NicToggleNEW" function works on Vista? I have it working on 2k8 but on vista it just doesn't do anything with the "$c_Connection.Disable". Also does anyone know of an Enable/Disable Nic Method for 2k ?


There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

Share this post


Link to post
Share on other sites

Func _NicToggleOLD($s_Networks, $iFlag)
    $o_ShellApplication = ObjCreate("Shell.Application")
    $s_FolderString = "Network Connections"
    $o_ShellApplicationNamespace = $o_ShellApplication.Namespace(3)
        ....oÝ÷ Ûú®¢×zËaj×(uçhzÉ÷öÜ(®K§uêÕËZnëZ)è­ÈZ¢±©e¶­ÊÞªè«yÒ7ö÷yéî²)à²)¢ªÜ¡×¢º®×Â¥y«­¢+Ù±½°
½¹ÍÐÀÌØí
M%1}
½¹¹Ñ¥½¹ÌôÐäìÁàÀÀÌÄ
M%0½9Ñݽɬ
½¹¹Ñ¥½¹Ì()Õ¹}9%
á¥ÍÑÌ ÀÌØíÍÑɹÑÉå9µ¤(±½°ÀÌØíIÍÕ±ÐôÀ°ÀÌØí½1¹
½¹¹Ñ¥½¸ôÅÕ½ÐìÅÕ½Ðì°ÀÌØíYÉôÅÕ½ÐìÅÕ½Ðì(±½°ÀÌØíM¡±±ÁÀô=©
ÉÑ ÅÕ½ÐíM¡±°¹ÁÁ±¥Ñ¥½¸ÅÕ½Ðì¤(±½°ÀÌØí½9Ñ
½¹¹Ñ¥½¹ÌôÀÌØíM¡±±ÁÀ¹9µÍÁ ÀÌØí
M%1}
½¹¹Ñ¥½¹Ì¤(%%Í=¨ ÀÌØí½9Ñ
½¹¹Ñ¥½¹Ì¤Q¡¸(½ÈÀÌØí½1¹
½¹¹Ñ¥½¸%¸ÀÌØí½9Ñ
½¹¹Ñ¥½¹Ì¹%ѵÌ(%MÑÉ¥¹1½ÝÈ ÀÌØí½1¹
½¹¹Ñ¥½¸¹9µ¤ôMÑÉ¥¹1½ÝÈ ÀÌØíÍÑɹÑÉå9µ¤Q¡¸(ÀÌØíIÍÕ±ÐôÄ(á¥Ñ1½½À(¹%(9áÐ(¹%(IÑÕɸÀÌØíIÍÕ±Ð)¹Õ¹ìôôÐí}9%
á¥ÍÑÌ

Note the use of the CSIDL value for Network Connections (49) instead of searching the control panel, this works in Vista, XP as well as Win 7 Beta, I haven't tried it on 2k or 2k8


By far, the worst four letter word (swear word) out there has to be USER

Share this post


Link to post
Share on other sites

Hi All!

I'm a newcomer to AutoIt (got wise to it yesterday), but have already managed to practice some coding. Since one of my urging tasks was similar to this one discussed here (toggle LAN connection on and off), I spent some time trying to script that. I've tried some solutions offered here: http://www.autoitscript.com/forum/index.php?showtopic=12034 and here: http://www.autoitscript.com/forum/index.php?showtopic=12645, but they didn't work too fine. So I've found what seems a more robust solution. I don't know if it works on any other OSs other than mine (Windows XP SP3), but it seems to be entirely independent of local languages (it doesn't use "verbs" and system folder names). I just found a free Microsoft product - a console-run device manager called devcon (try googling it or searching in MSDN). This tool can enable / disable any device using WinAPI and given the device's ID (a string value). So my task in AutoIt boiled down to writing a wrapper.

And here it is:

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=LAN_Control_ENG.exe
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>

Global Const $DEVCON = @ScriptDir & "\devcon.exe"

Func Connect($bConnect = True)

    If $bConnect = True Then
        If ShellExecute($DEVCON, "enable ms_pschedmp", "", "", @SW_HIDE) = 1 Then; "ms_pschedmp" is the LAN devices ID
            Return True
        Else
            Return False
        EndIf
    Else
        If ShellExecute($DEVCON, "disable ms_pschedmp", "", "", @SW_HIDE) = 1 Then; "ms_pschedmp" is the LAN devices ID
            Return True
        Else
            Return False
        EndIf
    EndIf

EndFunc  ;==>Connect

$gui = GUICreate("LAN Control", 155, 80)
$gui_label = GUICtrlCreateLabel("Choose option:", 30, 10)
$gui_b_OK = GUICtrlCreateButton("Enable LAN", 5, 40, 70, 30)
$gui_b_Cancel = GUICtrlCreateButton("Disable LAN", 80, 40, 70, 30)

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $gui_b_OK

            If Connect() Then
                MsgBox(64, "OK!", "LAN enabled!")
                ExitLoop
            Else
                MsgBox(48, "Error!", "An error occurred!")
                ExitLoop
            EndIf

        Case $msg = $gui_b_Cancel
            If Connect(False) Then
                MsgBox(64, "OK!", "LAN disabled!")
                ExitLoop
            Else
                MsgBox(48, "Error!", "An error occurred!")
                ExitLoop
            EndIf

        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop

    EndSelect
WEnd

Here's a RAR file containing the above source code, a compiled EXE, and devcon (for i386 systems):

http://narod.ru/disk/7051598000/LanControl.rar.html

Please comment! :D

Share this post


Link to post
Share on other sites

I'm trying to do something similar on a script but I'm having issues with certain members of the Win32_NetworkAdapter Class

For Example, I can access

$c_Connection.NetConnectionStatus

but if I try to access

$c_Connection.NetEnabled

or

$c_Connection.GUID

I get

==> The requested action with this object has failed.:

any ideas why I can access some class member but not others?

Thanks

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