进入Exchange 2010 时代,终于可以实现自动化了。前两天看到一老外写的关于Cmdlet Extension Agents的使用,觉得蛮有意思的,自己测试了下,果然不错。下面是一些分享。

首先,我们需要创建一个Receive connector 来接受从特定IP地址段来的 free relay。命令行中的IP地址段可自行修改,请注意此处的安全隐患。

  1. New-ReceiveConnector -Name "Internal Relay" -Bindings 0.0.0.0:25 -RemoteIPRanges 127.0.0.1,192.168.100.1-192.168.100.255 -AuthMechanism None -Enabled $true -Fqdn "ex2010cas01.mcmhost.com" -PermissionGroups AnonymousUsers -Server ex2010cas01 | Add-ADPermission -User "NT AUTHORITY\ANONYMOUS LOGON" -ExtendedRights "ms-Exch-SMTP-Accept-Any-Recipient"
复制代码

第二步,创建一个脚本来发送欢迎邮件到特定邮箱。我们为这个脚本取名为send-mail.ps1, 命令行参数为:

  1. send-mail.ps1 –mailboxname <邮箱名>
复制代码

代码如下, 请修改变量定义中的邮件地址,请注意邮件中会自动插入出用户的用户名,邮件地址等信息,另外,可自行修改声明。第三步之前,可先行运行此脚本,保证脚本能正常工作。

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

  1. param
     
  2. (
     
  3.     [string]$mailboxname
     
  4. )
     

  5.  
  6. #此处填上发信人地址
     
  7. $strMsgFrom = "MCMHost HelpDesk <user01@MCMhost.com>"
     

  8.  
  9. #邮件标题
     
  10. $strMsgTitle = "Welcome to MCMhost!"
     

  11.  
  12. #SMTP relay host name,一般是HUB服务器或者是某内部SMTP gateway
     
  13. $SMTPClient = New-Object Net.Mail.SmtpClient("ex2010cas01.mcmhost.com")
     

  14.  
  15. $mailbox = get-mailbox -id $mailboxname
     
  16. $strMsgTo = $mailbox.PrimarySMTPAddress
     

  17.  
  18. $strMsgBody = "您好, "+$mailbox.DisplayName+", 欢迎使用MCMhost邮件系统!
     

  19.  
  20. --------------------------------------
     
  21. 用户名和密码
     
  22. --------------------------------------
     
  23. 您的登录账户名是 is '"+$mailbox.SamAccountName+"'. Use your username and password to login to the network. Your password should NEVER be shared with anyone except the I.T. department, and only then when requested. Please do not write it down on anything that can be seen by your coworkers. You will be prompted to change it regularly.
     

  24.  
  25. --------------------------------------
     
  26. 邮箱
     
  27. --------------------------------------
     

  28.  
  29. 您的邮件地址是 '"+$mailbox.PrimarySMTPAddress+"'.
     

  30.  
  31. To access your email, calendar, contacts, and tasks from outside of the building, such as from home, you can do so from any Internet connected computer. Simply open Internet Explorer and go to the Outlook Web Access (OWA) page at https://mail.MCMhost.com/ and log in using your username and password. Please note the 's' in https.
     

  32.  
  33. If you'd like to have access to your email and contacts from your cell phone, you will need a cell phone that has Windows Mobile 5 or later, or an Apple iPhone. Blackberry phones are not supported. Instructions for configuring your device can be found in the Frequently Asked Questions (FAQ) section of the MCMhost Intranet at https://intranet.MCMhost.com/helpdesk/Lists/SupportFaq/AllItems.aspx
     
  34. --------------------------------------
     
  35. Contact information
     
  36. --------------------------------------
     
  37. Once you're situated, please go to http://directory/DirectoryUpdate and update your information. Log in using your username and password. It's important that you update your information anytime something changes, such as title, department, phone number, etc. This information is used in various systems and applications, and is your responsibility to keep up to date.
     

  38.  
  39. --------------------------------------
     
  40. Computer, Email, and Internet policies
     
  41. --------------------------------------
     
  42. MCMhost, Inc. provides a computer for your work tasks. The use of personally owned computers and related equipment is not permitted on our network. Additional information about use of MCMhost computers, email, Internet, etc. can be found in the Employee Handbook located in the HR section of the intranet at https://intranet.MCMhost.com/hr/
     

  43.  
  44. --------------------------------------
     
  45. Technical assistance
     
  46. --------------------------------------
     
  47. Should you need technical assistance, please check the Frequently Asked Questions (FAQ) section of the MCMhost Intranet at https://intranet.MCMhost.com/helpdesk/Lists/SupportFaq/AllItems.aspx. If you cannot find an answer there, submit a Service Request on the MCMhost intranet at https://intranet.MCMhost.com/helpdesk. If you are unable to access the intranet site, only then should you email HelpDesk@MCMhost.com. It is monitored by the whole IT department, and will ensure your issue is resolved in a timely manner.
     

  48.  
  49. Thank you, and, again, welcome to MCMhost!
     
  50. The Information Technology Department"
     

  51.  
  52. $SMTPClient.Send($strMsgFrom,$strMsgTo,$strMsgTitle,$strMsgBody)
