EWS不在Powershell工作

时间:2014-10-23 23:38:19

标签: powershell exchange-server exchangewebservices

所以我已经设置了相同的Exchange Web服务示例:

作为控制台应用程序运行的C#版本:

class Program
{
    static void Main(string[] args)
    {
        var es = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
        {
            TraceEnabled = true,
            UseDefaultCredentials = true,
            Url = new Uri("https://mail.myServer.com/EWS/Exchange.asmx")
        };

        ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;

        var inboxId = new FolderId(WellKnownFolderName.Inbox);
        Folder inboxFolder = null;
        try
        {
            inboxFolder = Folder.Bind(es, inboxId);
        } catch(Exception e)
        {
            Console.Out.WriteLine(e.Message);
        }

        if (inboxFolder == null)
        {
            Console.Out.WriteLine("FAILED");
            return;
        }
        Console.Out.WriteLine("Total stuff: [{0}]", inboxFolder.TotalCount);
        Console.In.ReadLine();
    }
}

Powershell版本:

clear
# Load EWS Managed API
Import-Module "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll";

$EWSService = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
$EWSService.traceenabled = $true
$EWSService.UseDefaultCredentials = $true 
$EWSService.Url = New-Object Uri("https://mail.myServer.com/EWS/Exchange.asmx")

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true };

$InboxID = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
Try {$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($EWSservice,$InboxID)}
Catch [Exception] {
    Write-Host $_.Exception.Message
}    

两者都在同一台机器上运行,同一个用户。控制台应用程序连接并返回数据。 powershell版本获得了非常有用的信息:

The request failed. The underlying connection was closed: An unexpected error occurred on a send.

检查异常不会提供任何额外的信息。两个版本都引用了相同的EWS托管API。任何人都有任何想法为什么会这样?

2 个答案:

答案 0 :(得分:0)

我想我看到了问题。

# Load EWS Managed API
Import-Module "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll";

这不是Powershell模块。

我使用Add-Type在我的EWS脚本中加载.dll,如下所示:

   Add-Type -Path 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'

答案 1 :(得分:0)

根据JohhnyJob的消息使用

    ## Code From http://poshcode.org/624
## Create a compilation environment
$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Provider.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null

$TASource=@'
  namespace Local.ToolkitExtensions.Net.CertificatePolicy{
    public class TrustAll : System.Net.ICertificatePolicy {
      public TrustAll() { 
      }
      public bool CheckValidationResult(System.Net.ServicePoint sp,
        System.Security.Cryptography.X509Certificates.X509Certificate cert, 
        System.Net.WebRequest req, int problem) {
        return true;
      }
    }
  }
'@ 
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly

## We now create an instance of the TrustAll and attach it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll

## end code from http://poshcode.org/624

而不是

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true };