.NET通过PowerShell操作ExChange为用户开通邮箱教程
转:http://www.cnblogs.com/gongguo/archive/2012/03/12/2392049.html
==============================================================================
要使.NET调用PowerShell组建能够管理Exchange必须在调用的时候加载管理模块,否则和Exchange相关的指令就不被支持。虽然并不是很明白这两种加载模块方式的具体区别,但是由于手动加载Exchange管理模块有两种方式,.NET(或者直接说C#)管理Exchange就有2种方式。第一种方式是将代码编译成COM+组建,注册到COM+应用程序中,以供客户机代码调用,第二种方式不需要注册COM+组建,更大程度得益于“远程管理”,第二种的调用方式五花八门,详情可以参考Exchange小组的技术博客
1.COM+:要求部署在Exchange服务器上(因为有PowerShell Modules),一般选用CAS服务器。另外COM+应用程序在没有使用的情况下会自动关闭,一有请求又开启,所以第一次调用会显得有点慢。
2.远程调用。主要是要基于Kerberos的身份验证,使用SSL加密模式也可以,但是就要配置证书,所以目前我使用的都是Kerberos模式的身份验证。可以在同一个AD上部署,或者在建立了信任关系的其他AD中部署。相比第一种来说灵活(差别不大),也干净一点。但是我尚未测试使用WEB调用的情况,所以有没有其他问题尚未明确。
主要原理是:(这个原理是我猜的,因为我也不太懂ExChange以及COM组件这方面的知识)
直接用C#代码访问ExChange是不行的
微软出了一个PowerShell的命令行工具 能够用命令行来操作ExChange
可以通过把.Net类注册成COM+组件的方式来操作PowerShell
所以我的流程就是
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 > );
}
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 > );
}
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
view code
PS: 这些都是ExChange的命令,暂时只封装了这么多,如果想实现更多的功能,只需要照着上面的例子把实现相应的ExChange命令就行了
在微软的官网上有ExChange的命令文档 http://msdn.microsoft.com/zh-cn/library/aa997174.aspx
https://msdn.microsoft.com/zh-cn/library/aa998225.aspx
另:还有远程调用方法,其实不一定要通过注册COM+组件的方式来完成任务,PowerShell支持远程管理,使用.NET也可以得益于这个功能。
// <summary>
/// 邮箱管理员凭证
/// </summary>
/// <returns></returns>
static WSManConnectionInfo OpenWSManConnInfo()
{
//Lync管理webservice地址 邮箱地址
//string uri = "http://sss-ex01.dev.com/PowerShell";
string uri = ConfigUtils.GetLocalParamValue("EmailAddress");
//命令解析uri
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
//管理账户
//string userName = @"dev\devadmin";
string DomainName = ConfigUtils.GetLocalParamValue("DomainName");//域
string AdminName = ConfigUtils.GetLocalParamValue("AdminName"); string adminName = AdminName + "\\" + DomainName; //管理密码
//string userPassWord = "admin";
string adminPassWord = ConfigUtils.GetLocalParamValue("AdminPassword");
//UPN后缀名
string SuffixName = ConfigUtils.GetLocalParamValue("UserPrincipalName"); System.Security.SecureString secPwd = new SecureString();
char[] adminPwds = adminPassWord.ToCharArray();
foreach (char c in adminPwds)
{
secPwd.AppendChar(c);
}
//下面这句屏蔽服务器证书验证,防止页面报“根据验证过程,远程证书无效”的错误
//ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }; //RunspaceConfiguration config = RunspaceConfiguration.Create();
PSCredential psc = new PSCredential(adminName, secPwd);
WSManConnectionInfo wsManConnInfo = new WSManConnectionInfo(new Uri(uri), shellUri, psc);
wsManConnInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;//计算机加入了域
return wsManConnInfo; /******** Runspace runspace = RunspaceFactory.CreateRunspace(wsManConnInfo);
if ((runspace.RunspaceStateInfo.State == RunspaceState.Closed) || (runspace.RunspaceStateInfo.State == RunspaceState.BeforeOpen))
{
runspace.Open();
}
else if (runspace.RunspaceStateInfo.State == RunspaceState.Broken)
{
runspace.Open();
} //PowerShell powerShell = PowerShell.Create();
//powerShell.Runspace = runspace;
//PSCommand psCommand = new PSCommand(); Pipeline pipeLine = runspace.CreatePipeline(); Command command = new Command("New-Mailbox"); //New-Mailbox 邮箱命令
char[] passwordChars = userPassWord.ToCharArray();
SecureString password = new SecureString();
foreach (char c in passwordChars)
{
password.AppendChar(c);
}
command.Parameters.Add("Name", userName);//姓名
command.Parameters.Add("UserPrincipalName", emailAdd);//邮箱地址
command.Parameters.Add("SamAccountName", accountName);//登录名
command.Parameters.Add("Password", password);//密码
//command.Parameters.Add("OrganizationalUnit", "");//组织单元
command.Parameters.Add("DisplayName", userName);
//command.Parameters.Add("Database", "Mailbox Database 1406924081");//数据库
pipeLine.Commands.Add(command); Collection<PSObject> result = pipeLine.Invoke();
runspace.Close(); return IsExistMailBox(emailAdd); *******/ }
最后运行生成项目,得到PowerShellComponent.dll,COM组件就创建好了。
接下来就是注册这个组件了:
步骤一: 【控制面板】→【管理工具】→【组件服务】

