Advertisement
Guest User

OutlookSignatureScript

a guest
Aug 29th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###########################################################################"
  2. #
  3. # NAME: Set-OutlookSignature.ps1
  4. #
  5. # AUTHOR: Jan Egil Ring
  6. # Modifications by Darren Kattan
  7. # Further Modifications by Jamie McKillop - http://jamiemckillop.wordpress.com/
  8. #
  9. # COMMENT: Script to create an Outlook signature based on user information from Active Directory.
  10. #          Adjust the variables in the "Custom variables"-section
  11. #          Create an Outlook-signature from Microsoft Word (logo, fonts etc) and copy this signature to \\domain\NETLOGON\Signatures\$SignatureName\$SignatureName.docx
  12. #          This script supports the following keywords:
  13. #           DisplayName
  14. #           Title
  15. #           Email
  16. #                      Description
  17. #                      TelephoneNumber
  18. #                      facsimileTelephoneNumber
  19. #                      mobile
  20. #                      streetaddress
  21. #                      City
  22. #                     postofficebox
  23. #                     extensionAttribute1
  24. #   See the following blog-posts for more information:
  25. # http://blog.crayon.no/blogs/janegil/archive/2010/01/09/outlook-signature-based-on-user-information-from-active-directory.aspx
  26. # http://gallery.technet.microsoft.com/office/6f7eee4b-1f42-499e-ae59-1aceb26100de
  27. # http://www.experts-exchange.com/Software/Server_Software/Email_Servers/Exchange/Q_28035335.html
  28. # http://jamiemckillop.wordpress.com/category/powershell/
  29. # http://www.immense.net/deploying-unified-email-signature-template-outlook/
  30. #
  31. #          Tested on Office 2003,2007,2010 and 2013
  32. #
  33. # You have a royalty-free right to use, modify, reproduce, and
  34. # distribute this script file in any way you find useful, provided that
  35. # you agree that the creator, owner above has no warranty, obligations,
  36. # or liability for such use.
  37. #
  38. # VERSION HISTORY:
  39. # 1.0 09.01.2010 - Initial release
  40. # 1.1 11.09.2010 - Modified by Darren Kattan
  41. #   - Removed bookmarks. Now uses simple find and replace for DisplayName, Title, and Email.
  42. #   - Email address is generated as a link
  43. #   - Signature is generated from a single .docx file
  44. #   - 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.
  45. # 1.11 11.15.2010 - Revised by Darren Kattan
  46. #   - Fixed glitch with text signatures
  47. # 1.2 07.06.2012 - Revised by Jamie McKillop
  48. #   - Modified script so that Force Signature settings are set on first run of script
  49. #   - Added variables to allow setting of default signature on creation of signature but not force the signature on each script run
  50. #   - Used variables defined in script for $ForceSignatureNew and $ForceSignatureReplyForward instead of pulling values from the registry
  51. # 1.3 01.13.2014 - Revised by Dominic Whyle
  52. #   - Modified script so Include logging
  53. #   - Added variables to allow setting of default signature address, telephone, fax and city
  54. #   - Modifed script to replace unused fields (Mobile or Description)
  55. #   - Added force script to run in x86mode for x86 versions of office
  56. # 1.4 01.16.2014 - Revised by Dominic Whyle
  57. #   - Added variable for AD account whenChanged to allow automatic updating of signauture when AD account changes
  58. #
  59. ###########################################################################"
  60.  
  61. #Run Script in x86 Mode
  62. if ($env:Processor_Architecture -ne "x86")
  63. { write-warning 'Launching x86 PowerShell'
  64. &"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -noninteractive -noprofile -file $myinvocation.Mycommand.path -executionpolicy bypass
  65. exit
  66. }
  67. "Always running in 32bit PowerShell at this point."
  68. $env:Processor_Architecture
  69. [IntPtr]::Size
  70.  
  71. #Custom variables
  72. $SignatureName = 'companyName' #insert the company name (no spaces) - could be signature name if more than one sig needed
  73. $DomainName = 'companyName.com' #insert the domain name
  74. $SigSource = "\\$DomainName\netlogon\sig_files\$SignatureName" #Change if desired for signature templates
  75. $ForceSignatureNew = '1' #When the signature is forced it sets the default signature for new messages each time the script runs. 0 = no force, 1 = force
  76. $ForceSignatureReplyForward = '1' #When the signature is forced it sets the default signature for reply/forward messages each time the script runs. 0 = no force, 1 = force
  77. $SetSignatureNew = '1' #Determines wheter to set the signature as the default for new messages on first run. This is overridden if $ForceSignatureNew = 1. 0 = don't set, 1 = set
  78. $SetSignatureReplyForward = '1' #Determines wheter to set the signature as the default for reply/forward messages on first run. This is overridden if $ForceSignatureReplyForward = 1. 0 = don't set, 1 = set
  79. $DefaultAddress = 'No Active Directory Entry'
  80. $DefaultCity = 'No Active Directory Entry' #insert default city
  81. $DefaultTelephone = 'No Active Directory Entry' #insert default phone number
  82. $DefaultFax = 'No Active Directory Entry' #insert default fax number
  83. $DefaultState = 'No Active Directory Entry'
  84. $DefaultZip = 'No Active Directory Entry'
  85. $DefaultDirectNum = 'No Active Directory Entry'
  86. $DefaultDepartment = 'No Active Directory Entry'
  87. $DefaultDisplayName = 'No Active Directory Entry'
  88.  
  89. #Environment variables
  90. $AppData=(Get-Item env:appdata).value
  91. $SigPath = '\Microsoft\Signatures'
  92. $LocalSignaturePath = $AppData+$SigPath
  93. $RemoteSignaturePathFull = $SigSource+'\'+$SignatureName+'.docx'
  94.  
  95. #Check signature path (needs to be created if a signature has never been created for the profile
  96. if (!(Test-Path -path $LocalSignaturePath)) {
  97.     New-Item $LocalSignaturePath -Type Directory
  98. }
  99.  
  100. #Get Active Directory information for current user
  101. $UserName = $env:username
  102. $Filter = "(&(objectCategory=person)(objectClass=user)(samAccountName=$UserName))"
  103. $Searcher = New-Object System.DirectoryServices.DirectorySearcher
  104. $Searcher.Filter = $Filter
  105. $ADUserPath = $Searcher.FindOne()
  106. $ADUser = $ADUserPath.GetDirectoryEntry()
  107. $ADDisplayName = $ADUser.DisplayName
  108. $ADEmailAddress = $ADUser.mail
  109. $ADTitle = $ADUser.title
  110. $ADDescription = $ADUser.description
  111. $ADTelePhoneNumber = $ADUser.TelephoneNumber
  112. $ADFax = $ADUser.facsimileTelephoneNumber
  113. $ADMobile = $ADUser.mobile
  114. $ADStreetAddress = $ADUser.streetaddress
  115. $ADCity = $ADUser.l
  116. $ADState = $ADUser.st
  117. $ADZip = $ADUser.postalCode
  118. $ADDept = $ADUser.department
  119. $ADDirectNum = $ADUser.homePhone
  120. $ADModify = $ADUser.whenChanged
  121.  
  122. #Setting registry information for the current user
  123. $CompanyRegPath = "HKCU:\Software\"+$DomainName
  124. $SignatureRegPath = $CompanyRegPath+'\'+$SignatureName
  125.  
  126. if (Test-Path $SignatureRegPath) { Echo 'Company Registry Exists'
  127. }
  128. else {
  129.     New-Item -path "HKCU:\Software" -name $DomainName
  130.     New-Item -path $CompanyRegPath -name $SignatureName
  131. }
  132.  
  133. if (Test-Path $SignatureRegPath'\Outlook Signature Settings') { Echo 'Outlook Settings Exist'
  134. }
  135. else {
  136.     New-Item -path $SignatureRegPath -name "Outlook Signature Settings"
  137. }
  138.  
  139. $SigVersion = (gci $RemoteSignaturePathFull).LastWriteTime  #When was the last time the signature was written
  140. $SignatureVersion = (Get-ItemProperty $SignatureRegPath'\Outlook Signature Settings').SignatureVersion
  141. Set-ItemProperty $SignatureRegPath'\Outlook Signature Settings' -name SignatureSourceFiles -Value $SigSource
  142. $SignatureSourceFiles = (Get-ItemProperty $SignatureRegPath'\Outlook Signature Settings').SignatureSourceFiles
  143. Set-ItemProperty $SignatureRegPath'\Outlook Signature Settings' -name UserAccountModifyDate -Value $ADModify.ToString()
  144. $UserModify = (Get-ItemProperty $SignatureRegPath'\Outlook Signature Settings').UserAccountModifyDate
  145.  
  146. #Copying signature sourcefiles and creating signature if signature-version are different from local version
  147. if (($SignatureVersion -eq $SigVersion) -or ($UserModify -eq $ADModify))
  148.     {
  149.     Echo 'Primary signature is up to date'
  150. }
  151. else
  152. {
  153.  
  154.     Echo 'Running Main Script'
  155.  
  156.     #Copy signature templates from domain to local Signature-folder
  157.     Copy-Item "$SignatureSourceFiles\*" $LocalSignaturePath -Recurse -Force
  158.     $ReplaceAll = 2
  159.     $FindContinue = 1
  160.     $MatchCase = $False
  161.     $MatchWholeWord = $True
  162.     $MatchWildcards = $False
  163.     $MatchSoundsLike = $False
  164.     $MatchAllWordForms = $False
  165.     $Forward = $True
  166.     $Wrap = $FindContinue
  167.     $Format = $False
  168.    
  169.     #Insert variables from Active Directory to rtf signature-file
  170.     $MSWord = New-Object -com word.application
  171.     $fullPath = $LocalSignaturePath+'\'+$SignatureName+'.docx'
  172.     $MSWord.Documents.Open($fullPath)
  173.    
  174.     #Street Address
  175.     If ($ADDisplayName -ne '') {
  176.         $FindText = "DisplayName"
  177.         $ReplaceText = $ADDisplayName.ToString()
  178.     }
  179.     Else {
  180.         $FindText = "DisplayName"
  181.         $ReplaceText = $DefaultDisplayName
  182.     }
  183.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  184.  
  185.     #Title     
  186.     $FindText = "Title"
  187.     $ReplaceText = $ADTitle.ToString()
  188.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  189.        
  190.     #Description
  191.     If ($ADDescription -ne '') {
  192.         $FindText = "Description"
  193.         $ReplaceText = $ADDescription.ToString()
  194.     }
  195.     Else {
  196.         $FindText = " | Description "
  197.         $ReplaceText = "".ToString()
  198.     }
  199.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  200.        
  201.     #Street Address
  202.     If ($ADStreetAddress -ne '') {
  203.         $FindText = "StreetAddress"
  204.         $ReplaceText = $ADStreetAddress.ToString()
  205.     }
  206.     Else {
  207.         $FindText = "StreetAddress"
  208.         $ReplaceText = $DefaultAddress
  209.     }
  210.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  211.            
  212.     #State
  213.     If ($ADState -ne '') {
  214.         $FindText = "ST"
  215.         $ReplaceText = $ADState.ToString()
  216.     }
  217.     Else {
  218.         $FindText = "ST"
  219.         $ReplaceText = $DefaultState
  220.     }
  221.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  222.        
  223.     #Zip
  224.     If ($ADZip -ne '') {
  225.         $FindText = "Zip"
  226.         $ReplaceText = $ADZip.ToString()
  227.     }
  228.     Else {
  229.         $FindText = "Zip"
  230.         $ReplaceText = $DefaultZip
  231.     }
  232.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  233.        
  234.     #Department
  235.     If ($ADDept -ne '') {
  236.         $FindText = "Department"
  237.         $ReplaceText = $ADDept.ToString()
  238.     }
  239.     Else {
  240.         $FindText = "Department"
  241.         $ReplaceText = $DefaultDept
  242.     }
  243.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  244.        
  245.     #Direct Number
  246.     If ($ADDirectNum -ne '') {
  247.         $FindText = "DirectPhone"
  248.         $ReplaceText = $ADDirectNum.ToString()
  249.     }
  250.     Else {
  251.         $FindText = "DirectPhone"
  252.         $ReplaceText = $DefaultDirectNum
  253.     }
  254.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  255.    
  256.     #City
  257.     If ($ADCity -ne '') {
  258.         $FindText = "City"
  259.         $ReplaceText = $ADCity.ToString()
  260.     }
  261.     Else {
  262.         $FindText = "City"
  263.         $ReplaceText = $DefaultCity
  264.     }
  265.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  266.        
  267.     #Telephone
  268.     If ($ADTelephoneNumber -ne "") {
  269.         $FindText = "Telephone"
  270.         $ReplaceText = $ADTelephoneNumber.ToString()
  271.     }
  272.     Else {
  273.         $FindText = "Telephone"
  274.         $ReplaceText = $DefaultTelephone
  275.     }
  276.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  277.        
  278.     #Mobile
  279.     If ($ADMobile -ne "") {
  280.         $FindText = "MobileNumber"
  281.         $ReplaceText = $ADMobile.ToString()
  282.     }
  283.     Else {
  284.         $FindText = "| Mob MobileNumber "
  285.         $ReplaceText = "".ToString()
  286.     }
  287.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  288.    
  289.     #Fax
  290.     If ($ADFaxNumber -ne '') {
  291.         $FindText = "FaxNumber"
  292.         $ReplaceText = $ADFaxNumber
  293.     }
  294.     Else {
  295.         $FindText = "Fax"
  296.         $ReplaceText = $DefaultFax
  297.     }
  298.     $ReplaceText = $ADFax.ToString()
  299.     $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,  $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll  )
  300.    
  301.     $MSWord.Selection.Find.Execute("EmailAddress")
  302.     $MSWord.ActiveDocument.Hyperlinks.Add($MSWord.Selection.Range, "mailto:"+$ADEmailAddress.ToString(), $missing, $missing, $ADEmailAddress.ToString())
  303.  
  304.     #Save new message signature
  305.     Echo 'Saving Signatures'
  306.     #Save HTML
  307.     $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatHTML");
  308.     $path = $LocalSignaturePath+'\'+$SignatureName+".htm"
  309.     $MSWord.ActiveDocument.saveas([ref]$path, [ref]$saveFormat)
  310.    
  311.     #Save RTF
  312.     $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatRTF");
  313.     $path = $LocalSignaturePath+'\'+$SignatureName+".rtf"
  314.     $MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat)
  315.    
  316.     #Save TXT    
  317.     $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatText");
  318.     $path = $LocalSignaturePath+'\'+$SignatureName+".txt"
  319.     $MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$SaveFormat)
  320.     $MSWord.ActiveDocument.Close()
  321.     $MSWord.Quit()
  322.    
  323.     #Save new message signature
  324.     #Echo 'Saving Signatures'
  325.     #Save HTML
  326.     #$SaveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatHTML");
  327.     #[ref]$BrowserLevel = "microsoft.office.interop.word.WdBrowserLevel" -as [type]
  328.     #$New.WebOptions.OrganizeInFolder = $true
  329.     #$New.WebOptions.UseLongFileNames = $true
  330.     #$New.WebOptions.BrowserLevel = $BrowserLevel::wdBrowserLevelMicrosoftInternetExplorer6
  331.     #$Path = $LocalSignaturePath+'\'+$CompanyName+'.htm'
  332.     #If ((Test-Path $Path) -eq '1') { (Get-Item $Path).attributes = 'Archive' }
  333.     #$New.SaveAs([ref]$Path, [ref]$saveFormat)
  334.  
  335.     #Set signature for new mesages if enabled
  336.     if ($SetSignatureNew -eq '1') {
  337.         #Set company signature as default for New messages
  338.         $MSWord = New-Object -com word.application
  339.         $EmailOptions = $MSWord.EmailOptions
  340.         $EmailSignature = $EmailOptions.EmailSignature
  341.         $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
  342.         $EmailSignature.NewMessageSignature=$SignatureName
  343.         $MSWord.Quit()
  344.     }
  345.    
  346.     #Set signature for reply/forward messages if enabled
  347.     if ($SetSignatureReplyForward -eq '1') {
  348.         #Set company signature as default for Reply/Forward messages
  349.         $MSWord = New-Object -com word.application
  350.         $EmailOptions = $MSWord.EmailOptions
  351.         $EmailSignature = $EmailOptions.EmailSignature
  352.         $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
  353.         $EmailSignature.ReplyMessageSignature=$SignatureName
  354.         $MSWord.Quit()
  355.     }
  356. }
  357.  
  358. #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.
  359. if ($ForcedSignatureNew -eq $ForceSignatureNew){
  360. }
  361. else {
  362.     Set-ItemProperty $SignatureRegPath'\Outlook Signature Settings' -name ForcedSignatureNew -Value $ForceSignatureNew
  363. }
  364.  
  365. if ($ForcedSignatureReplyForward -eq $ForceSignatureReplyForward){
  366. }
  367. else {
  368.     Set-ItemProperty $SignatureRegPath'\Outlook Signature Settings' -name ForcedSignatureReplyForward -Value $ForceSignatureReplyForward
  369. }
  370.  
  371. if ($SignatureVersion -eq $SigVersion){
  372. }
  373. else {
  374.     Set-ItemProperty $SignatureRegPath'\Outlook Signature Settings' -name SignatureVersion -Value $SigVersion
  375. }
  376.  
  377. #Forcing signature for new messages if enabled
  378. if ($ForceSignatureNew -eq '1') {
  379.     #Set company signature as default for New messages
  380.     $MSWord = New-Object -com word.application
  381.     $EmailOptions = $MSWord.EmailOptions
  382.     $EmailSignature = $EmailOptions.EmailSignature
  383.     $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
  384.     $EmailSignature.NewMessageSignature=$SignatureName
  385.     $MSWord.Quit()
  386. }
  387.  
  388. #Forcing signature for reply/forward messages if enabled
  389. if ($ForceSignatureReplyForward -eq '1') {
  390.     #Set company signature as default for Reply/Forward messages
  391.     $MSWord = New-Object -com word.application
  392.     $EmailOptions = $MSWord.EmailOptions
  393.     $EmailSignature = $EmailOptions.EmailSignature
  394.     $EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
  395.     $EmailSignature.ReplyMessageSignature=$SignatureName
  396.     $MSWord.Quit()
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement