【问题标题】:C# Powershell - Exchange management {"Value cannot be null.\r\nParameter name: serverSettings"}C# Powershell - Exchange 管理 {"Value cannot be null.\r\nParameter name: serverSettings"}
【发布时间】:2016-07-26 20:52:24
【问题描述】:

我想与通过 C# 项目安装在我的机器上的Microsoft.Exchange.Management.PowerShell.E2010 进行交互。

我的本​​地计算机是 Windows Server 2012 R2 Standard,并且安装了带有汇总更新 14 的 Exchange Server 2010 SP3。

我正在使用 4.5 .NET Framework(无法降级到旧版本)

        WSManConnectionInfo connectionInfo = new WSManConnectionInfo();

        connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.
        connectionInfo.OpenTimeout = 1 * 60 * 1000; // 1 minute.

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
        runspace.Open();
        using (PowerShell ps = PowerShell.Create())
        {
            ps.Runspace = runspace;

            ps.AddCommand("Add-PsSnapIn");
            ps.AddArgument("Microsoft.Exchange.Management.PowerShell.E2010");

            var results = ps.Invoke();

            try
            {
                ps.AddCommand("Get-MailBox");

                results = ps.Invoke();
            }
            catch (Exception e)
            {

            }
        }
        runspace.Close();
  • 我打开了一个远程 shell 会话(针对我的本地计算机)。
  • 添加 Exchange 管理 PsSnapIn,以便访问交换命令。
  • 最后我执行了我的 Exchange 管理命令。

\!/ 问题出现在最后一步,results = ps.Invoke(); 抛出 System.Management.Automation.RemoteException 和消息 "Value cannot be null.\r\nParameter name: serverSettings"

你们有什么想法吗?

感谢您的宝贵时间。

【问题讨论】:

    标签: c# powershell exchange-server exchange-server-2010


    【解决方案1】:

    过去几天我一直在与此作斗争。我知道这个问题已经存在几个月了,但我想我会分享我最终找到的解决方案。在您的 .config 中,您需要将 startup 标签上的 useLegacyV2RuntimeActivationPolicy 属性设置为 true。像这样:

    <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
    </startup>
    

    使用它,我成功地能够在面向 .NET 4.6.2 时运行 Get-Mailbox 命令。

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 2022-10-18
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2021-12-23
      • 2022-11-04
      相关资源
      最近更新 更多