步骤二: 出现窗口后,【组件服务】→【计算机】→【我的电脑】→【COM+ 应用程序】单击右键 →新建→ 应用程序→安装向导下一步→创建空应用程序→输入空应用程序名称:PowerShellComponent,并选择激活类型为服务器应用程序→设置应用程序标示(账号选择下列用户 账号和密码是该服务器登录用户名和密码)→完成。
右键单击创建出来的PowerShellComoponent,选择属性,找到"标志"选项卡,选择 ”下列用户“ 填入计算机的登录用户名和密码,确定
步骤三: 创建好应用程序后 打开PowerShellComponent 出现 【组件】【旧版组件】【角色】 在【组件】上单击右键 →新建→组件

步骤三: 点下一步,出现如下窗口,选择【安装新组件】:

选择前面项目生成的PowerShellComponent.dll文件→【打开】点下一步,选择完成。
步骤四:
为刚刚注册的PowerShellComponent组件添加用户权限
打开PowerShellComponent 下面的【角色】-【CreatorOwner】-【用户】右键 【新建】 - 【用户】

在出来的窗口点[高级]-[位置]-选择[整个目录]-[立即查找]



因为WebServicce是发布在IIS上面的 所以我的IIS需要有权限来操作这个COM组件 所以我添加的是IIS的用户
在搜索出来的结果里面 选择IIS_IUSRS并添加, 如果是用winform来调用这个COM+组件 则应该要添加管理员帐号Administrator

用户添加完了 组件就注册成功了。
把PowerShellComponent.dll拷到测试项目中
测试项目添加对 PowerShellComponent.dll 的引用 就能被调用了
如果你的COM+组件被启用了 在调试过程中如果你需要重新编译你的DLL文件 那么需要先关闭COM+组件 dll才能被重新编译

