'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