复制代码

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



第三步, 去到C:\Program Files\Microsoft\Exchange Server\V14\Bin\CmdletExtensionAgents 目录下(如果不是缺省安装路径,可自行修改)。创建一个XML文件ScriptingAgentConfig.xml

XML文件内容如下,其作用是当完成new-mailbox这个命令后,自动调用c:\temp\send-mail.ps1脚本发送欢迎邮件,并且禁止邮箱的IMAP和POP3访问。这里的功用可无限引申出去(注意,在某些时候script agent的优先级需要比provisionagent的优先级高,比如对database做操作的命令,此处就不赘述了)。

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

  1. <?xml version="1.0" encoding="utf-8" ?>
     
  2. <Configuration version="1.0">
     
  3.                 <Feature Name="MailboxProvisioning" Cmdlets="new-mailbox">
     
  4.                                 <ApiCall Name="OnComplete">
     
  5.                                                 if($succeeded)    {
     
  6.                                                                 $newmailbox = $provisioningHandler.UserSpecifiedParameters["Name"]
     
  7.                                                                 c:\temp\send-mail.ps1 -mailboxname $newmailbox
     
  8.                                                                 Set-CASMailbox $newmailbox -IMAPEnabled $false -POPEnabled $false
     
  9.                                                 }
     
  10.                                 </ApiCall>
     
  11.                 </Feature>
     
  12. </Configuration>
复制代码

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



第四步,关闭所有服务器上目前打开的EMS和EMC。然后拷贝该XML去您环境中的每一个Exchange 服务器。别怪我没告诉你噢,等下跟我说你打不开EMC了。

第五部,使用下面命令创建一个新用户(也可用界面创建)。请自行修改 OU路径和用户名等参数。 进入Exchange 2010 时代,终于可以实现自动化了。前两天看到一老外写的关于Cmdlet Extension Agents的使用,觉得蛮有意思的,自己测试了下,果然不错。下面是一些分享。

首先,我们需要创建一个Receive connector 来接受从特定IP地址段来的 free relay。命令行中的IP地址段可自行修改,请注意此处的安全隐患。

  1. New-ReceiveConnector -Name "Internal Relay" -Bindings 0.0.0.0:25 -RemoteIPRanges 127.0.0.1,192.168.100.1-192.168.100.255 -AuthMechanism None -Enabled $true -Fqdn "ex2010cas01.mcmhost.com" -PermissionGroups AnonymousUsers -Server ex2010cas01 | Add-ADPermission -User "NT AUTHORITY\ANONYMOUS LOGON" -ExtendedRights "ms-Exch-SMTP-Accept-Any-Recipient"
复制代码

第二步,创建一个脚本来发送欢迎邮件到特定邮箱。我们为这个脚本取名为send-mail.ps1, 命令行参数为:

  1. send-mail.ps1 –mailboxname <邮箱名>
复制代码

