First PS just pulls AD attributes, and group membership to determine which signatures you get, then runs the individual scripts for each template. I have only posted the first script and the default template script. Like I mentioned there is about 20 other templates. For each template we have a word doc with the field and the script finds and replaces and then saves the signatures. We make the sigs read only so that people cant change them.
Word doc templates look similar to this with company logo etc.
First script gets AD variables, checks for last time AD was updated etc, so that it only pushes new sig when needed etc.
#AD Variables
$UserName = $env:username
$Filter = "(&(objectCategory=User)(samAccountName=$UserName))"
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.Filter = $Filter
$ADUserPath = $Searcher.FindOne()
$ADUser = $ADUserPath.GetDirectoryEntry()
$ADLastMod = $ADUser.whenChanged -replace '[/,:]',''
$SignatureVersion = $ADLastMod
#Local Environment variables
$AppData=(Get-Item env:appdata).value
$SigPath = '\Microsoft\Signatures'
$LocalSignaturePath = $AppData+$SigPath
#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
$ADDisplayName = $ADUser.DisplayName
$ADTitle = $ADUser.title
$ADStreetAddress = $ADUser.streetaddress
$ADCity = $ADUser.l
$ADState = $ADUser.st
$ADZip = $ADUser.postalCode
$ADTelePhoneNumber = $ADUser.TelephoneNumber
$ADFaxNumber = $ADUser.facsimileTelephoneNumber
$ADEmailAddress = $ADUser.mail
$ADDescription = $ADUser.description
$ADCustomAttribute1 = $ADUser.extensionAttribute2
$Company = $ADUser.Company
$ADLicenseNumber = $ADUser.carLicense
$MangerDisplayName = ([adsi]"LDAP://$($ADUser.manager)").DisplayName
$MangerEmail = ([adsi]"LDAP://$($ADUser.manager)").mail
$MangerPhone = ([adsi]"LDAP://$($ADUser.manager)").TelephoneNumber
#Group Template variables
$grp_CS = "CN=grp-CS-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_IS = "CN=grp-IS-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_PD = "CN=grp-PD-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_UW = "CN=grp-UW-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_SI = "CN=grp-SI-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_SP = "CN=grp-SP-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_FN = "CN=grp-FN-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_IN = "CN=grp-IN-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_UM = "CN=grp-UM-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_CO = "CN=grp-CO-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_CU = "CN=grp-CC-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_DO = "CN=grp-DO-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
$grp_EO = "CN=grp-EO-Template,OU=Departments,OU=Groups,DC=CONTOSO,DC=LOCAL"
#Run Default Template for Everyone
\\CONTOSO.local\Templates\Default-Template.ps1
#Run CS Template Script
If ($ADUser.memberOf -contains $grp_CS){
\\CONTOSO.local\Templates\CS-Template.ps1
}
#Run IS Template
If ($ADUser.memberOf -contains $grp_IS){
\\CONTOSO.local\Templates\IN-Template.ps1
}
#Run PD Template
If ($ADUser.memberOf -contains $grp_PD){
\\CONTOSO.local\Templates\PD-Template.ps1
}
#Run UW Template
If ($ADUser.memberOf -contains $grp_UW){
\\CONTOSO.local\Templates\UW-Template.ps1
}
#Run SI Template
If ($ADUser.memberOf -contains $grp_SI){
\\CONTOSO.local\Templates\SI-Template.ps1
}
#Run SP Template
If ($ADUser.memberOf -contains $grp_SP){
\\CONTOSO.local\Templates\SP-Template.ps1
}
#Run FN Signatute Template
If ($ADUser.memberOf -contains $grp_FN){
\\CONTOSO.local\Templates\FN-Template.ps1
}
#Run IN Signatute Template
If ($ADUser.memberOf -contains $grp_IN){
\\CONTOSO.local\Templates\IN-Template.ps1
}
#Run UM Signatute Template
If ($ADUser.memberOf -contains $grp_UM){
\\CONTOSO.local\Tempates\UM-Template.ps1
}
#Run CO Signatute Template
If ($ADUser.memberOf -contains $grp_CO){
\\CONTOSO.local\Tempates\CO-Template.ps1
}
#Run CC Signatute Template
If ($ADUser.memberOf -contains $grp_CC){
\\CONTOSO.local\Tempates\CC-Template.ps1
}
#Run DO Signatute Template
If ($ADUser.memberOf -contains $grp_DO){
\\CONTOSO.local\Templates\DO-Template.ps1
}
#Run EO Signatute Template
If ($ADUser.memberOf -contains $grp_EO){
\\CONTOSO.local\Templates\EOI-Template.ps1
}
----
Example of follow on script that replaces everything in the word doc template and then saves the signatures and pushes to outlook sig folder etc.
#Set Signature Template to Default Template
$SigSource = "\\contoso.local\Templates\Default.docx"
$SignatureName = 'Contoso-Default'
$ForceSignature = '0' #Set to 1 if you don't want the users to be able to change signature in Outlook
#Copy signature templates from source to local Signature-folder
Write-Output "Copying Signatures"
$fullPath = $LocalSignaturePath+'\'+$SignatureName+'.docx'
Copy-Item "$Sigsource" $fullPath -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
$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 )
#Title
$FindText = "Title"
$ReplaceText = $ADTitle.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
#Defaults
$DefaultAddress = "1234 Some Street, Ste 100"
$DefaultCity = "Some City"
$DefaultState = "Some State"
$DefaultZip = "Some Zip"
$DefaultTelephone = "(800)555-1212"
$DefaultCompany = "Some Company, Inc"
#Company
If ($Company -ne '') {
$FindText = "Company"
$ReplaceText = $Company.ToString()
}
Else {
$FindText = "Company"
$ReplaceText = $DefaultCompany
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
#LicenseNumber
If ($ADLicenseNumber -ne "") {
$FindText = "LicenseNumber"
$ReplaceText = $ADLicenseNumber.ToString()
}
Else {
$FindText = "Lic # LicenseNumber"
$ReplaceText = " "
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
#Street Address
If ($ADStreetAddress -ne '') {
$FindText = "Address"
$ReplaceText = $ADStreetAddress.ToString()
}
Else {
$FindText = "Address"
$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 )
#State
If ($ADState -ne '') {
$FindText = "State"
$ReplaceText = $ADState.ToString()
}
Else {
$FindText = "State"
$ReplaceText = $DefaultState
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
#Zip
If ($ADZip -ne '') {
$FindText = "Zip"
$ReplaceText = $ADZip.ToString()
}
Else {
$FindText = "Zip"
$ReplaceText = $DefaultZip
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
#Telephone
If ($ADTelephoneNumber -ne "") {
$FindText = "PhoneNumber"
$ReplaceText = $ADTelephoneNumber.ToString()
}
Else {
$FindText = "PhoneNumber"
$ReplaceText = $DefaultTelephone
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
#FaxTelephone
If ($ADFaxNumber -ne "") {
$FindText = "FaxNumber"
$ReplaceText = $ADFaxNumber.ToString()
}
Else {
$FindText = "Fax: FaxNumber"
$ReplaceText = " "
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
#EmailAddress
If ($ADEmailAddress -ne "") {
$FindText = "emailaddress"
$ReplaceText = $ADEmailAddress.ToString()
}
Else {
$FindText = "Email:"
$ReplaceText = " "
}
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
#Save new message signature
Write-Output "Saving signatures"
#Save HTML
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatHTML");
$path = $LocalSignaturePath+'\'+$SignatureName+".htm"
Set-ItemProperty -Path $path -Name IsReadOnly -Value $false
$MSWord.ActiveDocument.saveas([ref]$path, [ref]$saveFormat)
Set-ItemProperty -Path $path -Name IsReadOnly -Value $true
#Save RTF
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatRTF");
$path = $LocalSignaturePath+'\'+$SignatureName+".rtf"
Set-ItemProperty -Path $path -Name IsReadOnly -Value $false
$MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat)
Set-ItemProperty -Path $path -Name IsReadOnly -Value $true
#Save TXT
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatText");
$path = $LocalSignaturePath+'\'+$SignatureName+".txt"
Set-ItemProperty -Path $path -Name IsReadOnly -Value $false
$MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$SaveFormat)
Set-ItemProperty -Path $path -Name IsReadOnly -Value $true
$MSWord.ActiveDocument.Close()
$MSWord.Quit()
#Office 2016 signature
If (Test-Path HKCU:Software\Microsoft\Office\16.0)
{
Write-Output "Setting signature for Office 2016"
If ($ForceSignature -eq '0')
{
Write-Output "Setting Office 2016 as available"
$MSWord = New-Object -ComObject word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
$MSWord.Quit()
}
If ($ForceSignature -eq '1')
{
Write-Output "Setting signature for Office 2016 as forced"
If (Get-ItemProperty -Name 'NewSignature' -Path HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings') { }
Else {
New-ItemProperty HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'NewSignature' -Value $SignatureName -PropertyType 'String' -Force
}
If (Get-ItemProperty -Name 'ReplySignature' -Path HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings') { }
Else {
New-ItemProperty HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'ReplySignature' -Value $SignatureName -PropertyType 'String' -Force
}
}
}
Like I mentioned - im sure there are third part products but has worked great. We did this as a login script but its getting to be a lot, so the plan is to move this over to a server to generate all signatures after hours etc, and then replace the login with just a file copy script to local PC to set outlook sigs. Im not a programmer - just write this to solve a problem.