exchange 2010新建用戶自動觸發功能

進入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 <[email protected]>"
     

  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 [email protected]. 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 <[email protected]>"
     

  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 [email protected]. 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 '[email protected]' -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 '[email protected]' -SamAccountName 'user08' -FirstName 'Exchange 2010' -Initials '' -LastName 'user08' -Password 'System.Security.SecureString' -ResetPasswordOnNextLogon $false
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章