使用 powershell 2.0 中的 ews

using ews from powershell 2.0

尝试执行以下代码时出错:

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

import-module -Name $dll

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

错误:

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

我们 运行 在 windows 服务器 2008 r2 上安装了 .net framework 4.5.2(现已升级到 4.6.1)。使用的 powershell 版本是 v2.0

我们应该能够通过 powershell 2.0 使用托管 api 对吗?

好像需要.net 3.5。 汇编版本 "Version=3.5.0.0" 说了。

要加载程序集,您应该使用:

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

Import-module 期望加载一个 psm1 文件。我不认为那是加载程序集的方式。

点击链接:

Load Dot Net Assemblies

Using C sharp

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

希望对您有所帮助。

我认为问题在于您正在尝试加载需要 .net 运行time >=3.5 的程序集。如果你在 PowerShell 2.0 中 运行 $PSVersionTable,你会得到这样的东西:

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

导致 EWS 程序集加载失败的是 CLR 版本。

要解决此问题,您可以创建一个批处理文件,该文件将依次创建一个调用 PowerShell 2.0 并使用较新的 CLR 运行time 的配置文件。

这是批处理文件:

@echo off
:: 
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=

powershell.exe 更改为 powershell_ise.exe 以使用 .net 4 CLR 运行time 启动 ISE。

另请参阅我的 SO answer 关于在 PowerShell 中使用 EWS