Rewriting existing .htm signature file in Outlook folder

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
}

Anyone know if this is possible with the above script?

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?

 

[quote quote=209994]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?

[/quote]

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.

Maybe try Out-File with Force

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

 

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 + "&quot; + $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