Rewriting existing .htm signature file in Outlook folder

Welcome Forums General PowerShell Q&A Rewriting existing .htm signature file in Outlook folder

Viewing 5 reply threads
  • Author
    Posts
    • #207522
      Participant
      Topics: 1
      Replies: 3
      Points: 25
      Rank: Member

      Hello, I found this awesome script online to create an email signature in a users local Outlook. It is all working via a GPO, however, if I wanted to make any changes to the html or the script on the server to change the .htm being created, I would have to go into each user’s appdata folders and delete the file so it would be recreated the next time they logon. Can someone help with what I need to add to this script for it to overwrite/replace the current file in the appdata/user/roaming/MSFT/signatures folder folder every time script is run?

      
      # Gets the path to the user appdata folder
      
      $AppData = (Get-Item env:appdata).value
      
      # This is the default signature folder for Outlook
      
      $localSignatureFolder = $AppData+'\Microsoft\Signatures'
      
      # This is a shared folder on your network where the signature template should be
      
      $templateFilePath = "C:\Users\\Desktop\template.html"
      
      # Get the current logged in username
      
      $userName = $env:username
      
      # The following 5 lines will query AD and get an ADUser object with all information
      
      $filter = "(&(objectCategory=User)(samAccountName=$userName))"
      
      $searcher = New-Object System.DirectoryServices.DirectorySearcher
      
      $searcher.Filter = $filter
      
      $ADUserPath = $searcher.FindOne()
      
      $ADUser = $ADUserPath.GetDirectoryEntry()
      
      # Now extract all the necessary information for the signature
      
      $name = $ADUser.displayName
      
      $email = $ADUser.mail
      
      $job = $ADUser.description
      
      $department = $ADUser.department
      
      $phone = $ADUser.telephonenumber
      
      $address = $ADUser.streetaddress
      
      $website = $ADUser.wWWHomePage
      
      $city = $ADUser.l
      
      $mobile = $ADUser.mobile
      
      $state = $ADUser.st
      
      $postcode = $ADUser.postalCode
      
      $namePlaceHolder = "DISPLAY_NAME"
      
      $emailPlaceHolder = "EMAIL"
      
      $jobPlaceHolder = "JOB_TITLE"
      
      $departmentPlaceHolder = "DEPARTMENT"
      
      $phonePlaceHolder = "PHONE"
      
      $addressPlaceHolder = "ADDRESS"
      
      $websitePlaceHolder = "WEBSITE"
      
      $cityPlaceholder = "SUBURB"
      
      $mobilePlaceholder = "MOBILE"
      
      $statePlaceHolder = "STATE"
      
      $postcodePlaceHolder = "POSTCODE"
      
      $rawTemplate = get-content $templateFilePath"\template.html"
      
      $signature = $rawTemplate -replace $namePlaceHolder,$name
      
      $rawTemplate = $signature
      
      $signature = $rawTemplate -replace $emailPlaceHolder,$email
      
      $rawTemplate = $signature
      
      $signature = $rawTemplate -replace $phonePlaceHolder,$phone
      
      $rawTemplate = $signature
      
      $signature = $rawTemplate -replace $jobPlaceHolder,$job
      
      $rawTemplate = $signature
      
      $signature = $rawTemplate -replace $departmentPlaceHolder,$department
      
      $rawTemplate = $signature
      
      $signature = $rawTemplate -replace $websitePlaceHolder,$website
      
      $rawTemplate = $signature
      
      $signature = $rawTemplate -replace $cityPlaceHolder,$city
      
      $rawTemplate = $signature
      
      $signature = $rawTemplate -replace $mobilePlaceHolder,$mobile
      
      $rawTemplate = $signature
      
      $signature = $rawTemplate -replace $statePlaceHolder,$state
      
      $rawTemplate = $signature
      
      $signature = $rawTemplate -replace $postcodePlaceHolder,$postcode
      
      $rawTemplate = $signature
      
      $signature = $rawTemplate -replace $addressPlaceHolder,$address
      
      # Save it as <username>.htm
      
      $fileName = $localSignatureFolder + "\" + $userName + ".htm"
      
      # Gets the last update time of the template.
      
      if(test-path $templateFilePath){
      
          $templateLastModifiedDate = [datetime](Get-ItemProperty -Path $templateFilePath -Name LastWriteTime).lastwritetime
      
      }
      
      # Checks if there is a signature and its last update time
      
      if(test-path $filename){
      
          $signatureLastModifiedDate = [datetime](Get-ItemProperty -Path $filename -Name LastWriteTime).lastwritetime
      
          if((get-date $templateLastModifiedDate) -gt (get-date $signatureLastModifiedDate)){
      
              $signature > $fileName
      
          }
      
      }else{
      
          $signature > $fileName
      }
      • This topic was modified 3 months ago by Darren H.
      • This topic was modified 3 months ago by grokkit.
      • This topic was modified 2 months, 3 weeks ago by kvprasoon. Reason: code formatting
    • #209868
      Participant
      Topics: 1
      Replies: 3
      Points: 25
      Rank: Member

      Anyone know if this is possible with the above script?

    • #209994
      Participant
      Topics: 6
      Replies: 93
      Points: 427
      Helping Hand
      Rank: Contributor

      At first glance, it looks like you are already doing that with

      $signature > $fileName

      It also looks like there have been two edits other than yourself … maybe they fixed it for you?

       

    • #210420
      Participant
      Topics: 1
      Replies: 3
      Points: 25
      Rank: Member

      At first glance, it looks like you are already doing that with

      $signature > $fileName

      It also looks like there have been two edits other than yourself … maybe they fixed it for you?

      I think the edits were mainly for presentation. I’m sure the code is exactly the same.

      The script only creates a signature if the signature folder is empty. If there are changes to the html template I have to ask everyone to delete their signature and then log on and off again. I would prefer a script that overwrites the file.

      • This reply was modified 2 months, 2 weeks ago by Darren H.
      • This reply was modified 2 months, 2 weeks ago by Darren H.
    • #210570
      Participant
      Topics: 6
      Replies: 93
      Points: 427
      Helping Hand
      Rank: Contributor

      Maybe try Out-File with Force

      $signature | Out-File -FilePath $fileName -Force

       

    • #211143
      Participant
      Topics: 1
      Replies: 3
      Points: 25
      Rank: Member

      Thanks Tony!! It didn’t work in my test, unfortunately. Still won’t override the current signature file.

      I assume you mean replacing $signature > $fileName with your above code?

      Edit: What I did was replace everything under line 122 with this:

      # Save it as .htm
      $fileName = $localSignatureFolder + "\" + $userName + ".htm"

      $signature | Out-File -FilePath $fileName -Force

      It actually works. I’m wondering now. Does this mean that the code removed is not important to the function of the script:

      # Gets the last update time of the template.
      if(test-path $templateFilePath){
      $templateLastModifiedDate = [datetime](Get-ItemProperty -Path $templateFilePath -Name LastWriteTime).lastwritetime
      }

      # Checks if there is a signature and its last update time
      if(test-path $filename){$signatureLastModifiedDate = [datetime](Get-ItemProperty -Path $filename -Name LastWriteTime).lastwritetime
      if((get-date $templateLastModifiedDate) -gt (get-date $signatureLastModifiedDate)){
      $signature > $fileName

      • This reply was modified 2 months, 2 weeks ago by Darren H.
      • This reply was modified 2 months, 2 weeks ago by Darren H.
      • This reply was modified 2 months, 2 weeks ago by Darren H.
      • This reply was modified 2 months, 2 weeks ago by Darren H.
      • This reply was modified 2 months, 2 weeks ago by Darren H.
Viewing 5 reply threads
  • You must be logged in to reply to this topic.