Setting Up a Dynamic Outlook signature for users pulling information from Active Directory free

By | July 10, 2013

Signature Setup Source Files

Many companies provide software to add signatures to emails you send out and even Exchange has the ability to text to the bottom of users emails.  With the help of several forums I’ve made the following script.  What it does is creates a Outlook signature file and sets it as default on users emails.  All information for the email signature is pulled from Active Directory which makes it easy to manage and ensures a constant corporate image for all your outgoing emails.  This obviously will not add the signature to users sending via OWA or other email clients only outlook.

Outlook stores the setting for email signatures in

"HKCUSoftwareMicrosoftOfficex.0CommonMailSettingsReplySignature"

where x.x is the version number (14.0 etc). The signature file is stored in the users appdata folder

"%appdata%\Microsoft\Signatures"

Outlook also uses the following registry key;

HKCUSoftwareMicrosoftOffice14.0OutlookSetupFirst-Run

If this has a value outlook will not automatically set the signature, but if we blank it the signature will be set and uses can possibly edit if you allow it.

Knowing all this we need to create a html file containing the signature we want and then set the registry key to the .html file, blank the first-run key and bingo users have the company approved signature.

Lets start creating the script dynamicsignaturesetup.vbs

Const adPersistXML = 1
Const ForWriting = 2
Const REGSIGKEY = "HKCUSoftwareMicrosoftOffice14.0OutlookSetupFirst-Run"
Const REGSIGKEY1 = "HKCUSoftwareMicrosoftOffice11.0CommonMailSettingsNewSignature"
Const REGSIGKEY2 = "HKCUSoftwareMicrosoftOffice11.0CommonMailSettingsReplySignature"
Const REGSIGKEY3 = "HKCUSoftwareMicrosoftOffice12.0CommonMailSettingsNewSignature"
Const REGSIGKEY4 = "HKCUSoftwareMicrosoftOffice12.0CommonMailSettingsReplySignature"
Const REGSIGKEY5 = "HKCUSoftwareMicrosoftOffice14.0CommonMailSettingsNewSignature"
Const REGSIGKEY6 = "HKCUSoftwareMicrosoftOffice14.0CommonMailSettingsReplySignature"

Dim XML,XSL, FSO
Dim WshShell, WshNet, rsAD, Com, ConAD, f, homepath, userprofile

we have now set the various key locations for office 2003, 2007 and 2010 as well as some variables we will use later in the script.

Now lets get  the username and various folder application paths we need

'Get username
Set WshNet = WScript.CreateObject("WScript.Network")
sUsername = WshNet.UserName

'Get userprofile path
Set WshShell = CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PROCESS")
homepath = WshSysEnv("HOMEPATH")
userprofile = WshSysEnv("appdata")

OK now lets setup the folders where the signature will be stored

' ***********************************************************
' Create Folder structure
' if required
' ***********************************************************
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFilePath = userprofile
strFileDelete = strFilePath
If objFSO.FolderExists(strFileDelete) then
  'WSCript.Echo "Exists " & strFileDelete
Else
  'WSCript.Echo "Creating " & strFileDelete
 objFSO.CreateFolder(strFileDelete)
end if

strFilePath = userprofile
strFileDelete = strFilePath & "Microsoft"
If objFSO.FolderExists(strFileDelete) then
  'WSCript.Echo "Exists " & strFileDelete
Else
  'WSCript.Echo "Creating " & strFileDelete
 objFSO.CreateFolder(strFileDelete)
end if

strFilePath = userprofile & "Microsoft"
strFileDelete = strFilePath & "Signatures"
If objFSO.FolderExists(strFileDelete) then
  'WSCript.Echo "Exists " & strFileDelete
Else
  'WSCript.Echo "Creating " & strFileDelete
 objFSO.CreateFolder(strFileDelete)
end if

The next part we will connect to Active Directory through the vbs script

'***********************************************************
'Getdomain Name
'***********************************************************
sPrefix = "LDAP://"
Set cont = GetObject(sPrefix & "rootdse")
sDN = cont.get("defaultnamingcontext")

'Open ADO connection to AD
Set conAD = WScript.CreateObject( "ADODB.Connection" )
Set com = WScript.CreateObject( "ADODB.Command" )

'set the provider
conAD.Provider = "ADSDSOObject"

' Open a connection object
conAD.Open "Active Directory Provider"
Set Com.ActiveConnection = conAD

Now lets get the users accountname from AD and a list of attributes(properties) for the user including telephone numbers, email etc from AD

'Find username
sFilter = "(samaccountname=" & sUsername & ")"
'Select attributes
sAttributes = "givenname,sn,telephonenumber,mail,Streetaddress,l,postalcode,co,mobile,company,facsimileTelephoneNumber,title,department,wwwHomepage"

OK we are now ready to pull the data

build the command string
Com.CommandText = "<" & sPrefix & sDN & ">;" & sFilter & ";" & sAttributes

' Set some preferences for search
Com.Properties( "Page Size" ) = 512
Com.Properties( "TimeOut" ) = 30 ' seconds

'Execute the query to get objects from Active Directory.
Set rsAD = CreateObject("ADODB.Recordset")
Set rsAD = Com.Execute

If (Err.Number <> 0) Then
    WScript.Echo Err.Number, "on Execute"
End If

OK we now need to use a XSL/XML file we have created to generate the HTML file. This XSL file is the design of the signature I detail information on creating this file later in the post. For now assume this file exists.

Set xml = CreateObject("Microsoft.XMLDOM")
rsAD.Save xml, adPersistXML
XML.async = False
Set xsl = CreateObject("Microsoft.XMLDOM")
XSL.async = False
'Load the XSL file. Keep it in the same path as the script, but it could be stored anyway. webserver
XSL.load "\pathtosignaturefileSignature.xsl"
XSL.preserveWhiteSpace = True

