c# - C# 中的 PowerShell 管理单元问题

标签 c# powershell powershell-2.0 exchange-server powershell-cmdlet

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


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();

我收到以下错误: 尚未为 Windows PowerShell 版本 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();
        }

最佳答案

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

关于c# - C# 中的 PowerShell 管理单元问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7811754/

相关文章:

windows - 使用 powershell 设置 Windows 屏幕保护程序需要密码

powershell - 如何过滤powershell中从控制台程序获取的行?

powershell - 如何使用PowerShell使用Bitly V4 API缩短URL?

c# - ModalPopupExtender 奇怪行为中的 Gridview 分页

c# - 如何使用 Aspose 6.4 将 PowerPoint 文件转换为 PDF?

powershell - 组合链接的对象数组

PowerShell 选择怪异

powershell - 在 powershell 中比较两个哈希表

c# - Asp.Net 网络服务 : I would like to return error 403 forbidden

c# - 为什么将游戏对象的移动限制在相机框架内会阻止移动?