vbscript to enable network connections

G

Guest

I have been using a vbscript in XP to enable/disable network connections that
uses the NAMESPACE method to examine the network control panel ( see code
snippet). However in Vista the network control panel and list of network
devices does not appear to be in the NAMESPACE method any more. The "Network
and Sharing Center" control panel replaced the "Network Connections" in Vista
and the list of connections seems to have disappeared from the namespace
method information. I am sure I am overlooking something simple. Anybody?
Here is a snippet of what I am doing on XP
---snippet-----------------------------------------------------------------------------
Const ssfCONTROLS = 3

sConnectionName = "Local Area Connection"

sEnableVerb = "En&able"
sDisableVerb = "Disa&ble"

set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)

set oNetConnections = nothing
for each folderitem in oControlPanel.items
if folderitem.name = "Network Connections" then
set oNetConnections = folderitem.getfolder: exit for
end if
next

if oNetConnections is nothing then
msgbox "Couldn't find 'Network Connections' folder"
wscript.quit
end if
---------------------------------------------------------------------
 
S

Steve Foster [SBS MVP]

dak said:
I have been using a vbscript in XP to enable/disable network connections
that
uses the NAMESPACE method to examine the network control panel ( see code
snippet). However in Vista the network control panel and list of network
devices does not appear to be in the NAMESPACE method any more. The
"Network
and Sharing Center" control panel replaced the "Network Connections" in
Vista
and the list of connections seems to have disappeared from the namespace
method information. I am sure I am overlooking something simple. Anybody?
Here is a snippet of what I am doing on XP.

http://www.microsoft.com/technet/technetmag/issues/2006/11/HeyScriptingGuy/default.aspx
 
G

Guest

Thanks. This seems to be the information I need. However, I have tried
running the VBscript and it does not seem to really enable the network card.
I am running an evaluation copy build 5744 of Windows Vista Ultimate and I
receive a error code 740 from the line "objitem.Enable" from Figure 4 of the
"Hey Scripting Guy" article:
-------------------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapter Where NetEnabled = 'False'")

For Each objItem in colItems
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "Description: " & objItem.Description
objItem.Enable
Wscript.Echo
Next
 
R

Ramesh, MS-MVP

Same error code seen in Vista RTM. 740 might be related to UAC / Elevation
issues, I guess.

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


Thanks. This seems to be the information I need. However, I have tried
running the VBscript and it does not seem to really enable the network card.
I am running an evaluation copy build 5744 of Windows Vista Ultimate and I
receive a error code 740 from the line "objitem.Enable" from Figure 4 of the
"Hey Scripting Guy" article:
-------------------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapter Where NetEnabled = 'False'")

For Each objItem in colItems
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "Description: " & objItem.Description
objItem.Enable
Wscript.Echo
Next
 
S

Steve Foster [SBS MVP]

Ramesh said:
Same error code seen in Vista RTM. 740 might be related to UAC / Elevation
issues, I guess.

I don't get an error with a standard CMD prompt, or by running the script
directly. But it doesn't do anything.

However, if I run it from an elevated CMD prompt (ie one started with Run
As Administrator), the script works.
 
R

Ramesh, MS-MVP

I don't get an error with a standard CMD prompt

The script terminates silently by default. You need to modify the code
slightly in order to see the error code.

Change the following line:

- - -
objItem.Enable
- - -

to

- - -
rtn=objItem.Enable
msgbox rtn
- - -

or

- - -
msgbox objItem.Enable
- - -

Nice! Then it's an UAC/elevation issue as I thought.

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


Ramesh said:
Same error code seen in Vista RTM. 740 might be related to UAC / Elevation
issues, I guess.

I don't get an error with a standard CMD prompt, or by running the script
directly. But it doesn't do anything.

However, if I run it from an elevated CMD prompt (ie one started with Run
As Administrator), the script works.
 
S

Steve Foster [SBS MVP]

dak said:
So my question is: "Is this working as designed or an oversight?"

I'd say a bit of both. Changing network properties is a privileged
operation, and therefore the script should only work with elevated
privileges. OTOH, you don't get a UAC prompt when running the script, and
you ought to (but I can see how implementing it could be an absolute
nightmare!).
 
R

Ramesh, MS-MVP

The VBscript really should prompt.

Certainly agree. BTW, here is some documentation I found at MSDN:

User Account Control and WMI:
http://msdn2.microsoft.com/en-gb/library/aa826699.aspx

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


I would have to agree. The VBscript really should prompt. I am sure many
administrators have scripted "privileged" operation that are all going to
fail without warning when run on Vista because the default way to run a
VBscript is to double-click. It appears to me this behavior would encourage
administrators that have written these types of script to assist their user
population might begin to simple turn UAC off on the computers that they
administer, so that end users could run these types of scripts. Does anyone
from Microsoft monitor these newgroup? What say you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top