OK the script has everything it needs to create the users signature file lets set it up

'Save the signature file to disk to the profile
Set WshShell = CreateObject("Wscript.Shell")
sUserProfile = WshShell.ExpandEnvironmentStrings("%appdata%")
sSignaturePath= sUserProfile & "MicrosoftSignatures"
sOutputfile = sSignaturePath & "AD_" & sUsername & ".htm"
Set FSO = CreateObject("Scripting.Filesystemobject")
Set f = fso.OpenTextFile(sOutputFile,ForWriting,True)
f.Write xml.transformNode(xsl)

Notice the last line converts the XSL file and saves it as AD_username.htm in %appdata%MicrosoftSignatures.
OK now lets set the registry up

On Error Resume Next
sRegKey = "" & WshShell.RegRead(REGSIGKEY)
WshShell.RegWrite REGSIGKEY, ""
WshShell.RegWrite REGSIGKEY5, "AD_" & sUsername, "REG_SZ"
WshShell.RegWrite REGSIGKEY6, "AD_" & sUsername, "REG_SZ"
On Error GoTo 0

Use the REGSIGKEY# that relates to your version of Office, you could easily amend this to allow a different reply signature too.
OK thats all the is to it set this script as a user logon script and everyone will get the signature on there outlook.
The XSL file is a simple XML file you can use if and otherwise statement to add infomation in case users don’t have a phone number for example. Use tags to set different fonts etc. Below is an example Signature.xsl.

<?xml version=’1.0′ encoding=’utf-8′?>

<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:rs=”urn:schemas-microsoft-com:rowset” xmlns:z=”#RowsetSchema” exclude-result-prefixes=”rs z”>
<xsl:output
method = “html”
doctype-public = “-//W3C//DTD HTML 4.01 Transitional//EN”
encoding = “UTF-8”
indent = “yes”/>

 

<xsl:template match=”/”>
<html>
<head></head>
<body>
<p>
<span style=”font-size: 11pt; font-family: ‘Trebuchet MS’, Arial, Helvetica, sans-serif; font-weight:bold”>
<xsl:value-of select=”xml/rs:data/z:row/@givenname”/><![CDATA[ ]]>
<xsl:value-of select=”xml/rs:data/z:row/@sn”/>
</span>
<span style=”font-size: 10pt; font-family: ‘Trebuchet MS’, Arial, Helvetica, sans-serif”>
<br/>
<xsl:if test=”xml/rs:data/z:row/@title”>
<xsl:value-of select=”xml/rs:data/z:row/@title”/>
<br/>
</xsl:if>
<xsl:choose>
<xsl:when test=”xml/rs:data/z:row/@telephonenumber”>
Phone: <xsl:value-of select=”xml/rs:data/z:row/@telephonenumber”/>
<br/>
<xsl:if test=”/xml/rs:data/z:row/@facsimileTelephoneNumber”>
Fax: <xsl:value-of select=”/xml/rs:data/z:row/@facsimileTelephoneNumber”/>
<br/>
</xsl:if>
</xsl:when>
<xsl:otherwise>
Phone: ###############
<br/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test=”xml/rs:data/z:row/@mobile”>
Mobile: <xsl:value-of select=”/xml/rs:data/z:row/@mobile”/>
<br/>
</xsl:if>
<xsl:if test=”xml/rs:data/z:row/@mail”>
Email: <a href=”mailto:{xml/rs:data/z:row/@mail}”><xsl:value-of select=”xml/rs:data/z:row/@mail”/></a>
<br/>
</xsl:if>
</span>
<a href=”http://www.##########.com”><img src=”file:///locationtofile/logo.gif” alt=”company name”/></a>
<hr />
<table width=”1000px” border=”0″>
<tr>
<td width=”100px” rowspan=”2″><img src=”file:///filelocation/logo2.gif” width=”96″ height=”91″ /></td>
<td width=”600px”><span style=”font-size: 11pt; font-family: ‘Trebuchet MS’, Arial, Helvetica, sans-serif; font-weight:normal; color: #F00;”> Some tag line </span></td>
<td width=”300px” rowspan=”2″><p style=”font-size: 10pt; font-family: ‘Trebuchet MS’, Arial, Helvetica, sans-serif; font-weight:normal; color: #F00; text-align: right;”>CITY POSTCODE<br />
t <span style=”color: #000;”>01904 623261 <span style=”color: #F00;”>f </span>#### #######</span><br />
<span style=”color: #F00;”>e </span> <span style=”color: #000;”>office@###########.com</span><br />
<span style=”color: #F00;”>w </span> <span style=”color: #000;”>www.##############.com</span> </p></td>
</tr>
<tr>
<td><p> <span style=”font-size: 10pt; font-family: ‘Trebuchet MS’, Arial, Helvetica, sans-serif; font-weight:normal; color: #000;”> Some company info or disclaimer </span> </p></td>
</tr>
</table>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

9 thoughts on “Setting Up a Dynamic Outlook signature for users pulling information from Active Directory free

  1. SEO

    Looking forward to reading more. Great article post.Really thank you! Cool.

    Reply
  2. Citeyne

    Had to fix some issues with ; instead of & i had to go and change all of the ; to &. now i have all the file i need i used your XSL file sample to do what i needed, however when i run the script it does not add anything to the signature it simply creates a blank signature.

    Reply
    1. Chris Allanson Post author

      Hi, I’ll publish the files you need rather than copying and pasting seems to cause problems for some

      Reply
  3. Daniel Beaulieu

    This is one awesome blog.Much thanks again. Much obliged.

    Reply
  4. SEO

    Really appreciate you discussing this article post.Much thanks again. Really Cool.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *