Exception calling “BeginSendMessage” with “3” argu

2019-08-20 04:52发布

问题:

I am attempting to execute a script for single contact IM (via Microsoft Lync), and have successfully ran the script all the way down to the last line:

$null = $m.BeginSendMessage($d, $null, $d)

Note:

$d = New-Object "System.Collections.Generic.Dictionary [Microsoft.Lync.Model.Conversation.InstantMessageContentType, String]"

$d.Add($PlainText, "This is a test.")

Below is the exception Psh is throwing upon execution of this syntax. It appears to fail at initiation of the $null variable.

Exception calling "BeginSendMessage" with "3" argument(s): "Value does not fall within the expected range."
At C:\ScriptsPS\IM_SingleContact.ps1:75 char:1
+ $null = $m.BeginSendMessage($d, $null, $d)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

回答1:

The second argument has to be a callback method. Using AsyncCallback in Powershell has a simple example:

$myCallback = [AsyncCallback]{
  param( $asyncResult)
  # callback code
  if ($asyncResult.isCompleted) {
    Write-Host "Message Sent"
  }
}
[void]$m.BeginSendMessage($d, $myCallback, $d)