如果在调用的过程中发生异常,可能是两个方面的原因:
1 如果com+组件在64位的环境下运行 是否有被编译成64位
2 权限问题
还有一个
因为操作的是ExChange2010 所以代码中是
PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);
如果你的ExChange是2007的 那么这行代码可能需要被改成
PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out PSException);
http://ishare.iask.sina.com.cn/f/23481273.html
这是我的源代码,赚点积分,勿怪
另外的参考资料:http://www.cnblogs.com/lightluomeng/archive/2013/01/20/2868028.html
http://www.cnblogs.com/lightluomeng/archive/2013/01/20/2868028.html
.NET通过PowerShell操作ExChange为用户开通邮箱教程的更多相关文章
- .NET通过PowerShell操作ExChange为用户开通邮箱账号
最近工作中一个web项目需要集成exchange邮箱服务,注册用户时需要动态创建邮箱用户,终于在http://www.cnblogs.com/gongguo/archive/2012/03/12/23 ...
- C#使用PowerShell 操作Exchange
先介绍一篇文章来参考一下 点开文章 该文章的最后使用了SSL来保证账户在连接服务器的时候不发生账户认证错误,但是我经过测试发现这个是不可行的,有一种更为简单的方法 首先要对服务器进行winrm设置 就 ...
- 一种读取Exchange的用户未读邮件数方法!
已好几个月没写博客了,由于之前忙于开发基于Sharepoint上的移动OA(AgilePoint)和采用混合移动开发技术开发一个安卓版的企业通讯录APP(数据与lync一致),并于1月初正式上线.马年 ...
- 模拟开户接口,使用python脚本实现批量用户开通
1.目的 通过模拟接口方法,实现批量用户开通 2.分析 A.接口含body和head部分,其中body中的某些变量为必填字段,包含用户的信息. B.用户信息清单可以整理成ott_after_check ...
- 模拟开户接口,使用shell脚本实现批量用户开通
1.目的 通过模拟接口方法,实现批量用户开通 2.分析 A.接口含body和head部分,其中body中的某些变量为必填字段,包含用户的信息,接口可整理成body.xml.head.xml文件. B. ...
- jQuery 操作cookie保存用户浏览信息
使用jQuery操作cookie之前需要引入jQuery的一个cookie小组件js,代码如下: /* jQuery cookie plugins */jQuery.cookie ...
- 安全错误使用CORS在IE10与Node和Express及XMLHttpRequest: 网络错误 0x4c7, 操作已被用户取消
在IE下:VUE项目,后台替换为https请求之后,vue热更新请求挂起,控制台报错:XMLHttpRequest: 网络错误 0x4c7, 操作已被用户取消.但是chrome与Firefox正常 当 ...
- MySQL优化之——为用户开通mysql权限
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46627263 为用户开通mysql权限: grant all privileges ...
- Exchange 导出用户邮箱
应用场景: 1.需要把某个用户的邮箱内容全部导出来,提供给审计或监察部门. 2.跨平台的迁移,从第三方的邮件系统迁移到exchange.其中一种迁移方式就是把用户批量导出为PST,然后在exchang ...
随机推荐
- scala lambda 表达式 & spark RDD函数操作
形式:(参数)=> 表达式 [ 一种匿名函数 ] 例1:map(x => x._2) 解:x=输入参数,“=>” 右边是表达式(处理参数): x._2 : x变为(**,x,**. ...
- java中计算一段时间内白天的时间和夜晚的时间
之前,采用拼接字符串的形式,不断地在Date类型和Long类型之间转换,实在是太过于麻烦,后来采取了这种思路:假设我们将22:00 ~ 10:00 视为夜间时间,则我们先计算出10:00 相对于当天的 ...
- Python之图片缩放功能实现
这几天由于有项目在做,自己的学习部分然后没有很充足的时间,但是这些零碎的时间也是很宝贵的,所以还是继续学我的python,我很喜欢这个语言,因为简洁,开发环境简单,更多的事,功能灰常的强大,所以好多有 ...
- Node JS爬虫:爬取瀑布流网页高清图
原文链接:Node JS爬虫:爬取瀑布流网页高清图 静态为主的网页往往用get方法就能获取页面所有内容.动态网页即异步请求数据的网页则需要用浏览器加载完成后再进行抓取.本文介绍了如何连续爬取瀑布流网页 ...
- 2019.2.25考试T3, 离线+线段树
\(\color{#0066ff}{题解}\) #include<bits/stdc++.h> #define LL long long LL in() { char ch; LL x = ...
- [比赛|考试]nowcoder NOIPpj组第二场
nowcoder NOIPpj组第二场 370pts/400pts(100,100,100,70) rank3 给自己的反思:前3题都A了,T4O(N^2)不会就是不会(没准是我懒得推了),DP了70 ...
- Leetcode 520. Detect Capital 发现大写词 (字符串)
Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...
- socket套接字基本概念
int socket()函数创建的是套接字socket,返回的是socket描述符(套接字描述符),其实就是文件描述符,socket(套接字)其实就是文件 socket()创建了套接字(文件),只是开 ...
- 2018北京网络赛D 80days (尺取)
#1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules ...
- freemarker常用标签解释
标签一: if else 你可以使用if,elseif和else指令来条件判断是否越过模板的一个部分.这些condition-s必须计算成布尔值,否则错误将会中止模板处理.elseif-s和else- ...