十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問題一站解決
最近在工作中負(fù)責(zé)解決 用代碼在Exchange中為用戶創(chuàng)建郵箱 的問題
主要就是用C#封裝一個(gè)ExChange操作類,在ExChange的服務(wù)器上發(fā)布一個(gè)WebService 供系統(tǒng)的java代碼調(diào)用
t01144d31d02dd7ebfb.jpg
在查閱很多資料后,終于在
http://www.cnblogs.com/xiaogelove/archive/2011/02/17/1956617.html
這篇帖子的指導(dǎo)下把這個(gè)問題解決了
不過這個(gè)帖子講得并不是特別的詳細(xì)
所以我覺得有必要把我在這個(gè)過程中遇到的問題記錄下來(lái)
所以在上面這篇帖子的基礎(chǔ)上寫了一個(gè)相對(duì)詳細(xì)的新流程
==============================================================================
主要原理是:(這個(gè)原理是我猜的,因?yàn)槲乙膊惶瓻xChange以及COM組件這方面的知識(shí))
直接用C#代碼訪問ExChange是不行的
微軟出了一個(gè)PowerShell的命令行工具 能夠用命令行來(lái)操作ExChange
可以通過把.Net類注冊(cè)成COM+組件的方式來(lái)操作PowerShell
所以我的流程就是
WebService->.NET寫的PowerShell操作類注冊(cè)成的COM+組件->ExChange
環(huán)境是:
VS2010 + ExChange2010 + Windows Server 2008 64位版 +IIS7.0
ps:這個(gè)COM+組件只能運(yùn)行在安裝ExChange的服務(wù)器上
公司的環(huán)境用的是ExChange2010, ExChange2010好像只有64位版的 只能安裝在64位的系統(tǒng)上
所以下面會(huì)說(shuō)到把COM+組件編譯成64位的問題
==============================================================================
1 首先先創(chuàng)建com組件并注冊(cè)
1)啟動(dòng)Visual Studio 2010
2)選擇File ->“新建->”項(xiàng)目...
3)選擇Windows
4)選擇“類庫(kù)”
5)在名稱框中鍵入“PowerShellComponent “
6)點(diǎn)擊確定。
7)添加下列引用
System.EnterpriseServices
System.DirectoryServices
System.Management.Automation路徑:
32位系統(tǒng):
C:ProgramFilesReferenceAssembliesMicrosoftWindowsPowerShellv1.System.Management.Automation.dll
64位系統(tǒng)
C:Program Files (x86)Reference AssembliesMicrosoftWindowsPowerShellv1.0System.Management.Automation.dll
接下來(lái)有關(guān)程序集的操作
1)在解決方案資源管理器,右鍵單擊PowerShellComponent項(xiàng)目,選擇屬性,點(diǎn)擊簽名選項(xiàng),選中"為程序集簽名",并創(chuàng)建一個(gè)新的強(qiáng)名稱密鑰稱為“PowerShellComponent.snk”,不用設(shè)置密碼。如下圖
2)還是在項(xiàng)目屬性窗口中,選擇"應(yīng)用程序"選項(xiàng)卡,然后點(diǎn)擊“程序集信息...”,檢查框,選中"使程序集COM可見"。如圖
PS:如果運(yùn)行這個(gè)com組件的機(jī)器是64位系統(tǒng)(32位的沒試過),這里需要再加一步:
把項(xiàng)目的運(yùn)行平臺(tái)設(shè)置成64位的
還是在項(xiàng)目屬性窗口中:
"生成"選項(xiàng)卡->目標(biāo)平臺(tái)->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)選擇“解決方案資源管理器”查看選項(xiàng)卡。將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)拷貝下面的方法到類中
復(fù)制代碼
1 #region 根據(jù)登錄名判斷是否存在郵箱 2 3 public bool IsExistMailBox(string identity) 4 5 { 6 7 try 8 9 { 10 11 PSSnapInException PSException = null; 12 13 RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 14 15 runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 16 17 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 18 19 runspace.Open(); 20 21 22 23 Pipeline pipeline = runspace.CreatePipeline(); 24 25 Command command = new Command("Get-Mailbox"); 26 27 command.Parameters.Add("identity", identity); 28 29 pipeline.Commands.Add(command); 30 31 Collection result = pipeline.Invoke(); 32 33 34 35 runspace.Close(); 36 37 38 39 return (result != null && result.Count > 0); 40 41 } 42 43 catch (System.Exception ex) 44 45 { 46 47 throw ex; 48 49 } 50 51 } 52 53 #endregion 54 55 56 57 #region 創(chuàng)建郵箱賬號(hào) 58 59 public bool NewMailbox(string name, string accountName, string pwd, string emailDomain, string organizationalUnit, string database) 60 61 { 62 63 string emailAdd = accountName + emailDomain; 64 65 66 67 if (this.IsExistMailBox(emailAdd)) 68 69 { 70 71 throw new Exception("已經(jīng)存在同名的郵箱"); 72 73 } 74 75 try 76 77 { 78 79 PSSnapInException PSException = null; 80 81 RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 82 83 runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 84 85 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 86 87 runspace.Open(); 88 89 Pipeline pipeline = runspace.CreatePipeline(); 90 91 92 93 Command command = new Command("New-Mailbox"); 94 95 char[] passwordChars = pwd.ToCharArray(); 96 97 SecureString password = new SecureString(); 98 99 foreach (char c in passwordChars) 100 101 { 102 103 password.AppendChar(c); 104 105 } 106 107 108 109 command.Parameters.Add("Name", name);//姓名 110 111 112 113 command.Parameters.Add("UserPrincipalName", emailAdd);//郵箱地址 114 115 command.Parameters.Add("SamAccountName", accountName);//登錄名 116 117 118 119 command.Parameters.Add("Password", password);//密碼 120 121 122 123 command.Parameters.Add("OrganizationalUnit", organizationalUnit);//組織單元 124 125 command.Parameters.Add("Database", database);//數(shù)據(jù)庫(kù) 126 127 128 129 pipeline.Commands.Add(command); 130 131 Collection result = pipeline.Invoke(); 132 133 runspace.Close(); 134 135 136 137 return this.IsExistMailBox(emailAdd); 138 139 } 140 141 catch (Exception ex) 142 143 { 144 145 throw ex; 146 147 } 148 149 } 150 151 #endregion 152 153 154 155 #region 刪除郵箱賬號(hào)(控制臺(tái)和域都刪除) 156 157 158 159 public bool RemoveMailbox(string identity) 160 161 { 162 163 164 165 try 166 167 { 168 169 PSSnapInException PSException = null; 170 171 RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 172 173 runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 174 175 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 176 177 runspace.Open(); 178 179 Pipeline pipeline = runspace.CreatePipeline(); 180 181 182 183 Command command = new Command("Remove-Mailbox"); 184 185 command.Parameters.Add("Identity", identity); 186 187 command.Parameters.Add("Confirm", false); 188 189 pipeline.Commands.Add(command); 190 191 Collection result = pipeline.Invoke(); 192 193 runspace.Close(); 194 195 196 197 return !this.IsExistMailBox(identity); 198 199 } 200 201 catch (System.Exception ex) 202 203 { 204 205 throw ex; 206 207 } 208 209 } 210 211 #endregion 212 213 214 215 #region 啟用郵箱賬號(hào) 216 217 public bool EnableMailbox(string identity) 218 219 { 220 221 try 222 223 { 224 225 PSSnapInException PSException = null; 226 227 RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 228 229 runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 230 231 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 232 233 runspace.Open(); 234 235 Pipeline pipeline = runspace.CreatePipeline(); 236 237 238 239 Command command = new Command("Enable-Mailbox"); 240 241 command.Parameters.Add("Identity", identity); 242 243 command.Parameters.Add("Confirm", false); 244 245 pipeline.Commands.Add(command); 246 247 Collection result = pipeline.Invoke(); 248 249 runspace.Close(); 250 251 return this.IsExistMailBox(identity); 252 253 } 254 255 catch (Exception ex) 256 257 { 258 259 throw ex; 260 261 } 262 263 } 264 265 #endregion 266 267 268 269 #region 禁用郵箱賬號(hào) 270 271 public bool DisableMailbox(string identity) 272 273 { 274 275 try 276 277 { 278 279 PSSnapInException PSException = null; 280 281 RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 282 283 runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 284 285 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 286 287 runspace.Open(); 288 289 290 291 Pipeline pipeline = runspace.CreatePipeline(); 292 293 Command command = new Command("Disable-Mailbox"); 294 295 command.Parameters.Add("Identity", identity); 296 297 command.Parameters.Add("Confirm", false); 298 299 pipeline.Commands.Add(command); 300 301 Collection result = pipeline.Invoke(); 302 303 runspace.Close(); 304 305 return !this.IsExistMailBox(identity); 306 307 } 308 309 catch (Exception ex) 310 311 { 312 313 throw ex; 314 315 } 316 317 } 318 319 #endregion 320 321 322 323 #region 判斷是否存在通訊組 324 325 public bool IsExistGroup(string identity) 326 327 { 328 329 try 330 331 { 332 333 PSSnapInException PSException = null; 334 335 RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 336 337 runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 338 339 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 340 341 runspace.Open(); 342 343 344 345 Pipeline pipeline = runspace.CreatePipeline(); 346 347 Command command = new Command("Get-DistributionGroup"); 348 349 command.Parameters.Add("identity", identity); 350 351 pipeline.Commands.Add(command); 352 353 Collection result = pipeline.Invoke(); 354 355 356 357 runspace.Close(); 358 359 360 361 return (result != null && result.Count > 0); 362 363 } 364 365 catch (System.Exception ex) 366 367 { 368 369 throw ex; 370 371 } 372 373 } 374 375 #endregion 376 377 378 379 #region 創(chuàng)建通訊組 380 381 public bool NewGroup(string name) 382 383 { 384 385 if (this.IsExistGroup(name)) 386 387 { 388 389 throw new Exception("已經(jīng)存在相同的通訊組"); 390 391 } 392 393 try 394 395 { 396 397 PSSnapInException PSException = null; 398 399 RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 400 401 runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 402 403 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 404 405 runspace.Open(); 406 407 408 409 Pipeline pipeline = runspace.CreatePipeline(); 410 411 Command command = new Command("New-DistributionGroup"); 412 413 command.Parameters.Add("Name", name); 414 415 pipeline.Commands.Add(command); 416 417 Collection result = pipeline.Invoke(); 418 419 runspace.Close(); 420 421 return this.IsExistGroup(name); 422 423 } 424 425 catch (Exception ex) 426 427 { 428 429 throw ex; 430 431 } 432 433 } 434 435 436 437 #endregion 438 439 440 441 #region 刪除通訊組 442 443 public bool RemoveGroup(string identity) 444 445 { 446 447 try 448 449 { 450 451 PSSnapInException PSException = null; 452 453 RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 454 455 runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 456 457 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 458 459 runspace.Open(); 460 461 462 463 Pipeline pipeline = runspace.CreatePipeline(); 464 465 Command command = new Command("Remove-DistributionGroup"); 466 467 command.Parameters.Add("Identity", identity); 468 469 command.Parameters.Add("Confirm", false); 470 471 pipeline.Commands.Add(command); 472 473 Collection result = pipeline.Invoke(); 474 475 runspace.Close(); 476 477 return !this.IsExistGroup(identity); 478 479 } 480 481 catch (Exception ex) 482 483 { 484 485 throw ex; 486 487 } 488 489 } 490 491 #endregion 492 493 494 495 #region 添加通訊組成員 496 497 public bool AddGroupMember(string groupIdentity, string mailIdentity) 498 499 { 500 501 try 502 503 { 504 505 PSSnapInException PSException = null; 506 507 RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 508 509 runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 510 511 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 512 513 runspace.Open(); 514 515 516 517 Pipeline pipeline = runspace.CreatePipeline(); 518 519 Command command = new Command("Add-DistributionGroupMember"); 520 521 command.Parameters.Add("Identity", groupIdentity); 522 523 command.Parameters.Add("Member", mailIdentity); 524 525 pipeline.Commands.Add(command); 526 527 Collection result = pipeline.Invoke(); 528 529 runspace.Close(); 530 531 return true; 532 533 } 534 535 catch (Exception ex) 536 537 { 538 539 throw ex; 540 541 } 542 543 } 544 545 #endregion 546 547 548 549 #region 刪除通訊組成員 550 551 public bool RemoveGroupMember(string groupIdentity, string mailIdentity) 552 553 { 554 555 try 556 557 { 558 559 PSSnapInException PSException = null; 560 561 RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 562 563 runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 564 565 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 566 567 runspace.Open(); 568 569 570 571 Pipeline pipeline = runspace.CreatePipeline(); 572 573 Command command = new Command("Remove-DistributionGroupMember"); 574 575 command.Parameters.Add("Identity", groupIdentity); 576 577 command.Parameters.Add("Member", mailIdentity); 578 579 command.Parameters.Add("Confirm", false); 580 581 pipeline.Commands.Add(command); 582 583 Collection result = pipeline.Invoke(); 584 585 runspace.Close(); 586 587 return true; 588 589 } 590 591 catch (Exception ex) 592 593 { 594 595 throw ex; 596 597 } 598 599 } 600 601 #endregion 602 603
復(fù)制代碼
PS: 這些都是ExChange的命令,暫時(shí)只封裝了這么多,如果想實(shí)現(xiàn)更多的功能,只需要照著上面的例子把實(shí)現(xiàn)相應(yīng)的ExChange命令就行了
在微軟的官網(wǎng)上有ExChange的命令文檔http://msdn.microsoft.com/zh-cn/library/aa997174.aspx
最后運(yùn)行生成項(xiàng)目,得到PowerShellComponent.dll,COM組件就創(chuàng)建好了。
接下來(lái)就是注冊(cè)這個(gè)組件了:
步驟一:
【控制面板】→【管理工具】→【組件服務(wù)】
步驟二:
出現(xiàn)窗口后,【組件服務(wù)】→【計(jì)算機(jī)】→【我的電腦】→【COM+ 應(yīng)用程序】單擊右鍵 →新建→ 應(yīng)用程序→安裝向?qū)乱徊?rarr;創(chuàng)建空應(yīng)用程序→輸入空應(yīng)用程序名稱:PowerShellComponent,并選擇激活類型為服務(wù)器應(yīng)用程序→設(shè)置應(yīng)用程序標(biāo)示(賬號(hào)選擇下列用戶 賬號(hào)和密碼是該服務(wù)器登錄用戶名和密碼)→完成。
右鍵單擊創(chuàng)建出來(lái)的PowerShellComoponent,選擇屬性,找到"標(biāo)志"選項(xiàng)卡,選擇 ”下列用戶“填入計(jì)算機(jī)的登錄用戶名和密碼,確定
步驟三:
創(chuàng)建好應(yīng)用程序后 打開PowerShellComponent出現(xiàn) 【組件】【舊版組件】【角色】在【組件】上單擊右鍵 →新建→組件
步驟三:
點(diǎn)下一步,出現(xiàn)如下窗口,選擇【安裝新組件】:
選擇前面項(xiàng)目生成的PowerShellComponent.dll文件→【打開】點(diǎn)下一步,選擇完成。
步驟四:
為剛剛注冊(cè)的PowerShellComponent組件添加用戶權(quán)限
打開PowerShellComponent 下面的【角色】-【CreatorOwner】-【用戶】右鍵【新建】-【用戶】
在出來(lái)的窗口點(diǎn)[高級(jí)]-[位置]-選擇[整個(gè)目錄]-[立即查找]
因?yàn)閃ebServicce是發(fā)布在IIS上面的 所以我的IIS需要有權(quán)限來(lái)操作這個(gè)COM組件 所以我添加的是IIS的用戶
在搜索出來(lái)的結(jié)果里面 選擇IIS_IUSRS并添加, 如果是用winform來(lái)調(diào)用這個(gè)COM+組件 則應(yīng)該要添加管理員帳號(hào)Administrator
用戶添加完了 組件就注冊(cè)成功了。
把PowerShellComponent.dll拷到測(cè)試項(xiàng)目中
測(cè)試項(xiàng)目添加對(duì)PowerShellComponent.dll的引用 就能被調(diào)用了
如果你的COM+組件被啟用了 在調(diào)試過程中如果你需要重新編譯你的DLL文件 那么需要先關(guān)閉COM+組件 dll才能被重新編譯
如果在調(diào)用的過程中發(fā)生異常,可能是兩個(gè)方面的原因:
1 如果com+組件在64位的環(huán)境下運(yùn)行 是否有被編譯成64位
2 權(quán)限問題
還有一個(gè)
因?yàn)椴僮鞯氖荅xChange2010 所以代碼中是
PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);
如果你的ExChange是2007的 那么這行代碼可能需要被改成
PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out PSException);