Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A Collection or not a Collection, that is the question. 1

Status
Not open for further replies.

scripto

Technical User
Oct 20, 2003
9
GB
I need to retrieve default gateway network settings. The information is available in Win32_NetworkAdapterConfiguration. Scriptomatic produces the following relevant code.
-----------------
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration",,48)
For Each objItem in colItems
Wscript.Echo "DefaultIPGateway: "
For Each objElement In objItem.DefaultIPGateway
WScript.Echo vbTab & objElement
Next
Next

-----------------

DefaultIPGateway appears to be a collection. This is confirmed by WMI Object Browser which indicates DefaultIPGateway is "array of string"

When I extract the code for use elsewhere, it does not work.

To investigate: I changed the first line of Scriptomatic to
on error GOTO 0

I get the following error message
C:\scriptomatic-network.vbs(7, 5) Microsoft VBScript runtime error: Object not a collection

Question:
Is DefaultIPGateway a collection or not?
Why does Scriptomatic produce errors?

Thanks for your help



 
Hello scripto,

Not all networkadaptorconfiguration possess defaultipgateway property. Why skd documentation does not make it clear, I cannot say. Besides, it won't be the last inaccuracy of the documentation. As to scriptomatic, amid a valuable free tool, it is more for educational purpose than anything else. It is not perfect to say the least, far from it.
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration",,48)
For Each objItem in colItems
    if IsNull(objItem.DefaultIPGateway) then
        wscript.echo "This adapter does not possess DefaultIPGateway property."
    else
        Wscript.Echo "DefaultIPGateway: "
        For Each objElement In objItem.DefaultIPGateway
           WScript.Echo vbTab & objElement
        Next
    end if
Next
set colItems=nothing
set objWMIService=nothing
- tsuji
 
Tsuji

Great response. Thanks, it answers a lot of questions.

If I understand correctly, when an object advertises a property of a particular type e.g. a collection, a reference to the property may return something or may return nothing. Does this mean that, for reliability, you must include code to check if a value has been returned for every reference to every property of every object. This would be very time and code consumining. How can you tell if a property might return nothing.

Scripto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top