代码如下, 请修改变量定义中的邮件地址,请注意邮件中会自动插入出用户的用户名,邮件地址等信息,另外,可自行修改声明。第三步之前,可先行运行此脚本,保证脚本能正常工作。

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

  1. param
     
  2. (
     
  3.     [string]$mailboxname
     
  4. )
     

  5.  
  6. #此处填上发信人地址
     
  7. $strMsgFrom = "MCMHost HelpDesk <user01@MCMhost.com>"
     

  8.  
  9. #邮件标题
     
  10. $strMsgTitle = "Welcome to MCMhost!"
     

  11.  
  12. #SMTP relay host name,一般是HUB服务器或者是某内部SMTP gateway
     
  13. $SMTPClient = New-Object Net.Mail.SmtpClient("ex2010cas01.mcmhost.com")
     

  14.  
  15. $mailbox = get-mailbox -id $mailboxname
     
  16. $strMsgTo = $mailbox.PrimarySMTPAddress
     

  17.  
  18. $strMsgBody = "您好, "+$mailbox.DisplayName+", 欢迎使用MCMhost邮件系统!
     

  19.  
  20. --------------------------------------
     
  21. 用户名和密码
     
  22. --------------------------------------
     
  23. 您的登录账户名是 is '"+$mailbox.SamAccountName+"'. Use your username and password to login to the network. Your password should NEVER be shared with anyone except the I.T. department, and only then when requested. Please do not write it down on anything that can be seen by your coworkers. You will be prompted to change it regularly.
     

  24.  
  25. --------------------------------------
     
  26. 邮箱
     
  27. --------------------------------------
     

  28.  
  29. 您的邮件地址是 '"+$mailbox.PrimarySMTPAddress+"'.
     

  30.  
  31. To access your email, calendar, contacts, and tasks from outside of the building, such as from home, you can do so from any Internet connected computer. Simply open Internet Explorer and go to the Outlook Web Access (OWA) page at https://mail.MCMhost.com/ and log in using your username and password. Please note the 's' in https.
     

  32.  
  33. If you'd like to have access to your email and contacts from your cell phone, you will need a cell phone that has Windows Mobile 5 or later, or an Apple iPhone. Blackberry phones are not supported. Instructions for configuring your device can be found in the Frequently Asked Questions (FAQ) section of the MCMhost Intranet at https://intranet.MCMhost.com/helpdesk/Lists/SupportFaq/AllItems.aspx
     
  34. --------------------------------------
     
  35. Contact information
     
  36. --------------------------------------
     
  37. Once you're situated, please go to http://directory/DirectoryUpdate and update your information. Log in using your username and password. It's important that you update your information anytime something changes, such as title, department, phone number, etc. This information is used in various systems and applications, and is your responsibility to keep up to date.
     

  38.  
  39. --------------------------------------
     
  40. Computer, Email, and Internet policies
     
  41. --------------------------------------
     
  42. MCMhost, Inc. provides a computer for your work tasks. The use of personally owned computers and related equipment is not permitted on our network. Additional information about use of MCMhost computers, email, Internet, etc. can be found in the Employee Handbook located in the HR section of the intranet at https://intranet.MCMhost.com/hr/
     

  43.  
  44. --------------------------------------
     
  45. Technical assistance
     
  46. --------------------------------------
     
  47. Should you need technical assistance, please check the Frequently Asked Questions (FAQ) section of the MCMhost Intranet at https://intranet.MCMhost.com/helpdesk/Lists/SupportFaq/AllItems.aspx. If you cannot find an answer there, submit a Service Request on the MCMhost intranet at https://intranet.MCMhost.com/helpdesk. If you are unable to access the intranet site, only then should you email HelpDesk@MCMhost.com. It is monitored by the whole IT department, and will ensure your issue is resolved in a timely manner.
     

  48.  
  49. Thank you, and, again, welcome to MCMhost!
     
  50. The Information Technology Department"
     

  51.  
  52. $SMTPClient.Send($strMsgFrom,$strMsgTo,$strMsgTitle,$strMsgBody)
复制代码

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



第三步, 去到C:\Program Files\Microsoft\Exchange Server\V14\Bin\CmdletExtensionAgents 目录下(如果不是缺省安装路径,可自行修改)。创建一个XML文件ScriptingAgentConfig.xml

XML文件内容如下,其作用是当完成new-mailbox这个命令后,自动调用c:\temp\send-mail.ps1脚本发送欢迎邮件,并且禁止邮箱的IMAP和POP3访问。这里的功用可无限引申出去(注意,在某些时候script agent的优先级需要比provisionagent的优先级高,比如对database做操作的命令,此处就不赘述了)。

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

  1. <?xml version="1.0" encoding="utf-8" ?>
     
  2. <Configuration version="1.0">
     
  3.                 <Feature Name="MailboxProvisioning" Cmdlets="new-mailbox">
     
  4.                                 <ApiCall Name="OnComplete">
     
  5.                                                 if($succeeded)    {
     
  6.                                                                 $newmailbox = $provisioningHandler.UserSpecifiedParameters["Name"]
     
  7.                                                                 c:\temp\send-mail.ps1 -mailboxname $newmailbox
     
  8.                                                                 Set-CASMailbox $newmailbox -IMAPEnabled $false -POPEnabled $false
     
  9.                                                 }
     
  10.                                 </ApiCall>
     
  11.                 </Feature>
     
  12. </Configuration>
复制代码

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



第四步,关闭所有服务器上目前打开的EMS和EMC。然后拷贝该XML去您环境中的每一个Exchange 服务器。别怪我没告诉你噢,等下跟我说你打不开EMC了。

第五部,使用下面命令创建一个新用户(也可用界面创建)。请自行修改 OU路径和用户名等参数。

  1. New-Mailbox -Name 'Exchange 2010 user08' -Alias 'exchange2010user08' -OrganizationalUnit 'MCMHost.com/MCM/Users' -UserPrincipalName 'user08@MCMHost.com' -SamAccountName 'user08' -FirstName 'Exchange 2010' -Initials '' -LastName 'user08' -Password 'System.Security.SecureString' -ResetPasswordOnNextLogon $false
复制代码
  1. New-Mailbox -Name 'Exchange 2010 user08' -Alias 'exchange2010user08' -OrganizationalUnit 'MCMHost.com/MCM/Users' -UserPrincipalName 'user08@MCMHost.com' -SamAccountName 'user08' -FirstName 'Exchange 2010' -Initials '' -LastName 'user08' -Password 'System.Security.SecureString' -ResetPasswordOnNextLogon $false
复制代码