The Outlook signature script you will find below is a bit more complicated then most other scripts I post, cause you might need to adjust a bit more. I used it for several years (as you can see in the script when it comes to Outlook versions and registry keys) in many networks and in most cases it worked just flawless once it was set up.
What does this script do exactly?
Good question – it actually writes every time a user logs on a signature file to his profile. The information in the file are pulled from Active Directory – where you are able to e.g. change the phone number, cell phone number or e.g. last name because the employee married. The signature file will automatically update. Even more important is the onboarding process, you actually can forget about setting up the signature. Assuming you don’t use roaming profiles, well – no worries – the signature will auto create everywhere, if you call it via a login script / logon script. In theory you could call it via a GPO as well.
What you need to do – simply said
- get an approved example signature from HR or marketing or who ever can provide you the signature and actually put it in your Outlook as signature.
- then replace names, phone numbers with variables (I come to that) and save it in your Outlook.
- go to your %appdata%\Microsoft\Signatures folder and grab the three files (.txt / .htm / .rtf) and the sub-folder with the name of the signature you saved
- copy them to your \\mydomain\netlogon\signatures folder (you might need to create it – any other location would need some adjustment in the script)
- you will need to open the all three file formats (.txt / .htm / .rtf) in a regular text editor – plain text editor like NOTEPAD.EXE (Windows) or Notepad ++
- make sure the variables are a complete word and not somehow divided or have characters replaced – if something is not how it should be, adjust it and save the files
- Copy the OutlookSignatures.vbs file to the same path and adjust it especially in the header-section with your domain information and execute the script in a CMD / command prompt via \\mydomain\netlogon\signatures\OutlookSignatures.vbs “my signature” 1 1
- Now go back to your Outlook (probably close and re-open) and create a new email – you should see your signature was auto-generated and the variables have been replaced with you user-specific values from Active Directory / LDAP.
- you should switch your email format to all three formats – HTML / Plain Text / RTF and check the signature in all three formats – to make sure all three files where generated correctly
- If something is not as expected, check the source-signature files and their variables and if needed adjust the variable-replacement section of the script
What you gain from this
The signatures will auto-generate and you actually have a cheap way to roll out corporate identity conform signatures, without spending a lot of money for tools that might provide you an easier to use configuration and some more fancy features – but if you don’t need those features and you can live with a more technical way to approach this you actually have a cheap way to implement this.
The variables
The script will pull certain properties / attributes from the currently logged on user object from Active Directory – those are configured in line 33 and if you need more you will need to add them here.
Between the lines 145 and 181 you see that the script is replacing place-holder variables in the source files (.TXT / .HTM / .RTF) with the information pulled from Active Directory – all those place holders in your source files need to be @@AnyName@@ – this is to make sure you have a unique definition of what the script will replace.
Example:
1 | strCurrentLine = Replace(strCurrentLine, "@@GivenName@@", NZ(objrecordset.Fields("givenName"), "")) |
This does nothing else then:
- replace the variable strCurrentLine with
- search in variable strCurrentLine
- for the value @@GivenName@@
- replace it with LDAP attribute “givenName“
There are a few special examples for putting e.g. a HTML conform line-break after Job-Title in the .HTM file only in the script (I had situations where only the HTML signature did not do a Line-Break, or the Text-Version alone was not doing something, etc..) – in the end this allows you to adjust something in a one of the three signature formats.
Another example in the script writes an additional line with the cell phone / mobile number if available. If the number is set in the user object, a new line will be created depending on the file format – if the number is not set the search-variable will be removed from the signature (you off course don’t want it there) instead of writing the information out. In this case we add a “Cell: ” as prefix before the number so the signature indicates clearly what this number is about. Simply said, since we replace a variable and not a whole line – we have to write out more then just the number – in this case we want to add text.
Can you execute the script with various signatures per department?
Yes – you actually can – but you would need to do this with an additional script and e.g. IFMEMBER from Microsoft or group based GPOs etc…
Can you create more then one signature?
Yes – you can execute the script in various ways – you can roll out a NEW MAIL signature (full length signature) and a second version for REPLIES (short signature) and additional signatures that the user could choose from that aren’t set as either NEW MAIL or REPLY signature. The script header explains how to call the script and what parameters it will expect and how to set them.
Feel free to use the script below, adjust it for your needs. I know some of the stuff like your domain-name could be searched automatically instead of putting it hard-coded in the script – even the reg-keys could be more advanced, feel free to do so – but in the end it is not that much work and it does its job either way.
Debugging the script
At line 79 the statement “On Error Resume Next” avoids that you see errors that might arise. This is good for production so that the client/user does see as little as possible messages due to timeouts or special circumstances – but if you want to debug something or in the process to test the script itself, please remark the line so errors actually occur. They might not mean much in some cases, but they might also give you the hint you need to see what is going wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | 'Parameters to execute the script: 'OutlookSignatures.VBS strInputFilePrefix bolUseAsDefaultNewMail bolUseAsDefaultReplyMail ' parameters are separated by spaces - use "" for input-file-names with spaces ' strInputFilePrefix: file name of the signature file - do not add any extensions (.txt / .htm / .rtf) ' bolUseAsDefaultNewMail: TRUE = 1 or -1 / FALSE = 0 - use numeric values ' bolUseAsDefaultReplyMail: TRUE = 1 or -1 / FALSE = 0 - use numeric values 'Example 'SIGNATUR.VBS MySignature 1 1 'generic variables - do not adjust 'Const HKEY_CLASSES_ROOT=&H80000000 'Const HKEY_LOCAL_MACHINE=&H80000002 'Const HKEY_USERS=&H80000003 'Const HKEY_CURRENT_CONFIG=&H80000005 Const HKEY_CURRENT_USER = &H80000001 Dim strLogonServer Dim strDomain Dim strEX Dim strSignaturName Dim strLDAPPath Dim strFelder Dim bolUseAsDefaultNewMail Dim bolUseAsDefaultReplyMail 'Settings you need to adjust '------------- BEGIN OF SETTINGS ------------- strDomain = "MyDomain" 'short NetBIOS domain name should be sufficient strLDAPPath = "dc=MyDomain,dc=local" '------------- SPECIAL SETTINGS ------------- 'Those are the fields / attributes from AD that the script will obey and try to replace 'Additionally there is another special settings section further below that will do the acutal replacement strFelder = "streetAddress,l,info,postalCode,telephoneNumber,facsimileTelephoneNumber,mail,sn,givenName,department,mobile" '------------- END OF SETTINGS ------------- 'the script assumes it is started from the logonserver respective from the \\mydomain\netlogon directory Dim oShell Set oShell = CreateObject("WScript.Shell") strLogonServer = oShell.expandEnvironmentStrings("%LogonServer%") strLogonServer = Replace(strLogonServer, "\\", "") Set oShell = Nothing 'read parameters strInputFilePrefix = WScript.Arguments.Item(0) bolUseAsDefaultNewMail = CBool(WScript.Arguments.Item(1)) bolUseAsDefaultReplyMail = CBool(WScript.Arguments.Item(2)) 'read files and adjust them strEX = ".htm" Convert strEX = ".rtf" Convert strEX = ".txt" Convert Sub Convert() Dim objNetwork Dim strDestinationPath Dim strInputFile Dim strOutputFile Dim strUser Dim strUserProfile Dim i Dim j Dim cn Dim rs Dim strSignaturNameHex Dim cmdParam Dim strUserstrDomain Dim strAppData Dim strSignaturePath Dim fso Dim strCurrentLine Set objNetwork = CreateObject("WScript.Network") Set WSHShell = WScript.CreateObject("WScript.Shell") On Error Resume Next 'find current Windows user and profile path strUserDomain = WSHShell.Environment("PROCESS").Item("userdomain") If LCase(strUserDomain) = LCase(strDomain) Then strUser = objNetwork.UserName Else strUser = WSHShell.Environment("PROCESS").Item("userdomain") End If strUserProfile = WSHShell.Environment("PROCESS").Item("userprofile") strAppData = WSHShell.Environment("PROCESS").Item("appdata") 'LDAP connection Set objConnection = CreateObject("ADODB.Connection") objConnection.Open "Provider=ADsDSOObject;" Set objCommand = CreateObject("ADODB.Command") objCommand.ActiveConnection = objConnection objCommand.CommandText = _ "<LDAP://" & strLDAPPath & ">;(&(objectCategory=User)" & _ "(samAccountName=" & strUser & "));" & strFelder & ";subtree" Set objRecordset = objCommand.Execute If Not objRecordset.RecordCount = 0 Then strSignaturName = strInputFilePrefix 'if you pull the signatures from a different path then \\mydomain\netlogon adjust the following line accordingly strInputFile = "\\" & strLogonServer & "\NetLogon\Signatures\" & strSignaturName & strEX 'this will write the signature to the user profile - should not need adjustment strSignaturePath = strAppData & "\Microsoft\Signatures\" strOutputFile = strSignaturePath & strSignaturName & strEX Set fso = CreateObject("Scripting.FileSystemObject") If Not fso.FileExists(strInputFile) Then MsgBox "Outlook-Signatures input file '" & strInputFile & "' not found or access denied - contact the helpdesk please!" Exit Sub End If If Not fso.FolderExists(strSignaturePath) Then fso.CreateFolder (strSignaturePath) End If 'let's see if there is a special sub-folder that comes with the signatures 'note that this might depend on your language - it might be "-Dateien" (german) or "_files" in english - in the end it 'is the signature name plus an Windows language specific extension as a sub-folder 'both languages are already obeyed - adjust if you have additional versions 'the script will copy the folder to the destination If strEX = ".htm" Then If fso.FolderExists("\\" & strLogonServer & "\NetLogon\signaturen\" & strSignaturName & "-Dateien") Then fso.CopyFolder "\\" & strLogonServer & "\NetLogon\signaturen\" & strSignaturName & "-Dateien", strAppData & "\Microsoft\Signatures\", True End If If fso.FolderExists("\\" & strLogonServer & "\NetLogon\signaturen\" & strSignaturName & "_files") Then fso.CopyFolder "\\" & strLogonServer & "\NetLogon\signaturen\" & strSignaturName & "_files", strAppData & "\Microsoft\Signatures\", True End If End If Set TS = fso.OpenTextFile(strInputFile, 1, False, -2) 'Change 0 to -1 or -2 for unicode files. Set MyFile = fso.CreateTextFile(strOutputFile, True, False) While Not TS.AtendOfStream linesource = Trim(TS.ReadLine) strCurrentLine = linesource If InStr(strCurrentLine, "@@") > 0 Then '------------- SPECIAL SETTINGS ------------- 'the code in this section will replace variables in the source files with fields from Active Directory/LDAP. In some cases you might want special 'replacements or e.g. write extra information - like if mobile number exists write "Cell: " + mobile - if not - do nothing - this might also depend on the current 'file extension strEX - see examples below 'regular replacements strCurrentLine = Replace(strCurrentLine, "@@GivenName@@", NZ(objrecordset.Fields("givenName"), "")) strCurrentLine = Replace(strCurrentLine, "@@LastName@@", NZ(objrecordset.Fields("sn"), "")) strCurrentLine = Replace(strCurrentLine, "@@Department@@", NZ(objrecordset.Fields("department"), "")) 'file specific and value specific replacement exmaples If strEX = ".htm" then 'this will ad a line break after in HTML code strCurrentLine = Replace(Replace(strCurrentLine, "@@Title@@", NZ(objrecordset.Fields("title"), "")),chr(13),"<br>") Else 'non html files don't need this line-break in this example - you could add a VBNewLine or CHR(10) & CHR(13) etc.. strCurrentLine = Replace(strCurrentLine, "@@Title@@", NZ(objrecordset.Fields("title"), "")) End If strCurrentLine = Replace(strCurrentLine, "@@eMail@@", NZ(objrecordset.Fields("mail"), "")) strCurrentLine = Replace(strCurrentLine, "@@Phone@@", NZ(objrecordset.Fields("telephoneNumber"), "")) strCurrentLine = Replace(strCurrentLine, "@@Fax@@", NZ(objrecordset.Fields("facsimileTelephoneNumber"), "")) strCurrentLine = Replace(strCurrentLine, "@@Street@@", NZ(objrecordset.Fields("streetAddress"), "")) strCurrentLine = Replace(strCurrentLine, "@@ZIPandCity@@", NZ(objrecordset.Fields("postalCode"), "") & " " & NZ(objrecordset.Fields("l"), "")) 'file specific and value specific replacement exmaples for mobile / cell number or not - the line will only appear if there is a cell phone number in LDAP If Len(Trim(NZ(objrecordset.Fields("mobile"), ""))) > 0 Then If strEX = ".htm" Then strCurrentLine = Replace(strCurrentLine, "@@Mobile@@", "<br>Cell: " & NZ(objrecordset.Fields("mobile"), "")) Else strCurrentLine = Replace(strCurrentLine, "@@Mobile@@", "Cell: " & NZ(objrecordset.Fields("mobile"), "")) End If Else strCurrentLine = Replace(strCurrentLine, "@@Mobile@@", "") End If '------------- END OF SPECIAL SETTINGS ------------- End If MyFile.WriteLine strCurrentLine Wend Else MsgBox ("Outlook-Signatures: User object not found in LDAP - please contact the helpdesk!") End If objConnection.Close If strSignaturName <> "" Then 'Registry settings - version specific 'Outlook 2000/2002/XP If bolUseAsDefaultNewMail Then WSHShell.RegWrite "HKCU\Software\Microsoft\Office\10.0\Common\MailSettings\NewSignature", strSignaturName Else WSHShell.RegWrite "HKCU\Software\Microsoft\Office\10.0\Common\MailSettings\NewSignature", "" End If If bolUseAsDefaultReplyMail Then WSHShell.RegWrite "HKCU\Software\Microsoft\Office\10.0\Common\MailSettings\ReplySignature", strSignaturName Else WSHShell.RegWrite "HKCU\Software\Microsoft\Office\10.0\Common\MailSettings\ReplySignature", "" End If 'Outlook 2003 (sorry - might be I lost the correct RegKey - but I doubgt anyone is still using 2003 - might be the same os the 2007 'we need some special calculations here - convert the regular strings to a reg-key formar / binary for Outlook 2007 and newer ReDim arr((Len(strSignaturName) * 2) + 1) For i = 1 To Len(strSignaturName) arr((i - 1) * 2) = Asc(Mid(strSignaturName, i, 1)) arr(((i - 1) * 2) + 1) = 0 Next arr((i - 1) * 2) = 0 arr(((i - 1) * 2) + 1) = 0 ReDim arrClean((Len("") * 2) + 1) For i = 1 To Len("") arrClean((i - 1) * 2) = Asc(Mid("", i, 1)) arrClean(((i - 1) * 2) + 1) = 0 Next arrClean((i - 1) * 2) = 0 arrClean(((i - 1) * 2) + 1) = 0 'Outlook 2007 Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") RegKeyName = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\9375CFF0413111d3B88A00104B2A6676\00000002" RegName = "New Signature" If bolUseAsDefaultNewMail Then BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arr) Else BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arrClean) End If RegName = "Reply-Forward Signature" If bolUseAsDefaultReplyMail Then BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arr) Else BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arrClean) End If Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") 'Outlook 2010 Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") RegKeyName = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\9375CFF0413111d3B88A00104B2A6676\00000003" RegName = "New Signature" If bolUseAsDefaultNewMail Then BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arr) Else BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arrClean) End If RegName = "Reply-Forward Signature" If bolUseAsDefaultReplyMail Then BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arr) Else BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arrClean) End If Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") 'Outlook 2013 Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") RegKeyName = "Software\Microsoft\Office\15.0\Outlook\Profiles\Outlook\9375CFF0413111d3B88A00104B2A6676\00000002" RegName = "New Signature" If bolUseAsDefaultNewMail Then BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arr) Else BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arrClean) End If RegName = "Reply-Forward Signature" If bolUseAsDefaultReplyMail Then BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arr) Else BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arrClean) End If Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") 'Outlook 2016 Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") RegKeyName = "Software\Microsoft\Office\16.0\Outlook\Profiles\Outlook\9375CFF0413111d3B88A00104B2A6676\00000002" RegName = "New Signature" If bolUseAsDefaultNewMail Then BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arr) Else BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arrClean) End If RegName = "Reply-Forward Signature" If bolUseAsDefaultReplyMail Then BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arr) Else BinaryReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, RegKeyName, RegName, arrClean) End If Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") End If MyFile.Close TS.Close Set fso = Nothing Set TS = Nothing Set MyFile = Nothing End Sub Function NZ(ValueIfNotNull, ValueIfNull) NZ = ValueIfNotNull If (IsNull(NZ)) Then NZ = ValueIfNull End Function |