在asp.net中使用Powershell操作Exchange 2007(啓用郵箱)

原文地址:
 
本文主要介紹如何在asp.net中對Exchange服務器進行管理員級別的操作,主要實現是通過Exchange 管理工具實現,因此,程序主機必須安裝Exchange 管理工具。
 
最近的一個項目需求,需要在sharepoint服務器上實現創建AD賬號,並啓用郵箱。
按照以前的僅有的開發經驗,弱智的嘗試用Web Service實現,用管理員的Credential,但是Web Service提供的29中operation中只是類似於outlook的操作。又因爲,2007版本中沒有CDOEXM,只能通過Exchange Management Shell。因爲創建郵箱這樣的操作必須具有高權限,而在自己的Application中的用戶不一定有此權限,所以需要使用COM+實現。
 
具體方法如下:
1、首先程序機器上必須安裝有Windows PowerShell 和 Exchange 管理工具。
2、創建項目,選擇類庫。
3、添加引用System.EnterpriseServices, System.Management.Automation(C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\)。強命名!
4、在AssemblyInfo.cs中添加 “using System.EnterpriseServices;”
並在最後加上:
[assembly: ApplicationActivation(ActivationOption.Server)]    
[assembly: ApplicationName("你的application名字")]    
[assembly: Description("PowerShell組件")]    
[assembly: ApplicationAccessControl(    
                     false,    
                     AccessChecksLevel = AccessChecksLevelOption.Application,    
                     Authentication = AuthenticationOption.None,    
                     ImpersonationLevel = ImpersonationLevelOption.Identify)]
添加代碼
1)將 class1.cs 改爲 “ManagementCommands.cs”.
類需要繼承System.EnterpriseServices.ServicedComponent
2)代碼如下
           
RunspaceConfiguration config = RunspaceConfiguration.Create();
                        PSSnapInException warning;

                        config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out warning);
                        if (warning != null) throw warning;

                        Runspace thisRunspace = RunspaceFactory.CreateRunspace(config)
                        thisRunspace.Open();
                        Pipeline thisPipeline = thisRunspace.CreatePipeline();

                        thisPipeline.Commands.Add("Enable-Mailbox");
                        thisPipeline.Commands[0].Parameters.Add("Identity", @"DOM157711\xxxxxxx");
                        thisPipeline.Commands[0].Parameters.Add("Database", @"XXXXXXX-8\First Storage Group\Mailbox Database");
                        thisPipeline.Commands[0].Parameters.Add("DomainController", domainController);
                        thisPipeline.Invoke();
                        thisRunspace.Close();
 創建和配置COM+組件
生成工程
用Visual Studio 命令行工具 運行 "regsvcs PowerShellComponent.dll"
然後 在管理工具-->組件服務-->我的電腦-->COM+應用程序-->PowerShellComponent
右擊屬性-標識,在“下列用戶”中輸入Exchange的管理員用戶名及密碼
組件就創建完成
在應用程序中使用

把PowerShellComponent.dll拷貝到.net應用程序的bin目錄下,添加引用。

      
PowerShellComponent.ManagementCommands objManage = new PowerShellComponent.ManagementCommands();

             String Results;
             Results = objManage.EnableMailbox(domain1, domainController1);
             Response.Write(Results);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章