Sign in to follow this  
Followers 0
lsakizada

Please help correct this function

8 posts in this topic

I have got this function from the forum but it is not working on win7 (SP1).

Can you please help to correct it so it will work on win 7 (This used to work on win XP)

Thanks in advanced.

$lanconnection ="Local Area Connection"
SwitchConnection($lanconnection)

Func SwitchConnection($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
                            $exitSwitchConnection = 0
                            While $exitSwitchConnection = 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
                                            $exitSwitchConnection = 1
                                            ExitLoop
                                        EndIf
                                    Next
                                EndIf
                            WEnd
                            Return 1
                        EndIf
                    Next
                EndIf
            Next
        EndIf
    Next
    Return 0
EndFunc   ;==>SwitchConnection

Be Green Now or Never (BGNN)!

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

 

Do you have enough rights ?
Try with #RequireAdmin at the beginning
 

 

 

I just found that the issue is about this line "   $folderitem.name = 'Network Connections'. Probably this will not work on Win 7. Can not browse and find name of this folder's item on win 7...

So I guess I need other solution that will do the same functionality on windows 7.e.g. getting status and connect/disconnect the connection ID.

Hope there is other solution then netsh.exe

Edited by lsakizada

Be Green Now or Never (BGNN)!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

According to this link I can use the WMI by using the Win32_NetworkAdapter class and Enable/Disable Methods.

But how do I using these methods?

 

I added this simple function:

Func _AdapterConnections($sComputer = @ComputerName)
    $LanName ="Local Area Connection"

    $oWMIService = ObjGet("winmgmts:\\" & $sComputer & "\")
    Local $oColItems = $oWMIService.ExecQuery('Select * From Win32_NetworkAdapter', 'WQL', 0x30)
    If IsObj($oColItems) Then
        For $oObjItem In $oColItems
            If StringUpper($oObjItem.PhysicalAdapter) = StringUpper("False") Then ; Ensures that the connections are physical adapters.
                ContinueLoop
            EndIf
            ;MsgBox(0,0,$oColItems.NetEnabled)
            ConsoleWrite($oObjItem.NetEnabled & @LF)
            If StringStripWS($oObjItem.NetConnectionID, 8) = "" Then ; Ensures that the connections id is not empty
                ContinueLoop
            EndIf

            If $oObjItem.NetConnectionID = $LanName Then

                If $oObjItem.NetEnabled Then

                    ;//DISABLE CONNECTION ; NEED TO FINISH THIS LINE
                Else

                    ;//ENABLE CONNECTION ; NEED TO FINISH THIS LINE
                EndIf

            EndIf
        Next
    EndIf

EndFunc   ;==>_AdapterConnections
Edited by lsakizada

Be Green Now or Never (BGNN)!

Share this post


Link to post
Share on other sites

Have you tried $oObjTem.Disable?


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Have you tried $oObjTem.Disable?

It is not working.

_AdapterConnections()
Func _AdapterConnections($sComputer = @ComputerName)
    $LanName ="Local Area Connection"
    $oWMIService = ObjGet("winmgmts:\\" & $sComputer & "\")
    Local $oColItems = $oWMIService.ExecQuery('Select * From Win32_NetworkAdapter', 'WQL', 0x30)
    If IsObj($oColItems) Then
        For $oObjItem In $oColItems
            If StringUpper($oObjItem.PhysicalAdapter) = StringUpper("False") Then ; Ensures that the connections are physical adapters.
                ContinueLoop
            EndIf
            ;MsgBox(0,0,$oColItems.NetEnabled)
            ;ConsoleWrite($oObjItem.NetEnabled & @LF)
            If StringStripWS($oObjItem.NetConnectionID, 8) = "" Then ; Ensures that the connections id is not empty
                ContinueLoop
            EndIf

            If $oObjItem.NetConnectionID = $LanName Then
                If $oObjItem.NetEnabled Then
                    ;MsgBox(0,0,$oObjItem.NetEnabled)
                    $oObjItem.Disable ;;;;// does not disable
                Else
                    MsgBox(0,1,$oObjItem.NetEnabled)
                    $oObjItem.Enable
                EndIf
            EndIf
        Next
    EndIf
EndFunc   ;==>_AdapterConnections

Be Green Now or Never (BGNN)!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

 

You can have a look at the >network UDF

Try with :

#Include "network.au3"
_DisableNetAdapter("Local Area Connection")

 

Hi jguinch

It's a nice UDF with nice coding. I will probably will use the functions that related to the DHCP.

But, I finally succeeded to overcome my problem. The second example that I provided is working if running the script as admin. I put #RequireAdmin in the head of the script and the NIC disabled.

Thank you very much and thanks also to JLogan3o13

This is the script that works:

#RequireAdmin
_AdapterConnections()
Func _AdapterConnections($sComputer = @ComputerName)
    $LanName ="Local Area Connection"
    $oWMIService = ObjGet("winmgmts:\\" & $sComputer & "\")
    Local $oColItems = $oWMIService.ExecQuery('Select * From Win32_NetworkAdapter', 'WQL', 0x30)
    If IsObj($oColItems) Then
        For $oObjItem In $oColItems
            If StringUpper($oObjItem.PhysicalAdapter) = StringUpper("False") Then ; Ensures that the connections are physical adapters.
                ContinueLoop
            EndIf
            If StringStripWS($oObjItem.NetConnectionID, 8) = "" Then ; Ensures that the connections id is not empty
                ContinueLoop
            EndIf
            If $oObjItem.NetConnectionID = $LanName Then
                If $oObjItem.NetEnabled Then
                    $oObjItem.Disable
                Else
                    $oObjItem.Enable
                EndIf
            EndIf
        Next
    EndIf
EndFunc   ;==>_AdapterConnections
Edited by lsakizada

Be Green Now or Never (BGNN)!

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