0

Got an error while trying to execute the following code:

$dll = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"

import-module -Name $dll

$exc = new-object Microsoft.Exchange.WebServices.Data.ExchangeService

Error:

New-Object : Could not load file or assembly 'System.Core, Version=3.5.0.0, Cul
ture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The
system cannot find the file specified.
At D:\scripts\get_mails.ps1:5 char:18
+ $exc = new-object <<<<  Microsoft.Exchange.WebServices.Data.ExchangeService
    + CategoryInfo          : NotSpecified: (:) [New-Object], FileNotFoundExce
   ption
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerS
   hell.Commands.NewObjectCommand

We ran this on a windows server 2008 r2 which has .net framework 4.5.2 (now upgraded to 4.6.1) installed. The version of powershell being used is v2.0

We should be able to use the managed api via powershell 2.0 right?

deostroll
  • 11,661
  • 21
  • 90
  • 161

3 Answers3

0

It seems like it needs .net 3.5. The assembly version "Version=3.5.0.0" says it.

0

For loading assemblies , you should use :

[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll")

Import-module expects a psm1 file to be loaded. I don't think thats the way to load assemblies.

Follow the links:

Load Dot Net Assemblies

Using C sharp

learn-to-use-the-exchange-web-services-with-powershell/

Hope it helps.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
0

I think the problem is that you're trying to load an assembly that needs .net runtime of >=3.5. If you run $PSVersionTable in PowerShell 2.0, you'll get something like this:

Name                           Value
----                           -----
CLRVersion                     2.0.50727.5485
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

It's the CLR version that's causing the EWS assembly to fail loading.

To fix this, you can create a batch file, which will in turn, create a config file that calls PowerShell 2.0 and uses a newer CLR runtime.

Here's the batch file:

@echo off
:: https://stackoverflow.com/questions/7308586/using-batch-echo-with-special-characters
if exist %~dp0powershell.exe.activation_config goto :run
echo.^<?xml version="1.0" encoding="utf-8" ?^>                 > %~dp0powershell.exe.activation_config
echo.^<configuration^>                                        >> %~dp0powershell.exe.activation_config
echo.  ^<startup useLegacyV2RuntimeActivationPolicy="true"^>  >> %~dp0powershell.exe.activation_config
echo.    ^<supportedRuntime version="v4.0"/^>                 >> %~dp0powershell.exe.activation_config
echo.  ^</startup^>                                           >> %~dp0powershell.exe.activation_config
echo.^</configuration^>                                       >> %~dp0powershell.exe.activation_config
:run
:: point COMPLUS_ApplicationMigrationRuntimeActivationConfigPath to the directory that this cmd file lives in
:: and the directory contains a powershell.exe.activation_config file which matches the executable name powershell.exe
set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=%~dp0
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe %*
set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=

Change powershell.exe to powershell_ise.exe to launch ISE with .net 4 CLR runtime.

Also see my SO answer on using EWS in PowerShell

Community
  • 1
  • 1
TechSpud
  • 3,418
  • 1
  • 27
  • 35
  • You are close...but why `powershell.exe.activation_config`? – deostroll Feb 10 '17 at 11:03
  • Close, how? What's missing? Re: the config file, see this SO answer: http://stackoverflow.com/a/31279372/1368849 – TechSpud Feb 10 '17 at 11:04
  • I actually went with the highest upvoted answer there...however, please do tell me why you recommend this way? – deostroll Feb 10 '17 at 11:06
  • It allosw you to use .net > 2.0 assemblies in PowerShell 2.0 (i.e. not upgrade), which can only normally use .net <=2.0 assemblies. – TechSpud Feb 10 '17 at 11:16
  • Okay, sorry. I didn't realize this is a way to launch a single instance of powershell using a higher version of.net framework..but it looks complicated... – deostroll Feb 10 '17 at 11:16
  • Yeh, and you're not hitting the registry, so it's low impact to the server - i.e. the only launched instance of PowerShell is affected. – TechSpud Feb 10 '17 at 11:17