Jump to content

Type Mismatch Error


Recommended Posts

I'm getting a type mismatch error on the code below but don't know why. Since no one will have the WMI classes I'm just going to simplify, let me know if someone see's something I'm missing.

Thanks,

$Server = "X"
$ID = "16"
$PC = "X"

$Locator = ObjCreate("WbemScripting.SWbemLocator")
$Service = $Locator.ConnectServer($server,"root\CIMV2)
$Security = $Service.Security_
$Security.ImpersonationLevel = 3

$colMembers = $Service.ExecQuery("SELECT * FROM FCM WHERE ID = '" & $ID & "'")
For $Member in $Members
    If $Member.Name = $PC Then
        $PcID = $Member.ResourceID
    EndIf
Next

$oClass = $Service.Get("Coll")

$objInParams = $oClass.Methods_("Approved").inParameters.SpawnInstance_()

$objInParams.Properties_.Item("IDs") = $PcID
$objInParams.Properties_.Item("Appr") = True

$objOutParams = $Service.ExecMethod("Coll","Approved", $objInParams)

Everything works fine up to the line, a $PcID is populated = $Member.ResourceID.

But then at this line I get Type Mismatch.

$objInParams = $oClass.Methods_("Approved").inParameters.SpawnInstance_()

Err.number is: 80020005

I've tried rewriting this a few times with no luck.

Thanks,

Terry

Link to comment
Share on other sites

Well, since "no one will have the WMI classes" all we can do is hope you learn something helpful from whatever documentation you have for that class.

Are there working examples anywhere, perhaps in VBScript?

:blink:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Well, since "no one will have the WMI classes" all we can do is hope you learn something helpful from whatever documentation you have for that class.

Are there working examples anywhere, perhaps in VBScript?

What I'm looking for is someone who knows more than me to look over the structure of the code I have to see if that fits with what is going on.

For example:

$Service = $Locator.ConnectServer($server,"root\CIMV2)

Should this be a GetObj instead of ConnectServer? I'm really not sure how to tell.

or since the Type mismatch Occurs right after this line:

$objInParams = $oClass.Methods_("Approved").inParameters.SpawnInstance_()

What would cause that at that location? Is there something I missed? Is oClass right? before that?

The only real info I have on this class is this:

SInt32 Approved(

UInt32 IDs[],

Boolean Appr

);

Parameters:

IDs

Data type: UInt32 Array

Qualifiers: [in]

IDs of member resource(s).

Appr

Data type: Boolean

Qualifiers: [in, optional]

true if resources are approved. The default value is true.

Return Values

An SInt32 data type that is 0 to indicate success or non-zero to indicate failure.

Thanks,

Terry

Link to comment
Share on other sites

I don't get it. Where did you get the syntax you used? Looks to me like it would be:

; Array of IDs
Global $aID[3] = [14, 39, 6]
$iRET = $oClass.Approved($aID, True)
If $iRET = 0 Then
     MsgBox(64, "Success", "Success")
Else
     MsgBox(16, "Fail", "Fail")
EndIf

:blink:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I don't get it. Where did you get the syntax you used? Looks to me like it would be:

; Array of IDs
Global $aID[3] = [14, 39, 6]
$iRET = $oClass.Approved($aID, True)
If $iRET = 0 Then
     MsgBox(64, "Success", "Success")
Else
     MsgBox(16, "Fail", "Fail")
EndIf

I got it from... I thought you have to get the Class, spawn an instance of the method, add your properties and then execute the method? At least I've seen a million examples of that in the forums when people are doing WMI stuff. Plus I've done it that way too executing certain WMI methods before with success.

I did try your suggestion and changed code to (oh and while this method can handle multiple IDs there will only be one returned from the execquery every time because I have: If $Member.Name = $PC Then..., so I left the array out):

$Server = "X"
$ID = "16"
$PC = "X"

$Locator = ObjCreate("WbemScripting.SWbemLocator")
$Service = $Locator.ConnectServer($server,"root\CIMV2)
$Security = $Service.Security_
$Security.ImpersonationLevel = 3

$colMembers = $Service.ExecQuery("SELECT * FROM FCM WHERE ID = '" & $ID & "'")
For $Member in $Members
    If $Member.Name = $PC Then
        $PcID = $Member.ResourceID
    EndIf
Next

$oClass = $Service.Get("Coll")

$iRET = $oClass.Approved($PcID, True)
If $iRET = 0 Then
     MsgBox(64, "Success", "Success")
Else
     MsgBox(16, "Fail", "Fail")
EndIf

I am still getting a type mismatch error right after:

$oClass = $Service.Get("Coll")

Details are:

err.description is: Type mismatch

err.windescription:

err.number is: 80020009

err.lastdllerror is: 0

err.scriptline is: -1

err.source is: SWbemObjectEx

err.helpfile is:

err.helpcontext is: 0

This is a different err.number than before.

But funny thing is then it does a message box saying "Success"?

Do you think I need to change the:

$Service = $Locator.ConnectServer($server,"root\CIMV2)

I'm not sure I understand connecting to the server if I'm on the local system? Can't I just get the object? Could that be it or is this ok for what I'm trying to do?

Thanks,

Terry

Link to comment
Share on other sites

Seems overly complicated for local WMI. Typical AutoIt usage is more like:

$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$oWMI = ObjGet("WinMgmts:\\.\Root\CIMv2")
; -- or  --
; $WMIObj = ObjGet("WinMgmts:\\" & @ComputerName & "\Root\CIMv2")

$colServices = $oWMI.ExecQuery("SELECT * FROM WIN32_Service WHERE name LIKE 'Win%'")
If IsObj($colServices) Then
    For $oService In $colServices
        MsgBox(64, "Service", "Service = " & $oService.name)
    Next
Else
    MsgBox(16, "Error", "Failed to get collection.")
EndIf

Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    MsgBox(16, "COM Error", "We intercepted a COM Error !" & @CRLF & _
            "Number is: " & $HexNumber & @CRLF & _
            "Windescription is: " & $oMyError.windescription)
EndFunc   ;==>MyErrFunc

You can search the forum for MANY examples of working WMI scripts.

:blink:

Edit: Added fully functional demo.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Seems overly complicated for local WMI. Typical AutoIt usage is more like:

$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$oWMI = ObjGet("WinMgmts:\\.\Root\CIMv2")
; -- or  --
; $WMIObj = ObjGet("WinMgmts:\\" & @ComputerName & "\Root\CIMv2")

$colServices = $oWMI.ExecQuery("SELECT * FROM WIN32_Service WHERE name LIKE 'Win%'")
If IsObj($colServices) Then
    For $oService In $colServices
        MsgBox(64, "Service", "Service = " & $oService.name)
    Next
Else
    MsgBox(16, "Error", "Failed to get collection.")
EndIf

Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    MsgBox(16, "COM Error", "We intercepted a COM Error !" & @CRLF & _
            "Number is: " & $HexNumber & @CRLF & _
            "Windescription is: " & $oMyError.windescription)
EndFunc   ;==>MyErrFunc

You can search the forum for MANY examples of working WMI scripts.

:blink:

Edit: Added fully functional demo.

I always try to search the forum before posting questions... and have in this case too. I've reviewed many many examples. Which is how I came up with what I had.

I feel like we took a step back though since my last post?

Is there some thing else I can try? Seems like you are saying what I have is overly complex, ok I can try to change the first part. I still don't see in your most recent example how to simplify for Methods and method executes? Also I had a couple questions in that last post about the new error code being shown and if:

$Service = $Locator.ConnectServer($server,"root\CIMV2) should be changed, maybe to something similar to your most recent post?

Thanks,

Terry

Link to comment
Share on other sites

Well, to start with, you keep repeating what is hopefully only a typo in this post: missing closing quotes

$Service = $Locator.ConnectServer($server,"root\CIMV2)

Hopefully that's not really there.

Your $oClass = $Service.Get("Coll") is referring straight back to the CIMv2 root object. What is that? It doesn't have anything to do with the mystery FCM class and this simplification fails:

$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$Service = ObjGet("WinMgmts:\\.\Root\CIMv2")
; -- or  --
; $WMIObj = ObjGet("WinMgmts:\\" & @ComputerName & "\Root\CIMv2")

$oClass = $Service.Get("Coll")
If IsObj($oClass) Then
    MsgBox(64, "Object", "$oClass = " & ObjName($oClass))
Else
    MsgBox(16, "Not an object", "$oClass = " & $oClass & "; Type = " & VarGetType($oClass))
EndIf

Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    MsgBox(16, "COM Error", "We intercepted a COM Error !" & @CRLF & _
            "Number is: " & $HexNumber & @CRLF & _
            "Windescription is: " & $oMyError.windescription)
EndFunc   ;==>MyErrFunc

What did you think Get("col") was going to get you from the root of CIMv2?

:blink:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Well, to start with, you keep repeating what is hopefully only a typo in this post: missing closing quotes Hopefully that's not really there.

Your $oClass = $Service.Get("Coll") is referring straight back to the CIMv2 root object. What is that? It doesn't have anything to do with the mystery FCM class and this simplification fails:

$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$Service = ObjGet("WinMgmts:\\.\Root\CIMv2")
; -- or  --
; $WMIObj = ObjGet("WinMgmts:\\" & @ComputerName & "\Root\CIMv2")

$oClass = $Service.Get("Coll")
If IsObj($oClass) Then
    MsgBox(64, "Object", "$oClass = " & ObjName($oClass))
Else
    MsgBox(16, "Not an object", "$oClass = " & $oClass & "; Type = " & VarGetType($oClass))
EndIf

Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    MsgBox(16, "COM Error", "We intercepted a COM Error !" & @CRLF & _
            "Number is: " & $HexNumber & @CRLF & _
            "Windescription is: " & $oMyError.windescription)
EndFunc   ;==>MyErrFunc

What did you think Get("col") was going to get you from the root of CIMv2?

1.) Typo = Yes. Not like this in my script. Must've got deleted when I was first posting...

2.) under root\CIMv2 is two classes "FCM" and "COLL". First I need to get the resource ID from the FCM Class membership for a specific PC so that I can plug it into the method which applies to Coll.

3.) I ran your code above on my test server and got:

$oClass = ISWbemObjectEx

No errors.

Thanks,

Terry

Link to comment
Share on other sites

Well, we've come full circle back to documentation of whatever that $oClass object is. The mystery context is not helping to figure this out.

:blink:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Well, we've come full circle back to documentation of whatever that $oClass object is. The mystery context is not helping to figure this out.

:blink:

It isn't a mystery, it's just something in use where I'm at from some past developer... all I'm looking for is help on the basics of something that should be simple.

How does one execute a method in WMI using AutoIt script?

From the researching on the forums I came up with what I posted and I'm open to suggestions of ways to change it.

For example found this on the forum:

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$objShare = $objWMIService.Get("Win32_ScheduledJob")
$objInParam = $objShare.Methods_("Create").inParameters.SpawnInstance_()
$objInParam.Properties_.Item("Command") = $scheduledprog
$objInParam.Properties_.Item("DaysOfMonth") =  "" ; only for scheduling repeat events
$objInParam.Properties_.Item("DaysOfWeek") = ""   ; only for scheduling repeat events
$objInParam.Properties_.Item("InteractWithDesktop") = True ; False = Run process as System
$objInParam.Properties_.Item("RunRepeatedly") =  False     ; False = Run one time only
$objInParam.Properties_.Item("StartTime") =  "********" & $time &"00.000000-" & $tzo
$objOutParams = $objWMIService.ExecMethod("Win32_ScheduledJob", "Create", $objInParam)

I've compared this with what I have and it looks the same to me pretty much. I even changed my code to have this:

ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

To try to match more closely and got the type mismatch still?

If you have a better example on executing methods I could try to use that?

Thanks,

Terry

Edited by mattw112
Link to comment
Share on other sites

I figured it out. Remember a million posts ago when I sent you the class info... I was looking at that again and noticed that it says:

IDs

Data type: UInt32 Array

Qualifiers: [in]

IDs of member resource(s).

Then you sent me some code to try with an array in it and I dismissed it because I only had a single resource that was begin returned...

It hit me, I HAVE to use an array, even if just 1 PC... that's why I was getting the type mismatch.

I just changed my Variable to an array and it works now!

Thanks for all your help as usual. Sorry I dismissed the array part so soon without thinking.

Terry

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...