0

I am unable to pass an object of type [System.Messaging.Message] into a function within my script.

e.g. (outline of code)

function global:CopyQueue() {

    $vTotalCountInMSMQ = $global:qSource.GetAllMessages()
    foreach ($msg in $vTotalCountInMSMQ)
    {
        ReadAndCopyMessage $destinationQueue ([REF]$msg)  
    }
 }

Target Function:

function global:ReadAndCopyMessage($destinationQueueName, [REF]$message)
{ 
      $message = $message.BodyStream.Position

      .etc.....
}

Unable to access properties (Property 'Position' cannot be found on this object; make sure it exists and is settable.). However, if this code is run within the CopyQueue function, everything works as expected.

I am having trouble to outsource this and process the $msg object out of the loop.

Thanks for your help in advance

similiar questions didn't work:

Community
  • 1
  • 1
jens
  • 534
  • 1
  • 8
  • 17
  • Is there a reason that you're using [ref]? You may need to do $message.value.bodystream.position because using [ref] makes for some strange syntax. – TheMadTechnician Oct 23 '14 at 22:17
  • Tried it with and without [ref] also tried .value. with no success. I thought instead of a copy I try to pass a reference to this object. – jens Oct 24 '14 at 02:15
  • The parens around the [REF]$msg are definitely not what you want. Try removing them. – Mike Shepard Oct 24 '14 at 04:16
  • Tried both, no positive effect. I am getting something like this `code`(Cannot process argument transformation on parameter 'message'. Reference type is expected in argument.) Tried of cause the object type [System.Messaging.Message] in all combinations. Google'd the errors and all combinations of passing a complex object type to a function. Instead I get -1 on my question ;) – jens Oct 24 '14 at 14:26

1 Answers1

0

It appears that you shouldn't use [REF] anymore. Also, I must have made the common "," error between parameters before and thus it didn't work.

The code above works fine without [REF]

Call:

ReadAndCopyMessage $destinationQueue $msg

Function:

function global:ReadAndCopyMessage($destinationQueueName, $message)
jens
  • 534
  • 1
  • 8
  • 17