Script to set Outlook signature using AD info, Security Groups and Templates

Script to set Outlook e-mail signature using Active Directory information, Security groups and unlimited templates.

Script Localization: https://gallery.technet.microsoft.com/office/Script-to-set-Outlook-da7b56ee 

PowerShell
<#
    .SYNOPSIS
    Script to set Outlook 2010/2013/2016 e-mail signature using Active Directory information, Security groups and unlimited templates
    .DESCRIPTION
    This script will set the Outlook 2010/2013/2016 e-mail signature on the local client using Active Directory information. 
    The template is created with a Word document, where images can be inserted and AD values can be provided.

    Author: João Dias
	Company: Mutega IT AB
	Email: jd@mutega.se
	Site: www.mutega.se
    Version 1.0
	
	Script it is a mix of Jan Egil Ring and Daniel Classon Script
	https://gallery.technet.microsoft.com/office/6f7eee4b-1f42-499e-ae59-1aceb26100de
	https://gallery.technet.microsoft.com/office/Set-Outlook-20102013-8341e049

    .DISCLAIMER
    All scripts and other powershell references are offered AS IS with no warranty.
    These script and functions are tested in my environment and it is recommended that you test these scripts in a test environment before using in your production environment.
    #>


#Custom variables
$SignatureName = 'OSignature' #insert the company name (no spaces) - could be signature name if more than one sig needed
$SigSource = $env:appdata+"MUTEGAOutlookSignatureTemplatesOSignature.docx" #Path to the *.docx file, i.e "c:temptemplate.docx"
$Source = $env:appdata+"MutegaOutlookSignatureIMG"
$SignatureVersion = "1.0" #Change this if you have updated the signature. If you do not change it, the script will quit after checking for the version already on the machine
$ForceSignature = '1' #Set to 1 if you don't want the users to be able to change signature in Outlook
 
#Environment variables
$AppData=(Get-Item env:appdata).value
$SigPath = 'MicrosoftSignatures'
$LocalSignaturePath = $AppData+$SigPath
$RemoteSignaturePathFull = $SigSource

<# #Copy version file
If (-not(Test-Path -Path $LocalSignaturePath$SignatureVersion))
{
New-Item -Path $LocalSignaturePath$SignatureVersion -ItemType Directory
}
Elseif (Test-Path -Path $LocalSignaturePath$SignatureVersion)
{
Write-Output "Latest signature already exists"
break
}  #>

#Check signature path (needs to be created if a signature has never been created for the profile
if (-not(Test-Path -path $LocalSignaturePath)) {
	New-Item $LocalSignaturePath -Type Directory
}

#Get Active Directory information for current user
$UserName = $env:username
$Filter = "(&(objectCategory=User)(samAccountName=$UserName))"
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.Filter = $Filter
$ADUserPath = $Searcher.FindOne()
$ADUser = $ADUserPath.GetDirectoryEntry()
$ADDisplayName = $ADUser.DisplayName
$ADEmailAddress = $ADUser.mail
$ADTitle = $ADUser.title
$ADDescription = $ADUser.description
$ADTelePhoneNumber = $ADUser.TelephoneNumber
$ADMobile = $ADUser.mobile
$ADStreetAddress = $ADUser.streetaddress
$ADwWWHomePage = $ADUser.wWWHomePage
$ADCity = $ADUser.l
<# $ADCustomAttribute1 = $ADUser.extensionAttribute1 #>
$ADModify = $ADUser.whenChanged

#Copy signature templates from source to local Signature-folder
Write-Output "Copying Signatures"
Copy-Item "$Sigsource" $LocalSignaturePath -Recurse -Force
$ReplaceAll = 2
$FindContinue = 1
$MatchCase = $False
$MatchWholeWord = $True
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $FindContinue
$Format = $False
	
#Insert variables from Active Directory to rtf signature-file
$MSWord = New-Object -ComObject word.application
$fullPath = $LocalSignaturePath+''+$SignatureName+'.docx'
$MSWord.Documents.Open($fullPath)

<# 	
#User Name $ Designation 
$FindText = "DisplayName"
$Designation = $ADCustomAttribute1.ToString()

#Designations in Exchange custom attribute 1
If ($Designation -ne '') { 
	$Name = $ADDisplayName.ToString()
	$ReplaceText = $Name+', '+$Designation
}
Else {
	$ReplaceText = $ADDisplayName.ToString() 
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,	$MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap,	$Format, $ReplaceText, $ReplaceAll	) 
#>

#DisplayName		
$FindText = "DisplayName"
$ReplaceText = $ADDisplayName.ToString() 
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,	$MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap,	$Format, $ReplaceText, $ReplaceAll	)
	
#Title		
$FindText = "Title"
$ReplaceText = $ADTitle.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,	$MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap,	$Format, $ReplaceText, $ReplaceAll	)
	
