Rich Rumble
asked on
Grab Network info from two different namespaces
I'd like to grab the info from Win32_NetworkAdapterConfig uration Where IPEnabled = True
and use the Description of each result to then query the root\WMI namespace and the NDIS class for those same adapters...
So NetworkAdapterConfiguratio n returns two IP enabled nics:
Description: VMware Accelerated AMD PCNet Adapter - Packet Scheduler Miniport
Description: TAP-Win32 Adapter V9 - Packet Scheduler Miniport
And I'd like to query MSNdis_LinkSpeed where InstanceName = %the_results_from_above%
if that can be done. And if the names for some reason don't match description != instancename then print the speed is "unknown".
Below is the code I'd like to "merge" into one sub if possible, or a function, I don't know which way is best. Let me know if you have any questions!
-rich
and use the Description of each result to then query the root\WMI namespace and the NDIS class for those same adapters...
So NetworkAdapterConfiguratio
Description: VMware Accelerated AMD PCNet Adapter - Packet Scheduler Miniport
Description: TAP-Win32 Adapter V9 - Packet Scheduler Miniport
And I'd like to query MSNdis_LinkSpeed where InstanceName = %the_results_from_above%
if that can be done. And if the names for some reason don't match description != instancename then print the speed is "unknown".
Below is the code I'd like to "merge" into one sub if possible, or a function, I don't know which way is best. Let me know if you have any questions!
-rich
Wscript.Echo "<Network>"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration " & "Where IPEnabled = True")
For Each objItem in colItems
Wscript.Echo "<NIC>"
' Beginning of Adapter specific information
Wscript.Echo "<Caption>" & objItem.Caption & "</Caption>"
If Not IsNull(objItem.DNSDomainSuffixSearchOrder) Then
Dim strDefaultIPGateway : strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
Wscript.Echo "<DefaultIPGateway>" & strDefaultIPGateway & "</DefaultIPGateway>"
End If
Wscript.Echo "<Description>" & objItem.Description & "</Description>"
If Not IsNull(objItem.DNSDomainSuffixSearchOrder) Then
Dim strDNSDomainSuffixSearchOrder : strDNSDomainSuffixSearchOrder = Join(objItem.DNSDomainSuffixSearchOrder, ",")
Wscript.Echo "<DNSSrchOrd>" & strDNSDomainSuffixSearchOrder & "</DNSSrchOrd>"
End If
Wscript.Echo "<DHCPEnabled>" & objItem.DHCPEnabled & "</DHCPEnabled>"
Wscript.Echo "<MACAddress>" & objItem.MACAddress & "</MACAddress>"
Wscript.Echo "<WINSPrimaryServer>" & objItem.WINSPrimaryServer & "</WINSPrimaryServer>"
Wscript.Echo "<WINSSecondaryServer>" & objItem.WINSSecondaryServer & "</WINSSecondaryServer>"
' Beginning IP Address Listing
Wscript.Echo "<IPAddresses>"
Dim i
For i = 0 To UBound (objItem.IPAddress)
Wscript.Echo "<IPAddress>" & objItem.IPAddress(i) & "</IPAddress>"
Wscript.Echo "<IPSubnet>" & objItem.IPSubnet(i) & "</IPSubnet>"
Next
' Writing End of IP Address Listing
Wscript.Echo "</IPAddresses>"
' Writing End of NIC
Wscript.Echo "</NIC>"
And....
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")
Set colItems = objWMIService.ExecQuery("SELECT * FROM MSNdis_LinkSpeed",,48)
For Each objItem In colItems
WScript.Echo "Active: " & objItem.Active
WScript.Echo "InstanceName: " & objItem.InstanceName
WScript.Echo "NdisLinkSpeed: " & objItem.NdisLinkSpeed
WScript.Echo
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked great!
ASKER
How could I do a join so that the xml tag's appear on the same line as the output, see the code below.
-rich
-rich
strComputer = "."
WScript.Echo "<Network>"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration " & "Where IPEnabled = True")
For Each objItem in colItems
Wscript.Echo "<NIC>"
' Beginning of Adapter specific information
Wscript.Echo "<Caption>" & objItem.Caption & "</Caption>"
If Not IsNull(objItem.DNSDomainSuffixSearchOrder) Then
Dim strDefaultIPGateway : strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
Wscript.Echo "<DefaultIPGateway>" & strDefaultIPGateway & "</DefaultIPGateway>"
End If
Wscript.Echo "<Description>" & objItem.Description & "</Description>"
If Not IsNull(objItem.DNSDomainSuffixSearchOrder) Then
Dim strDNSDomainSuffixSearchOrder : strDNSDomainSuffixSearchOrder = Join(objItem.DNSDomainSuffixSearchOrder, ",")
Wscript.Echo "<DNSSrchOrd>" & strDNSDomainSuffixSearchOrder & "</DNSSrchOrd>"
End If
Wscript.Echo "<DHCPEnabled>" & objItem.DHCPEnabled & "</DHCPEnabled>"
Wscript.Echo "<MACAddress>" & objItem.MACAddress & "</MACAddress>"
Wscript.Echo "<WINSPrimaryServer>" & objItem.WINSPrimaryServer & "</WINSPrimaryServer>"
Wscript.Echo "<WINSSecondaryServer>" & objItem.WINSSecondaryServer & "</WINSSecondaryServer>"
' Beginning IP Address Listing
Wscript.Echo "<IPAddresses>"
Dim i
For i = 0 To UBound (objItem.IPAddress)
Wscript.Echo "<IPAddress>" & objItem.IPAddress(i) & "</IPAddress>"
Wscript.Echo "<IPSubnet>" & objItem.IPSubnet(i) & "</IPSubnet>"
Next
' Writing End of IP Address Listing
Wscript.Echo "</IPAddresses>"
' Beginning of LinkSpeed
WScript.Echo "<LinkSpeed>"
LinkSpeed(objItem.Description)
Wscript.Echo "</LinkSpeed>"
' Writing End of NIC
Wscript.Echo "</NIC>"
Next
Sub LinkSpeed(Desc)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")
Set colItems = objWMIService.ExecQuery("SELECT * FROM MSNdis_LinkSpeed Where InstanceName = '" & Desc & "'",,48)
For Each objItem In colItems
WScript.Echo objItem.NdisLinkSpeed
Next
End Sub
ASKER
So instead of this:
<LinkSpeed>
100000
</LinkSpeed>
have this: <LinkSpeed>100000</LinkSpe ed>
Thanks again!
<LinkSpeed>
100000
</LinkSpeed>
have this: <LinkSpeed>100000</LinkSpe
Thanks again!
Change the sub to WScript.Echo "<NdisLinkSpeed>" & objItem.NdisLinkSpeed & "</NdisLinkSpeed>"
Then the result will be
<LinkSpeed>
<NdisLinkSpeed>100000</Ndi
</LinkSpeed>
I just added the <LinkSpeed> Tag as a place holder to call the sub You can remove it or move it to the sub without affecting the script
ASKER
Ahh, I should of tried that, so simple :)
Thanks again!
-rich
Thanks again!
-rich
strComputer = "."
WScript.Echo "<Network>"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration " & "Where IPEnabled = True")
For Each objItem in colItems
Wscript.Echo "<NIC>"
' Beginning of Adapter specific information
Wscript.Echo "<Caption>" & objItem.Caption & "</Caption>"
If Not IsNull(objItem.DNSDomainSuffixSearchOrder) Then
Dim strDefaultIPGateway : strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
Wscript.Echo "<DefaultIPGateway>" & strDefaultIPGateway & "</DefaultIPGateway>"
End If
Wscript.Echo "<Description>" & objItem.Description & "</Description>"
If Not IsNull(objItem.DNSDomainSuffixSearchOrder) Then
Dim strDNSDomainSuffixSearchOrder : strDNSDomainSuffixSearchOrder = Join(objItem.DNSDomainSuffixSearchOrder, ",")
Wscript.Echo "<DNSSrchOrd>" & strDNSDomainSuffixSearchOrder & "</DNSSrchOrd>"
End If
Wscript.Echo "<DHCPEnabled>" & objItem.DHCPEnabled & "</DHCPEnabled>"
Wscript.Echo "<MACAddress>" & objItem.MACAddress & "</MACAddress>"
Wscript.Echo "<WINSPrimaryServer>" & objItem.WINSPrimaryServer & "</WINSPrimaryServer>"
Wscript.Echo "<WINSSecondaryServer>" & objItem.WINSSecondaryServer & "</WINSSecondaryServer>"
' Beginning IP Address Listing
Wscript.Echo "<IPAddresses>"
Dim i
For i = 0 To UBound (objItem.IPAddress)
Wscript.Echo "<IPAddress>" & objItem.IPAddress(i) & "</IPAddress>"
Wscript.Echo "<IPSubnet>" & objItem.IPSubnet(i) & "</IPSubnet>"
Next
' Writing End of IP Address Listing
Wscript.Echo "</IPAddresses>"
' Beginning of LinkSpeed
LinkSpeed(objItem.Description)
' Writing End of NIC
Wscript.Echo "</NIC>"
Next
Sub LinkSpeed(Desc)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")
Set colItems = objWMIService.ExecQuery("SELECT * FROM MSNdis_LinkSpeed Where InstanceName = '" & Desc & "'",,48)
For Each objItem In colItems
WScript.Echo "<LinkSpeed>" & objItem.NdisLinkSpeed & "</LinkSpeed>"
Next
End Sub
Open in new window