1

我正在尝试执行以下操作:


StringBuilder errorList = new StringBuilder();
            RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create();
            PSSnapInException snapEx = null;
            PSSnapInInfo psinfo = runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapEx);
            Runspace runSpace = RunspaceFactory.CreateRunspace(runspaceConfig);
            runSpace.Open();
            Pipeline pipeLine = runSpace.CreatePipeline();

我收到以下错误: No snap-ins have been registered for Windows PowerShell version 2.

我是 PoweShell 的新手,不确定该错误到底意味着什么。这是我需要安装的东西吗?

编辑:完整代码


        /// 
        /// Creates mailbox for the given user.
        /// 
        /// Email address of user.
        public void EnableMailbox(string userEmail)
        {
            StringBuilder errorList = new StringBuilder();
            RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create();
            PSSnapInException snapEx = null;
            PSSnapInInfo psinfo = runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapEx);
            Runspace runSpace = RunspaceFactory.CreateRunspace(runspaceConfig);
            runSpace.Open();
            Pipeline pipeLine = runSpace.CreatePipeline();
            if (!MailBoxAlreadyExist(userEmail, runSpace))
            {
                Command createMailbox = new Command("Enable-Mailbox");
                createMailbox.Parameters.Add("identity", userEmail);
                createMailbox.Parameters.Add("database", "Mailbox Database Name");
                pipeLine.Commands.Add(createMailbox);
                pipeLine.Invoke();
                if (pipeLine.Error != null && pipeLine.Error.Count > 0)
                {
                    foreach (object item in pipeLine.Error.ReadToEnd())
                    {
                        errorList.Append(item.ToString());
                        errorList.Append(System.Environment.NewLine);
                    }
                    Console.WriteLine(errorList.ToString());
                }
            }
            else
            {
                Console.WriteLine("Mailbox of user " + userEmail + " already exists on exchange server.");
            }
            pipeLine.Dispose();
            runSpace.Close();
            runSpace.Dispose();
        }

4

3 回答 3

4

32 位和 64 位管理单元之间有区别。可能 Echange 仅是 32 位的,在这种情况下,请将您的 C# 项目设置为目标平台 x86。如果 Exchange 是 64 位,则仅将 C# 项目设置为目标平台 x64。

于 2011-10-19T00:29:19.387 回答
3

正如基思指出的那样,您需要正确的目标平台。Exchange 仅为 64 位。您需要将 C# 项目设置为目标平台 x64。在新项目中,默认设置为 x86(至少在 Visual Studio 2010 中)。

于 2011-10-19T08:34:05.277 回答
0

您应该打开远程 powershell 会话以从 c# 运行 PS 命令。不再支持从 c# 代码本地执行 Exchange 管理单元。这是枚举现有邮箱的示例:

var ExchangeCredential = new PSCredential(user, password.ToSecureString());
string serverName = string.Format("{0}.{1}", GetMachinename(), GetDomainName());
var serverUri = new Uri(String.Format("http://{0}/powershell?serializationLevel=Full", serverName));

var connectionInfo = new WSManConnectionInfo(serverUri,"http://schemas.microsoft.com/powershell/Microsoft.Exchange", ExchangeCredential);

runspace = RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell psh = PowerShell.Create();
psh.Runspace = ru

Pipeline pipeline = runspace.CreatePipeline();
var command = new Command("Get-MailboxDatabase");
command.Parameters.Add(new CommandParameter("Status", true));

pipeline.Commands.Add(command);
commandResults = pipeline.Invoke();
于 2013-11-05T08:14:47.873 回答