#Description
   If ($ADDescription -ne '') { 
   	$FindText = "Description"
   	$ReplaceText = $ADDescription.ToString()
}
Else {
	$FindText = " | Description "
   	$ReplaceText = "".ToString()
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,	$MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap,	$Format, $ReplaceText, $ReplaceAll	)
#$LogInfo += $NL+'Description: '+$ReplaceText
   	
#Street Address
If ($ADStreetAddress -ne '') { 
       $FindText = "StreetAddress"
    $ReplaceText = $ADStreetAddress.ToString()
   }
   Else {
    $FindText = "StreetAddress"
    $ReplaceText = $DefaultAddress
    }
	$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,	$MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap,	$Format, $ReplaceText, $ReplaceAll	)

#City
If ($ADCity -ne '') { 
    $FindText = "City"
       $ReplaceText = $ADCity.ToString()
   }
   Else {
    $FindText = "City"
    $ReplaceText = $DefaultCity 
   }
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,	$MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap,	$Format, $ReplaceText, $ReplaceAll	)
	
#Telephone
If ($ADTelephoneNumber -ne "") { 
	$FindText = "TelephoneNumber"
	$ReplaceText = $ADTelephoneNumber.ToString()
   }
Else {
	$FindText = "TelephoneNumber"
    $ReplaceText = $DefaultTelephone
	}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,	$MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap,	$Format, $ReplaceText, $ReplaceAll	)
	
#Mobile
If ($ADMobile -ne "") { 
	$FindText = "MobileNumber"
	$ReplaceText = $ADMobile.ToString()
   }
Else {
	$FindText = "| Mob MobileNumber "
    $ReplaceText = "".ToString()
	}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,	$MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap,	$Format, $ReplaceText, $ReplaceAll	)

#Email
$MSWord.Selection.Find.Execute(“EmailAddress”) 
$MSWord.ActiveDocument.Hyperlinks.Add($MSWord.Selection.Range, “mailto:”+$ADEmailAddress.ToString(), $missing, $missing, $ADEmailAddress.ToString()) 

#Linkedin
$selection = $MSword.selection
$selection.TypeParagraph()
$selection.TypeParagraph()
$selectimage = $selection.InlineShapes.AddPicture($Source+"linkedinbutton.jpg")
$msword.ActiveDocument.Hyperlinks.Add($selectimage.Range,$ADwWWHomePage.ToString())
$selection.TypeParagraph()

#Save new message signature 
Write-Output "Saving signatures"

#Fixes Enumeration Problems
$wdTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Word' -Passthru
$wdSaveFormat = $wdTypes | Where {$_.Name -eq "wdSaveFormat"}

#Save HTML
$path = $LocalSignaturePath+''+$SignatureName+".htm"
$MSWord.ActiveDocument.saveas([ref]$path, [ref]$wdSaveFormat::wdFormatHTML);
$MSWord.ActiveDocument.saveas($path, $wdSaveFormat::wdFormatHTML);
    
#Save RTF 
$path = $LocalSignaturePath+''+$SignatureName+".rtf"
$MSWord.ActiveDocument.SaveAs([ref]$path, [ref]$wdSaveFormat::wdFormatRTF);
$MSWord.ActiveDocument.SaveAs($path, $wdSaveFormat::wdFormatRTF);
	
#Save TXT    
$path = $LocalSignaturePath+''+$SignatureName+".txt"
$MSWord.ActiveDocument.SaveAs([ref]$path, [ref]$wdSaveFormat::wdFormatUnicodeText);
$MSWord.ActiveDocument.SaveAs($path, $wdSaveFormat::wdFormatUnicodeText);
	
$MSWord.ActiveDocument.Close();
$MSWord.Quit();

#Remove old regedit keys
If (Test-Path HKCU:'SoftwareMicrosoftOffice14.0')
{
Remove-ItemProperty HKCU:'SoftwareMicrosoftOffice14.0CommonMailSettings' -Name 'ReplySignature'
Remove-ItemProperty HKCU:'SoftwareMicrosoftOffice14.0CommonMailSettings' -Name 'NewSignature'
Remove-ItemProperty HKCU:'SoftwareMicrosoftOffice14.0OutlookSetup' -Name 'First-Run'
}
If (Test-Path HKCU:'SoftwareMicrosoftOffice15.0')
{
Remove-ItemProperty HKCU:'SoftwareMicrosoftOffice15.0CommonMailSettings' -Name 'ReplySignature'
Remove-ItemProperty HKCU:'SoftwareMicrosoftOffice15.0CommonMailSettings' -Name 'NewSignature'
Remove-ItemProperty HKCU:'SoftwareMicrosoftOffice15.0OutlookSetup' -Name 'First-Run'
}
If (Test-Path HKCU:'SoftwareMicrosoftOffice16.0')
{
Remove-ItemProperty HKCU:'SoftwareMicrosoftOffice16.0CommonMailSettings' -Name 'ReplySignature'
Remove-ItemProperty HKCU:'SoftwareMicrosoftOffice16.0CommonMailSettings' -Name 'NewSignature'
Remove-ItemProperty HKCU:'SoftwareMicrosoftOffice16.0OutlookSetup' -Name 'First-Run'
}

