error running the $api.Return($Bag) command

Hi all - I am running a simple powershell command in a ps script to return a value, to be used later for reporting in scom. However I am getting an error when running it - script is below:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
$Mailflow = Test-Mailflow | select @{n='MailFlowLatency-(Seconds)';e={$_.messagelatencytime.totalseconds}}
$Mailflow.'MailFlowLatency-(Seconds)'.ToString()

$api = new-object -comObject 'MOM.ScriptAPI'
$bag = $api.CreatePropertyBag()

$bag.AddValue('Mailflow', $Mailflow)
$api.Return($Bag)
$bag

Every time i run this - or even other scripts returning values, i get the same error:

Exception calling "Return" with "1" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
At C:\temp\test.ps1:11 char:1
+ $api.Return($Bag)
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

I've noticed that this error appears with other users too when doing a google search, but nothing concrete as to what the problem may be. The command itself works fine, and removing the return line removes the error, so i can only assume it is that line that is causing the problem, as i cant see an issue with the powershell itself given that it works.

Any help would be much appreciated!! Thx in advance..

March 27th, 2015 5:52am

Hi,

Add $Mailflow.GetType() before the $bag.AddValue('Mailflow', $Mailflow) line and check if $Mailflow actually returns a string, not an o

Free Windows Admin Tool Kit Click here and download it now
March 27th, 2015 6:08am

Thanks for the reply. Running that, i get:

 

8.1864894

IsPublic IsSerial Name                                     BaseType                                                                                
-------- -------- ----                                     --------                                                                                
True     False    PSCustomObject                           System.Object                                                                           
Exception calling "Return" with "1" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
At C:\temp\test.ps1:12 char:1
+ $api.Return($Bag)
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

System.__ComObject

I have no idea what i am supposed to do with this info - any help?


March 28th, 2015 3:09am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics