当前位置: 移动技术网 > IT编程>开发语言>.net > .NET通过PowerShell操作ExChange为用户开通邮箱账号

.NET通过PowerShell操作ExChange为用户开通邮箱账号

2018年01月28日  | 移动技术网IT编程  | 我要评论

天中悬剑,谭歆柔,汕头苏宁电器招聘

就自己碰到的问题记录下来共勉。

 

直接用C#代码访问ExChange不会报错,但不会成功创建邮箱用户,主要是因为权限不足导致。

 

微软出了一个PowerShell的命令行工具 能够用命令行来操作ExChange,创建邮箱账号的命令是 new-mailbox

 

可以通过把.Net类注册成COM+组件的方式来操作PowerShell。

 

这里需要注意的是, 当你的调试环境和exchange服务器环境不一样时(比如调试环境是win7 64,exchange服务器是winserver2008 64),将编译的64位dll放在web项目里会报错,此时你可以忽略这个错误,只有放到exchange服务器后才会成功,所以最好调试环境和exchange服务器一致。

 

 

我的流程就是

 

WebService->.NET写的PowerShell操作类注册成的COM+组件->ExChange

 

环境是:

 

VS2010 + ExChange2010 + Windows Server 2008 64位版 + IIS7.0 

 

 

 

ps:这个COM+组件只能运行在安装ExChange的服务器上

 

公司的环境用的是ExChange2010, ExChange2010好像只有64位版的 只能安装在64位的系统

 

所以下面会说到把COM+组件编译成64位的问题

 

==============================================================================

 

 

 

1 首先先创建com组件并注册

 

1)启动Visual Studio 2010

 

2)选择File ->“新建->”项目...

 

3)选择Windows

 

4)选择“类库”

 

5)在名称框中键入“PowerShellComponent “

 

6)点击确定。

 

7)添加下列引用

 

System.EnterpriseServices

 

System.DirectoryServices

 

System.Management.Automation 路径:

 

32位系统:

 

C:\ProgramFiles\ReferenceAssemblies\Microsoft\WindowsPowerShell\v1.\System.Management.Automation.dll

 

64位系统

 

C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll

 

 

 

接下来有关程序集的操作

 

1)在解决方案资源管理器,右键单击PowerShellComponent项目,选择属性,点击签名选项,选中"为程序集签名",并创建一个新的强名称密钥称为“PowerShellComponent.snk” , 不用设置密码。如下图

 

 

 

 

 

 

 

 

 

2)还是在项目属性窗口中,选择"应用程序"选项卡,然后点击“程序集信息...”,检查框,选中"使程序集COM可见"。如图

 

 

 

 

 

 

 

PS:如果运行这个com组件的机器是64位系统(32位的没试过),这里需要再加一步:

 

把项目的运行平台设置成64位的

 

还是在项目属性窗口中:

 

"生成"选项卡->目标平台->64位

 

 

 

    ->   

 

 

 

 

 

3)打开AssemblyInfo.cs中,并添加“using System.EnterpriseServices;”,并添加

 

 

 

[assembly: ApplicationActivation(ActivationOption.Server)] 

[assembly: ApplicationName("PowerShellComponent")] 

[assembly: Description("Simple PowerShell Component Sample")] 

[assembly: ApplicationAccessControl( 

           false, 

           AccessChecksLevel = AccessChecksLevelOption.Application, 

           Authentication = AuthenticationOption.None, 

           ImpersonationLevel = ImpersonationLevelOption.Identify)]

 

然后添加ManagementCommands类...

 

1)选择“解决方案资源管理器”查看选项卡。将Class1.cs文件重命名为“ManagementCommands.cs”。

 

类需要继承System.EnterpriseServices.ServicedComponent,否则不能被编译成COM+组件

 

2)添加引用如图并using

 

 

 

using System.EnterpriseServices;

using System.Security;

using System.Security.Principal;

using System.Runtime.InteropServices;

using System.Collections.ObjectModel;

using System.Management.Automation;

using System.Management.Automation.Host;

using System.Management.Automation.Runspaces;

using System.DirectoryServices;

using Microsoft.PowerShell.Commands;

using System.Collections;

 

3)拷贝下面的方法到类中

 

#region 根据登录名判断是否存在邮箱

 

        public bool IsExistMailBox(string identity)

 

        {

 

            try

 

            {

 

                PSSnapInException PSException = null;

 

                RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

 

                runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

 

                Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

 

                runspace.Open();

 

 

 

                Pipeline pipeline = runspace.CreatePipeline();

 

                Command command = new Command("Get-Mailbox");

 

                command.Parameters.Add("identity", identity);

 

                pipeline.Commands.Add(command);

 

                Collection<PSObject> result = pipeline.Invoke();

 

 

 

                runspace.Close();

 

 

 

                return (result != null && result.Count > 0);

 

            }

 

            catch (System.Exception ex)

 

            {

 

                throw ex;

 

            }

 

        }

 

        #endregion

 

 

 

        #region 创建邮箱账号

 

        public bool NewMailbox(string name, string accountName, string pwd, string emailDomain, string organizationalUnit, string database)

 

        {

 

            string emailAdd = accountName + emailDomain;

 

 

 

            if (this.IsExistMailBox(emailAdd))

 

            {

 

                throw new Exception("已经存在同名的邮箱"); 

 

            }

 

            try

 

            {

 

                PSSnapInException PSException = null;

 

                RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

 

                runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

 

                Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

 

                runspace.Open();

 

                Pipeline pipeline = runspace.CreatePipeline();

 

 

 

                Command command = new Command("New-Mailbox");

 

                char[] passwordChars = pwd.ToCharArray();

 

                SecureString password = new SecureString();

 

                foreach (char c in passwordChars)

 

                {

 

                    password.AppendChar(c);

 

                }

 

 

 

                command.Parameters.Add("Name", name);//姓名 

 

 

 

                command.Parameters.Add("UserPrincipalName", emailAdd);//邮箱地址

 

                command.Parameters.Add("SamAccountName", accountName);//登录名

 

 

 

                command.Parameters.Add("Password", password);//密码

 

 

 

                command.Parameters.Add("OrganizationalUnit", organizationalUnit);//组织单元

 

                command.Parameters.Add("Database", database);//数据库 

 

 

 

                pipeline.Commands.Add(command);

 

                Collection<PSObject> result = pipeline.Invoke();

 

                runspace.Close();

 

 

 

                return this.IsExistMailBox(emailAdd);

 

            }

 

            catch (Exception ex)

 

            {

 

                throw ex;

 

            }

 

        }

 

        #endregion

 

 

 

        #region 删除邮箱账号(控制台和域都删除)

 

 

 

        public bool RemoveMailbox(string identity)

 

        {

 

 

 

            try

 

            {

 

                PSSnapInException PSException = null;

 

                RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

 

                runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

 

                Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

 

                runspace.Open();

 

                Pipeline pipeline = runspace.CreatePipeline();

 

 

 

                Command command = new Command("Remove-Mailbox");

 

                command.Parameters.Add("Identity", identity);

 

                command.Parameters.Add("Confirm", false);

 

                pipeline.Commands.Add(command);

 

                Collection<PSObject> result = pipeline.Invoke();

 

                runspace.Close();

 

 

 

                return !this.IsExistMailBox(identity);

 

            }

 

            catch (System.Exception ex)

 

            {

 

                throw ex;

 

            }

 

        }

 

        #endregion

 

 

 

        #region 启用邮箱账号

 

        public bool EnableMailbox(string identity)

 

        {

 

            try

 

            {

 

                PSSnapInException PSException = null;

 

                RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

 

                runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

 

                Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

 

                runspace.Open();

 

                Pipeline pipeline = runspace.CreatePipeline();

 

 

 

                Command command = new Command("Enable-Mailbox");

 

                command.Parameters.Add("Identity", identity);

 

                command.Parameters.Add("Confirm", false);

 

                pipeline.Commands.Add(command);

 

                Collection<PSObject> result = pipeline.Invoke();

 

                runspace.Close();

 

                return this.IsExistMailBox(identity);

 

            }

 

            catch (Exception ex)

 

            {

 

                throw ex;

 

            }

 

        }

 

        #endregion

 

 

 

        #region 禁用邮箱账号

 

        public bool DisableMailbox(string identity)

 

        {

 

            try

 

            {

 

                PSSnapInException PSException = null;

 

                RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

 

                runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

 

                Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

 

                runspace.Open();

 

 

 

                Pipeline pipeline = runspace.CreatePipeline();

 

                Command command = new Command("Disable-Mailbox");

 

                command.Parameters.Add("Identity", identity);

 

                command.Parameters.Add("Confirm", false);

 

                pipeline.Commands.Add(command);

 

                Collection<PSObject> result = pipeline.Invoke();

 

                runspace.Close();

 

                return !this.IsExistMailBox(identity);

 

            }

 

            catch (Exception ex)

 

            {

 

                throw ex;

 

            }

 

        }

 

        #endregion

 

 

 

        #region 判断是否存在通讯组

 

        public bool IsExistGroup(string identity)

 

        {

 

            try

 

            {

 

                PSSnapInException PSException = null;

 

                RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

 

                runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

 

                Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

 

                runspace.Open();

 

 

 

                Pipeline pipeline = runspace.CreatePipeline();

 

                Command command = new Command("Get-DistributionGroup");

 

                command.Parameters.Add("identity", identity);

 

                pipeline.Commands.Add(command);

 

                Collection<PSObject> result = pipeline.Invoke();

 

 

 

                runspace.Close();

 

 

 

                return (result != null && result.Count > 0);

 

            }

 

            catch (System.Exception ex)

 

            {

 

                throw ex;

 

            }

 

        }

 

        #endregion

 

 

 

        #region 创建通讯组

 

        public bool NewGroup(string name)

 

        {

 

            if (this.IsExistGroup(name))

 

            {

 

                throw new Exception("已经存在相同的通讯组"); 

 

            }

 

            try

 

            {

 

                PSSnapInException PSException = null;

 

                RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

 

                runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

 

                Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

 

                runspace.Open();

 

 

 

                Pipeline pipeline = runspace.CreatePipeline();

 

                Command command = new Command("New-DistributionGroup");

 

                command.Parameters.Add("Name", name);

 

                pipeline.Commands.Add(command);

 

                Collection<PSObject> result = pipeline.Invoke();

 

                runspace.Close(); 

 

                return this.IsExistGroup(name);

 

            }

 

            catch (Exception ex)

 

            {

 

                throw ex;

 

            }

 

        }

 

 

 

        #endregion

 

 

 

        #region 删除通讯组

 

        public bool RemoveGroup(string identity)

 

        {

 

            try

 

            {

 

                PSSnapInException PSException = null;

 

                RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

 

                runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

 

                Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

 

                runspace.Open();

 

 

 

                Pipeline pipeline = runspace.CreatePipeline();

 

                Command command = new Command("Remove-DistributionGroup");

 

                command.Parameters.Add("Identity", identity);

 

                command.Parameters.Add("Confirm", false);

 

                pipeline.Commands.Add(command);

 

                Collection<PSObject> result = pipeline.Invoke();

 

                runspace.Close();

 

                return !this.IsExistGroup(identity); 

 

            }

 

            catch (Exception ex)

 

            {

 

                throw ex;

 

            } 

 

        }

 

        #endregion

 

 

 

        #region 添加通讯组成员

 

        public bool AddGroupMember(string groupIdentity, string mailIdentity)

 

        {

 

            try

 

            {

 

                PSSnapInException PSException = null;

 

                RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

 

                runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

 

                Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

 

                runspace.Open();

 

 

 

                Pipeline pipeline = runspace.CreatePipeline();

 

                Command command = new Command("Add-DistributionGroupMember");

 

                command.Parameters.Add("Identity", groupIdentity);

 

                command.Parameters.Add("Member", mailIdentity);

 

                pipeline.Commands.Add(command);

 

                Collection<PSObject> result = pipeline.Invoke();

 

                runspace.Close();

 

                return true;

 

            }

 

            catch (Exception ex)

 

            {

 

                throw ex;

 

            }

 

        }

 

        #endregion

 

         

 

        #region 删除通讯组成员

 

        public bool RemoveGroupMember(string groupIdentity, string mailIdentity)

 

        {

 

            try

 

            {

 

                PSSnapInException PSException = null;

 

                RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

 

                runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

 

                Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

 

                runspace.Open();

 

 

 

                Pipeline pipeline = runspace.CreatePipeline();

 

                Command command = new Command("Remove-DistributionGroupMember");

 

                command.Parameters.Add("Identity", groupIdentity);

 

                command.Parameters.Add("Member", mailIdentity);

 

                command.Parameters.Add("Confirm", false);

 

                pipeline.Commands.Add(command);

 

                Collection<PSObject> result = pipeline.Invoke();

 

                runspace.Close();

 

                return true;

 

            }

 

            catch (Exception ex)

 

            {

 

                throw ex;

 

            }

 

        }

 

        #endregion

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网