WMI Errors Using Count, ItemIndex, …

I was recently doing some WMI work and trying to implement some code simplification to an existing routine, but the code, which I knew was valid, kept throwing an

Error -2147217386 – Invalid operation

when trying to use the ItemIndex method.

I did some troubleshooting and was amazed when something as simple as object set Count property was also failing and returning

Error -2147467259 – Unspecified error

I won’t bore you with all the details, but the issue has to do with the WMI ExecQuery method flags I was using. The culprit was the fact that I was utilizing the wbemFlagForwardOnly flag.

So note to one’s self, many methods/properties are not available when using forward only.

So you have a choice, not use the forward only option, OR, not use the ItemIndex method and instead iterate over the object set objects individually until you find the one you wanted.

So now you know! 🙂

One response on “WMI Errors Using Count, ItemIndex, …

  1. Keith A. Swerling

    Those Long error codes. I had a lot of issues with those error codes. Maybe this would be interesting to you?

    https://medium.com/@NoLongerSet/breaking-down-windows-error-codes-4e42e4609579

    Function ErrToOct(ErrNum As Double)
        ErrToOct = Right(Oct(ErrNum), 3) 
    End Function
    
    Sub testErrToOct()
        Debug.Print Error(ErrToOct("-2147217386"))
        Debug.Print Error(ErrToOct("-2147467259"))
    End Sub