teryx510 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I've been working on a way to enable/disable my windows networking adapters. I found a few scripts out on the web which do the same thing only in vbscript and began working from there.

My script does work...sometimes. It always seems to disable the adapter but most of the time it cannot enable the adapter. Results seem to vary machine to machine on whether enabling works, though it appears that on most, it doesn't work.

I still consider myself a noobie and so I humbly ask for your help in resolving this problem.

use strict; use Win32::OLE("in"); $Win32::OLE::Warn = 3; my $Adapter = 'Local Area Connection'; AdapterReset(); sub AdapterReset { my $OShellConnection = Win32::OLE->new("Shell.Application") or die + "Failure to get Shell.Application." .Win32::OLE->LastError(). "\n"; my $Folder; my ($NetConn, $NetConns); my $NetAdapter; my $verb; my $OShellConnectionControlPanel = $OShellConnection->NameSpace(3) +; foreach $Folder (in $OShellConnectionControlPanel->{Items}) { if($Folder->Name eq "Network Connections") { $NetConns = $Folder->GetFolder; } } if($NetConns == '') { TimedMsg("Network Connections not found"); die "Network Connections not found"; } foreach $NetConn (in $NetConns->{Items}) { if($NetConn->{Name} eq $Adapter) { $NetAdapter = $NetConn; } } if($NetAdapter eq "") { die "$Adapter could not be found."; } foreach $verb (in $NetAdapter->{Verbs}) { if($verb->{Name} eq "En&able") { eval { $verb->DoIt or die print "Failed to Enable ".$verb-> +LastError(). "\n";} } elsif ($verb->{Name} eq "Disa&ble") { eval { $verb->DoIt or die print "Failed to Disable ".$verb- +>LastError(). "\n";} } } if ($@) { print "An error occurred ($@), continuing\n"; } }

I have thus far only only been able to get the following error back:

An error occurred (Win32::OLE(0.1709) error 0x80020011: "Does not support a collection" in METHOD/PROPERTYGET ""

Ideas?

Replies are listed 'Best First'.
Re: Using OLE to enable/disable network adapters in Windows
by Gangabass (Vicar) on Oct 02, 2009 at 15:45 UTC

    Why don't you use plain cmd command (for examplel via system):

    netsh interface set interface <interface name> DISABLED

      Hi Gangabass, Thanks for the reply. I looked at using netsh to do the job but was unable to get it to work as I'm using WinXP.

Re: Using OLE to enable/disable network adapters in Windows
by Bloodnok (Vicar) on Oct 02, 2009 at 20:02 UTC
    One question that immediately comes to my mind is: do the VBscript versions behave in the same way as you observe with your perl translation i.e. equally unreliable starting ?

    If so, then I suspect the ROI is, other than a useful exercise in perl coding, minimal and the best you can hope to acheive is that they both i.e. perl & VBscript, perform equally unreliably.

    A user level that continues to overstate my experience :-))

      Heya Bloodnok, thanks for the reply.

      No, the behaviors between the vbscript version I found on the web and my version are a little different. And by that I mean that their version works without fail and mine well...

        Show the VB version, maybe your translation is off :)