Bài viết hướng dẫn tự động tạo chữ ký người dùng trong Outlook 2010/2013 dựa trên dữ liệu từ Active Directory. Senario: lần đầu người dùng domain đăng nhập máy trạm, script PowerShell tự động tạo file chữ ký Outlook với thông tin liên hệ lấy từ AD.
Để script hoạt động đúng, tất cả người dùng trong AD phải điền đầy đủ các thuộc tính bắt buộc. Ví dụ sử dụng các thuộc tính AD sau cho chữ ký:
- Tên đầy đủ (lưu trong thuộc tính Description)
- Chức danh (Title)
- Tên công ty (Company)
- Mã bưu chính, thành phố, địa chỉ (PostalCode, City, StreetAddress)
- Số điện thoại (OfficePhone)
- Email (Mail)
- Trang web (Homepage)
Cần tạo 3 file mẫu chữ ký cho Outlook định dạng htm (HTML), rtf (Rich Text) và txt (Plain Text). Thiết kế, nội dung và giao diện phải tuân thủ chính sách chữ ký email doanh nghiệp.
Tạo file signature.htm với mã HTML sau:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head></head>
<body>
<div id style="font-family:Arial&;color:#5B9BD5;">
<span style="font-size:10pt;color:#000000;">
<b><p>Best Regards</p>
<p>@NAME</p> </span>
<span style="font-size:9.0pt;">
<p> </p>
<p>@DESCRIPTION</p>
<p>@COMPANY</p></b>
<p> </p></span>
<span style="font-size:8.0pt;">
<p> @POSTALCODE, @CITY, @STREETADDRESS</p>
<p> Phone.@OFFICEPHONE</p>
<p> <a href="https://@WEBSITE">@WEBSITE</a></p>
<p>e-mail:<a href="mailto:@EMAIL">@EMAIL</a></span>
</div>
</body>
</html>
Nội dung file signature.rtf và signature.txt:
Best Regards,
@NAME
@DESCRIPTION
@COMPANY
@POSTALCODE, @CITY, @STREETADDRESS
Phone. @OFFICEPHONE
e-mail:@EMAIL
site:@WEBSITE
Tạo thư mục OutlookSignature trong C:\Users\Public\Downloads để lưu mẫu chữ ký và chữ ký người dùng. Trong đó tạo thư mục con Templates và copy 3 file mẫu vào. Có thể làm thủ công hoặc dùng Group Policy Preferences (GPP).
Tạo file outlooksignature.ps1 với mã PowerShell sau (có mô tả từng khối):
Xác định tập biến. Biến $User chứa tên người dùng chạy script. Chỉ định tên, phần mở rộng file và đường dẫn.
$User = $env:UserName
$FileName = "signature"
$FileExtension = "htm","rtf","txt"
$Path = "C:\Users\Public\Downloads"
$PathSignature = "$Path\OutlookSignature"
$PathSignatureTemplates = "$Path\OutlookSignature\Templates"
$PathSignatureUser = "$PathSignature\$User"
$AppSignatures =$env:APPDATA + "\Microsoft\Signatures"
Import module PowerShell để truy cập AD. Dùng Get-ADUser lấy giá trị thuộc tính người dùng AD cần thiết lưu vào đối tượng $AD_user.
Lưu ý. Để Get-ADUser hoạt động trên Windows 7, cần cài RSAT và bật thành phần Active Directory Module For Windows PowerShell (Control Panel → Programs and Features → Turn On/Off Windows Features → Remote Server Administration Tools → Role Administration Tools → AD DS And AD LDS Tools).
Import-module activedirectory
$AD_user = Get-ADUser $User -Properties Title,Company,Description,Fax,HomePage,Mail,OfficePhone,PostalCode,City,StreetAddress
Tạo thư mục lưu file chữ ký người dùng và copy mẫu vào:
New-Item -Path "$PathSignature\$User" -ItemType Container –Force
foreach ($Ext in $FileExtension)
{
Copy-Item -Force "$PathSignatureTemplates\$FileName.$Ext" "$PathSignatureUser\$FileName.$Ext"
}
Thay thế dữ liệu trong mẫu bằng dữ liệu người dùng từ AD:
foreach ($Ext in $FileExtension)
{
(Get-Content "$PathSignatureUser\$FileName.$Ext") | Foreach-Object {
$_`
-replace "@NAME", $AD_user.Description `
-replace "@DESCRIPTION", $AD_user.title `
-replace "@COMPANY", $AD_user.Company `
-replace "@STREETADDRESS", $AD_user.StreetAddress `
-replace "@POSTALCODE", $AD_user.PostalCode `
-replace "@CITY", $AD_user.City `
-replace "@OFFICEPHONE", $AD_user.OfficePhone `
-replace "@EMAIL", $AD_user.Mail `
-replace "@WEBSITE", $AD_user.Homepage `
} | Set-Content "$PathSignatureUser\$FileName.$Ext"
}
Copy file chữ ký vào thư mục Outlook lưu chữ ký — %APPDATA%\Microsoft\Signatures (C:\Users\username\AppData\Roaming\Microsoft\Signatures).
foreach ($Ext in $FileExtension)
{
Copy-Item -Force "$PathSignatureUser\$FileName.$Ext" "$AppSignatures\$User.$Ext"
write-host "$PathSignatureUser\$FileName.$Ext"
write-host "$AppSignatures\$User.$Ext"
}
Để Outlook dùng file chữ ký này khi khởi động:
- Xóa giá trị First-Run từ HKEY_CURRENT_USER\Software\Microsoft\Office\<Office_Version>\Outlook\Setup
- Trong HKEY_CURRENT_USER\Software\Microsoft\Office\<Office version>\Common\MailSettings tạo hai giá trị chuỗi NewSignature và ReplySignature chứa tên mẫu và chữ ký (trong ví dụ tên mẫu trùng tên tài khoản AD)
Để hoạt động với các phiên bản MS Office khác nhau, thêm mã:
#Office 2010
If (Test-Path HKCU:'\Software\Microsoft\Office\14.0') {
Remove-ItemProperty -Path HKCU:\Software\Microsoft\Office\14.0\Outlook\Setup -Name First-Run -Force -ErrorAction SilentlyContinue -Verbose
New-ItemProperty HKCU:'\Software\Microsoft\Office\14.0\Common\MailSettings' -Name 'ReplySignature' -Value $User -PropertyType 'String' -Force
New-ItemProperty HKCU:'\Software\Microsoft\Office\14.0\Common\MailSettings' -Name 'NewSignature' -Value $User -PropertyType 'String' -Force
}
#Office 2013
If (Test-Path HKCU:'\Software\Microsoft\Office\15.0') {
Remove-ItemProperty -Path HKCU:\Software\Microsoft\Office\15.0\Outlook\Setup -Name First-Run -Force -ErrorAction SilentlyContinue -Verbose
New-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'ReplySignature' -Value $User -PropertyType 'String' -Force
New-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'NewSignature' -Value $User -PropertyType 'String' -Force
}
Cấu hình script PowerShell chạy một lần qua Group Policy Preferences khi người dùng đăng nhập. Kết quả: Outlook sẽ dùng chữ ký vừa tạo cho email đi. Hình ảnh đầu bài viết minh họa chữ ký mẫu.
Một số mẹo
- Nếu Outlook hiển thị chữ ký HTM với khoảng cách dòng lớn (kép), đó là lỗi Outlook. Nên tạo chữ ký HTM ngay trong Outlook và dùng làm mẫu (lưu trong %APPDATA%\Microsoft\Signatures)
- Có thể thêm ảnh người dùng từ thuộc tính thumbnailPhoto của AD vào chữ ký. Vì không có cách đơn giản thêm ảnh vào chữ ký Outlook, tốt nhất tạo mẫu chữ ký chứa ảnh trong Outlook, sau đó trong script PowerShell copy đè file ảnh trong thư mục mẫu (ảnh lưu trong %AppData%\Microsoft\Signatures\<tên chữ ký>.files)
- Trên Exchange 2007 trở lên, có thể chèn chữ ký đơn giản vào tất cả email bằng Transport Rules