#Office 2010
If (Test-Path HKCU:'SoftwareMicrosoftOffice14.0')
{
If ($ForceSignature -eq '1')
    {
    Write-Output "Setting signature for Office 2010 as forced"
    New-ItemProperty HKCU:'SoftwareMicrosoftOffice14.0CommonMailSettings' -Name 'ReplySignature' -Value $SignatureName -PropertyType 'String' -Force
    New-ItemProperty HKCU:'SoftwareMicrosoftOffice14.0CommonMailSettings' -Name 'NewSignature' -Value $SignatureName -PropertyType 'String' -Force
    }
else
{
Write-Output "Setting Office 2010 signature as available"

$MSWord = New-Object -comobject word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries

}
}

#Office 2013
If (Test-Path HKCU:'SoftwareMicrosoftOffice15.0')
{
If ($ForceSignature -eq '1')
    {
    Write-Output "Setting signature for Office 2013 as forced"
    New-ItemProperty HKCU:'SoftwareMicrosoftOffice15.0CommonMailSettings' -Name 'ReplySignature' -Value $SignatureName -PropertyType 'String' -Force
    New-ItemProperty HKCU:'SoftwareMicrosoftOffice15.0CommonMailSettings' -Name 'NewSignature' -Value $SignatureName -PropertyType 'String' -Force
    }
else
{
Write-Output "Setting Office 2013 signature as available"

$MSWord = New-Object -comobject word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries

}
}


#Office 2016 signature

If (Test-Path HKCU:'SoftwareMicrosoftOffice16.0')

{
Write-Output "Setting signature for Office 2016"

If ($ForceSignature -eq '1')

{
Write-Output "Setting Office 2016 as available"

$MSWord = New-Object -ComObject word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries

}

If ($ForceSignature -eq '1')
{
Write-Output "Setting signature for Office 2016 as forced"
    If (Get-ItemProperty -Name 'NewSignature' -Path HKCU:'SoftwareMicrosoftOffice16.0CommonMailSettings') { } 
    Else { 
    New-ItemProperty HKCU:'SoftwareMicrosoftOffice16.0CommonMailSettings' -Name 'NewSignature' -Value $SignatureName -PropertyType 'String' -Force 
    } 
    If (Get-ItemProperty -Name 'ReplySignature' -Path HKCU:'SoftwareMicrosoftOffice16.0CommonMailSettings') { } 
    Else { 
    New-ItemProperty HKCU:'SoftwareMicrosoftOffice16.0CommonMailSettings' -Name 'ReplySignature' -Value $SignatureName -PropertyType 'String' -Force
    } 
}

}
taskkill /F /IM winword.exe

Prerequisites

User Groups

Name Description Type
OSIGADMIN Outlook Signature Admin Group Security Group – Global
MTSE001 User Outlook Signature Template in Swedish without Mobile Number Security Group – Global
MTSE002 User Outlook Signature Template in Swedish with the Mobile Number Security Group – Global

Nomeclature for Creation of User Groups

Example:

MTSE001

MT = MUTEGA IT 

SE = Swedish

001 = Template number 001

The security group and the template .docx must have the same name, as I should describe further on.

Active Directory Fields

The Outlook Signature gets the information from the Active Directory, so we need to fill up the following fields.

AD Fields

DisplayName

Title

telephoneNumber

mobileNumber

EmailAddress

wWWHomePage

Outlook Signature localization (Example)

Location of the script files: \mutega.localSysVolmutega.localscriptsOutlookSignature

Location of the logon file: \mutega.localSysVolmutega.localPolicies{xxxxx-xxxx-xxxxx-xxxxx}UserScriptsLogon

Location of the Outlook Signature of the Clients: %appdata%mutegaOutlooksignature

Final location of the Outlook Signature on the Clients: %appdata%MicrosoftSignatures

Outlook e-mail Signature GPO

This GPO is the one responsible to run Outlook Signature script at the Log On.

Name of the GPO: OutlookSignature

Fuction: Running the file Run_LO.cmd for the users that below to the Outlook Signature Security Group.

Outlook Signature Execution

The Outlook signature runs allow on the Logon, but the execution only occurs in the following conditions:

  1. The version of the Signature is not the same;
  2. The user is added to an Outlook Signature security group;
  3. The user is changed to a new Outlook Signature security group to another one;
  4. The Signature file under %appdata%MicrosoftSignatures is deleted manually.

Outlook Signature Desktop Icon

Now the user has the possibility to recreate the Outlook Signature.

  1. The shortcut is called ReCreate Outlook Signature, and if the end-user double clicks it, will recreate the Outlook Signature

 

Stängt för kommentarer.