Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@andreagx
Last active February 12, 2017 20:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreagx/9067652 to your computer and use it in GitHub Desktop.
Save andreagx/9067652 to your computer and use it in GitHub Desktop.
###########################################################################”
# AUTHOR: Jan Egil Ring
# Modifications by Darren Kattan
#
# COMMENT: Script to create an Outlook signature based on user information from Active Directory.
# Adjust the variables in the “Custom variables”-section
# Create an Outlook-signature from Microsoft Word (logo, fonts etc) and copy this signature to \\domain\NETLOGON\sig_files\$CompanyName\$CompanyName.docx
# This script supports the following keywords:
# DisplayName
# Title
# Email
#
# See the following blog-posts for more information:
# http://blog.powershell.no/2010/01/09/outlook-signature-based-on-user-information-from-active-directory
# http://www.immense.net/deploying-unified-email-signature-template-outlook
#
# Tested on Office 2003, 2007 and 2010
#
# You have a royalty-free right to use, modify, reproduce, and
# distribute this script file in any way you find useful, provided that
# you agree that the creator, owner above has no warranty, obligations,
# or liability for such use.
#
# VERSION HISTORY:
# 1.0 09.01.2010 – Initial release
# 1.1 11.09.2010 – Modified by Darren Kattan
# 1.2 11.09.2012 - Modified by Andrea Gallazzi
# - Removed bookmarks. Now uses simple find and replace for DisplayName, Title, and Email.
# - Email address is generated as a link
# - Signature is generated from a single .docx file
# - Removed version numbers for script to run. Script runs at boot up when it sees a change in the “Date Modified” property of your signature template.
# ************* 1.2 **************
# - Fix signature txt format
# - Add Variables TelePhoneNumber,description, facsimileTelephoneNumber, mobile, department
# - Add support for OWA, the script now generate the html file by username and copy it on Exchange Server shared Directory for succeeding import (line 64)
# - Changed "wdFormatHTML" with "wdFormatFilteredHTML" this last command save only HTML whitout Office TAGs. This is important because Exchange Server support only 8k for OWA Signs.
#
# On Exchange Server share a folder with name "Signatures" and set the write permissions to "Authenticated Users"
###########################################################################”
###########################################################################”
#
# COMMENT: Script to create an Outlook signature based on user information from Active Directory.
# Adjust the variables in the “Custom variables”-section
# Create an Outlook-signature from Microsoft Word (logo, fonts etc) and copy this signature to \\domain\NETLOGON\sig_files\$CompanyName\$CompanyName.docx
# This script supports the following keywords:
# DisplayName
# Title
# Email
#
# VERSION HISTORY:
# 1.0 – Initial release
#
#
#
###########################################################################”
#Custom variables
$CompanyName='company'
$DomainName='domain.local'
#test path and crete folder
$TARGETDIR = $env:APPDATA + "\Microsoft\Signatures"
if(!(Test-Path -Path $TARGETDIR )){
New-Item -ItemType directory -Path $TARGETDIR
}
$SigSource ="\\$DomainName\netlogon\sig_files\$CompanyName"
$ForceSignatureNew=$true #When the signature are forced the signature are enforced as default signature for new messages the next time the script runs. 0 = no force, 1 = force
$ForceSignatureReplyForward=$true #When the signature are forced the signature are enforced as default signature for reply/forward messages the next time the script runs. 0 = no force, 1 = force
#Environment variables
$AppData=$env:appdata
$SigPath='\Microsoft\Signatures'
$LocalSignaturePath=$AppData+$SigPath
$RemoteSignaturePathFull="$SigSource\$CompanyName.docx"
$ExchPath='\\exchangeserver\Signatures\'
#Get Active Directory information for current user
$UserName = $env:username
$Searcher=[adsisearcher]"(&(objectCategory=User)(samAccountName=$UserName))"
$ADUserPath=$Searcher.FindOne()
$ADUser=$ADUserPath.GetDirectoryEntry()
$ADDisplayName = $ADUser.DisplayName
$ADEmailAddress = $ADUser.mail
$ADTitle = $ADUser.title
$ADTelePhoneNumber = $ADUser.telephoneNumber
$ADdescription = $ADuser.description
$ADFax = $ADuser.facsimileTelephoneNumber
$ADMobile = $ADuser.mobile
$ADDepartment = $ADuser.department
#Setting registry information for the current user
$CompanyRegPath="HKCU:\Software\$CompanyName"
if(-not(Test-Path $CompanyRegPath)){
New-Item -path “HKCU:\Software” -name $CompanyName
}
if(-not(Test-Path "$CompanyRegPath\Outlook Signature Settings")){
New-Item -path $CompanyRegPath -name 'Outlook Signature Settings'
}
#$SigVersion=(gci $RemoteSignaturePathFull).LastWriteTime #When was the last time the signature was written
$ForcedSignatureNew=(Get-ItemProperty "$CompanyRegPath\Outlook Signature Settings").ForcedSignatureNew
$ForcedSignatureReplyForward=(Get-ItemProperty "$CompanyRegPath\Outlook Signature Settings").ForcedSignatureReplyForward
$SignatureVersion=(Get-ItemProperty "$CompanyRegPath\Outlook Signature Settings").SignatureVersion
Set-ItemProperty "$CompanyRegPath\Outlook Signature Settings" -name SignatureSourceFiles -Value $SigSource
$SignatureSourceFiles=(Get-ItemProperty "$CompanyRegPath\Outlook Signature Settings").SignatureSourceFiles
# create this once to make release easier
$MSWord = New-Object -com word.application
#Forcing signature for new messages if enabled
if ($ForcedSignatureNew){
#Set company signature as default for New messages
$MSWord.EmailOptions.EmailSignature.NewMessageSignature=$CompanyName
}
#Forcing signature for reply/forward messages if enabled
if ($ForcedSignatureReplyForward){
#Set company signature as default for Reply/Forward messages
$MSWord.EmailOptions.EmailSignature.ReplyMessageSignature=$CompanyName
}
#Copying signature sourcefiles and creating signature if signature-version are different from local version
if ($SignatureVersion -ne $SigVersion){
#Copy signature templates from domain to local Signature-folder
Copy-Item “$SignatureSourceFiles\*” $LocalSignaturePath -Recurse -Force
$ReplaceAll = 2
$FindContinue = 1
$MatchCase = $False
$MatchWholeWord = $True
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $FindContinue
$Format = $False
#assembly load
Add-Type -Assembly Microsoft.Office.Interop.Word
# [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Word.WdsaveFormat")
#Insert variables from Active Directory to rtf signature-file
$fullPath="$LocalSignaturePath\$CompanyName.docx"
$MSWord.Documents.Open($fullPath)
$FindText = “DisplayName”
$ReplaceText = $ADDisplayName.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
$FindText = “Title”
$ReplaceText = $ADTitle.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
# $MSWord.Selection.Find.Execute(“PostaEL”)
$FindText = “PostaEL”
$ReplaceText = $ADEmailAddress.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
$FindText = “TelePhoneNumber”
$ReplaceText = $ADTelePhoneNumber.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
$FindText = “facsimileTelephoneNumber”
$ReplaceText = $ADFax.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
$FindText = “BlogIQ”
$ReplaceText = $ADDepartment.ToString()
$MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
$MSWord.ActiveDocument.Save()
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “wdFormatFilteredHTML”);
#$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “wdFormatFilteredHTML”);
[ref]$BrowserLevel = “microsoft.office.interop.word.WdBrowserLevel” -as [type]
$MSWord.ActiveDocument.WebOptions.OrganizeInFolder = $true
$MSWord.ActiveDocument.WebOptions.UseLongFileNames = $true
$MSWord.ActiveDocument.WebOptions.BrowserLevel = $BrowserLevel::wdBrowserLevelMicrosoftInternetExplorer6
$path = $LocalSignaturePath+’\'+$CompanyName+”.htm”
$MSWord.ActiveDocument.saveas($path, $saveFormat)
$MSWord.ActiveDocument.saveas([ref]$path, [ref]$saveFormat)
# Save html file with username from AD (OWA)
$pathOWA = $LocalSignaturePath+’\'+$UserName+”.htm”
$MSWord.ActiveDocument.saveas($pathOWA, $saveFormat)
Copy-Item “$LocalSignaturePath\$UserName.htm” "$ExchPath" -Recurse -Force
# Saving rtf and txt
$path = "$LocalSignaturePath\$CompanyName.rtf"
$MSWord.ActiveDocument.SaveAs($path, [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatRTF)
$MSWord.ActiveDocument.SaveAs([ref]$path, [ref][Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatRTF)
$path = "$LocalSignaturePath\$CompanyName.rtf"
$MSWord.ActiveDocument.SaveAs($path, [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatText)
$MSWord.ActiveDocument.SaveAs([ref]$path, [ref][Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatText)
$path = "$LocalSignaturePath\$CompanyName.txt"
$MSWord.ActiveDocument.SaveAs($path, [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatText)
$MSWord.ActiveDocument.SaveAs([ref] $path, [ref][Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatText)
}
$MSWord.ActiveDocument.Close()
$MSWord.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($MSWord)
Remove-Variable MSWord
#Stamp registry-values for Outlook Signature Settings if they doesn`t match the initial script variables. Note that these will apply after the second script run when changes are made in the “Custom variables”-section.
if ($ForcedSignatureNew -eq $ForceSignatureNew){
Set-ItemProperty $CompanyRegPath’\Outlook Signature Settings’ -name ForcedSignatureNew -Value $ForceSignatureNew
}
if ($ForcedSignatureReplyForward -ne $ForceSignatureReplyForward){
Set-ItemProperty "$CompanyRegPath\Outlook Signature Settings" -name ForcedSignatureReplyForward -Value $ForceSignatureReplyForward
}
if($SignatureVersion -ne $SigVersion){
Set-ItemProperty "$CompanyRegPath\Outlook Signature Settings" -name SignatureVersion -Value $SigVersion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment