c# - Powershell AddPSSnapin导致错误

本文关键字:错误 AddPSSnapin Powershell | 更新日期: 2023-09-27 18:19:10

因此,我导入System.Management.Automation dll并尝试使用参数

运行New-Mailbox命令

所以我使用:

RunspaceConfiguration config = RunspaceConfiguration.Create();
PSSnapInException psEx = null;
config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out psEx);

一切都好极了……但是当我去运行应用程序时,我得到以下内容: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.

所以,我在网上做了一些研究,发现我需要从Any CPU改为x86作为平台目标。

然而,当我这样做时,我得到一个HTTP Error 503. The service is unavailable.错误

我几乎肯定我必须将其作为32位进程运行,以便它可以使用snapin(从其他读取似乎是snapin运行的内容)

我确实将应用程序池从Enable 32-bit Applications更改为True。也就是我得到错误的时候。

我看过其他的帖子…但是,我不确定如何通过这个Service unavailable的事情。

我尝试使用x64构建并获得Could not load file or assembly 'EmailAdminWeb2' or one of its dependencies. An attempt was made to load a program with an incorrect format.

c# - Powershell AddPSSnapin导致错误

你根本不用这个:

config.AddPSSnapIn("your snapin here", out psEx);

相反……只需按如下方式使用连接:

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://yourdomainhere/Powershell/Microsoft.Exchange"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", PsCreds);
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

现在运行你的命令,你就可以开始了。

快速笔记:

  • 确保你的应用程序是针对x64而不是Any CPUx86
  • 如果你使用。net 4.5(或4),请确保你的应用程序池设置正确(v4.0不是v2.0),你有Enable 32bit apps设置为false