Jump to content
Allow2010

problem with wmi query

Recommended Posts

Allow2010

Hi,

I would like to get all ip adresses if all my network adapters, so I tried something like this:

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) Then
    For $objItem In $colItems
        $strIPAddress = $objItem.IPAddress(0)
        ConsoleWrite("IPAddress0: " & $strIPAddress & @CRLF)
        $strIPAddress = $objItem.IPAddress(1)
        ConsoleWrite("IPAddress1: " & $strIPAddress & @CRLF)
        $strIPAddress = $objItem.IPAddress(2)
        ConsoleWrite("IPAddress2: " & $strIPAddress & @CRLF)
        $strIPAddress = $objItem.IPAddress(3)
        ConsoleWrite("IPAddress3: " & $strIPAddress & @CRLF)
    Next
EndIf

it seems that not all networ adapters provide information for more than one ip adress so this keeps failing with the following output:

 

Quote

IPAddress0:
IPAddress1:
IPAddress2:
IPAddress3:
IPAddress0: 192.168.253.1
IPAddress1: fe80::ddde:cfe6:a2d2:155c
"C:\Users\Administrator\Desktop\test.au3" (18) : ==> The requested action with this object has failed.:
$strIPAddress = $objItem.IPAddress(2)
$strIPAddress = $objItem^ ERROR

how can i prevent the error so that the script contines? I never worked with WMI Objects before...

Thanks!

Share this post


Link to post
Share on other sites
water

Add a COM error handler to your script. Teh help file describes this in ObjEvent.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
jguinch

The problem is that you use a non existing array index : $objItem.IPAddress(2)

Check the size of the element and use an loop to enumerate IP adresses :

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If Not IsObj($colItems) Then Exit

For $objItem In $colItems
    ConsoleWrite("[" & $objItem.Description & "]" & @CRLF)
    $aIPAddress = $objItem.IPAddress
    If UBound($aIPAddress) Then
        For $i = 0 To UBound($aIPAddress) - 1
            ConsoleWrite("- " & $aIPAddress[$i] & @CRLF)
        Next
    Else
        ConsoleWrite("! No IP adress" & @CRLF)
    EndIf
Next

 

Share this post


Link to post
Share on other sites
Allow2010

thanks, both answers will help !

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

×