Share via


Error: Cannot find appropriate constructor - Works in ISE but not on command prompt

Question

Monday, January 2, 2017 8:54 AM

This problem is driving me nuts. I have a PS script thats uses EWS for sending emails. Everything works just fine and dandy in the ISE but when I tried to schedule the scripts the problems started.

It all boils down to the fact that initializing a EmailMessage object fails when running from the command prompt.

$message = New-Object -TypeName Microsoft.Exchange.WebServices.Data.EmailMessage($service)
New-Object : Constructor not found. 
Cannot find an appropriate constructor for type Microsoft.Exchange.WebServices.Data.EmailMessage.At line:1 char:12+ $message = New-Object -TypeName Microsoft.Exchange.WebServices.Data.EmailMessage            
+ CategoryInfo: ObjectNotFound: (:) [New-Object], PSArgumentException    
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

I have loaded the EWS dll file, I can create other objects like service and messgebody.

I have tried starting the script in STA mode.

I have upgraded PS from v3 to 4.

I noticed that there are alot more assemblies loaded in the ISE than on the command line, but have not found out that the EWS should have any relevant dependencies.

Still not working. Any ideas what I could try next?

All replies (4)

Monday, January 2, 2017 9:31 AM | 1 vote

You need to specify a service type

Import-Module 'C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll'
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2016_SP1)
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)

\(ツ)_/


Monday, January 2, 2017 9:38 AM

Sorry, the parameter got lost in the copy-paste. I'm providing the service. The dll I'm loading using Add-type


Monday, January 2, 2017 9:47 AM

You can also do this:

Add-Type -Path 'C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll'
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2016_SP1)
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)

This piece of code works in CLI and ISE in either STA or MTA,

Do not use EWS 1.x.  Upgrade to 2.0.

\(ツ)_/


Thursday, November 16, 2017 7:57 PM

You need to specify a service type

Import-Module 'C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll'
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2016_SP1)
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)

\(ツ)_/

Thanks for this!  My script was tripping up on a variable scope issue by re-instantiating the service object, my task scheduler problem calling this script, went away!