简体   繁体   English

C#Powershell-Exchange管理{“值不能为空。\\ r \\ n参数名称:serverSettings”}

[英]C# Powershell - Exchange management {“Value cannot be null.\r\nParameter name: serverSettings”}

I want to interact with Microsoft.Exchange.Management.PowerShell.E2010 that is installed on my machine through a C# project. 我想与通过C#项目安装在计算机上的Microsoft.Exchange.Management.PowerShell.E2010进行交互。

My local machine is a Windows Server 2012 R2 Standard and the Exchange Server 2010 SP3 with the Rollup Update 14 is installed. 我的本地计算机是Windows Server 2012 R2 Standard,并且安装了具有汇总更新14的Exchange Server 2010 SP3。

I'm using the 4.5 .NET Framework (downgrading to an older version isn't possible) 我正在使用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();
  • I open a remote shell session (targetted on my local machine). 我打开一个远程Shell会话(定位在我的本地计算机上)。
  • Add the Exchange management PsSnapIn, in order have access to the exchange commands. 添加Exchange管理PsSnapIn,以便可以访问交换命令。
  • Finally I execute my Exchange management command. 最后,我执行我的Exchange管理命令。

\\!/ Problem is at the last step, results = ps.Invoke(); \\!/问题在最后一步, results = ps.Invoke(); throws a System.Management.Automation.RemoteException with the message "Value cannot be null.\\r\\nParameter name: serverSettings" . 引发System.Management.Automation.RemoteException ,并显示消息"Value cannot be null.\\r\\nParameter name: serverSettings"

Do you guys have any idea? 你们有什么主意吗?

Thank you for your time. 感谢您的时间。

I've been fighting with this for the last couple days. 在过去的几天里,我一直在与这个斗争。 I know this question is a few months old, but I thought I'd share the solution I finally found. 我知道这个问题还有几个月的时间了,但是我想我会分享我终于找到的解决方案。 In your .config, you need the useLegacyV2RuntimeActivationPolicy attribute set to true on the startup tag. 在您的.config中,您需要在startup标记useLegacyV2RuntimeActivationPolicy属性设置为true Like so: 像这样:

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

Using this, I was successfully able to run a Get-Mailbox command while targeting .NET 4.6.2. 使用此工具,我可以在定位.NET 4.6.2时成功运行Get-Mailbox命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM