.net - 无法加载 Exchange powershell 管理单元 : The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception

标签 .net powershell exchange-server nlog exchange-server-2010

我有以下代码创建一个 PowerShell 运行空间,并加载了 Exchange 2010 管理单元。

Dim runspaceConfig = RunspaceConfiguration.Create()

Dim snapInException As PSSnapInException = Nothing
runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", snapInException)

Dim runspace = RunspaceFactory.CreateRunspace(runspaceConfig)
runspace.Open()

自从安装 Visual Studio 2012 后,在执行将管理单元添加到运行空间配置的行时,我开始收到以下错误。
System.Management.Automation.Runspaces.PSSnapInException occurred
  HResult=-2146233087
  Message=Cannot load Windows PowerShell snap-in Microsoft.Exchange.Management.PowerShell.E2010 because of the following error: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception.
  Source=System.Management.Automation
  WasThrownFromThrowStatement=False
  StackTrace:
       at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadCustomPSSnapIn(PSSnapInInfo mshsnapinInfo)
       at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo)
       at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo, PSSnapInException& warning)
       at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.DoAddPSSnapIn(String name, PSSnapInException& warning)
       at System.Management.Automation.Runspaces.RunspaceConfiguration.AddPSSnapIn(String name, PSSnapInException& warning)

我已经能够确认 nlog 以某种方式导致了这个问题。在创建 powershell 运行空间之前创建 nlog 记录器的组合导致错误。

如果我从我的应用程序配置中删除 nlog 配置部分,并只创建一个空的 nlog 记录器,那么该管理单元将正确加载。此外,如果我将 nlog 配置保留在我的应用程序配置中但不创建 nlog 记录器,则管理单元也会成功加载。
  • 我尝试在 x64 和 x86 中构建项目。
  • 我已经重新安装了交换管理工具。
  • 我已经尝试在交换环境中的另一台机器上进行测试。

  • 如果有人可以提供任何可以帮助我解决这个问题的建议,我将不胜感激。

    谢谢

    最佳答案

    经过进一步调查,我发现 .NET 4.5 是一个就地更新,这意味着 .NET 4.0 被覆盖并在安装时替换为 .NET 4.5。我不知道 .NET 4.5 中发生了什么变化导致了这个问题,但问题是通过卸载 .NET 4.5 并切换回 Visual Studio 2010 来解决的。希望微软在不久的将来会有一些更新来解决这个问题并允许我再次使用 Visual Studio 2012。

    有关就地更新的更多信息,请参阅以下文章。
    http://www.devproconnections.com/article/net-framework/net-framework-45-versioning-faces-problems-141160

    关于.net - 无法加载 Exchange powershell 管理单元 : The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12480447/

    相关文章:

    windows - 为什么在删除 WinRM 日志时 Windows guest 配置失败?

    powershell - 使用 powershell 对 Microsoft Graph api 进行身份验证

    c# - 自动化 EF 迁移 "Update-Database -Script"

    exchange-server - 使用保留 header 的 EWS 托管 API 转发电子邮件

    jakarta-mail - javax.mail.MessagingException : A3 BAD User is authenticated but not connected

    c# - .NET 定时器控件

    .net - Webpack 4 中与本地 js 文件(第三方库)捆绑的问题

    .net - 更改 .NET 应用程序配置文件名

    java - EWS : Category management

    c# - 从 UWP 中的 BitmapImage 或流获取 IntPtr 值