Jump to content

problem with wmi query


Recommended Posts

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!

Link to comment
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...