PS input template html - output html without diacritics

Ivan Totar 0 Reputation points
2023-01-16T07:24:06.9066667+00:00

Hi everyone!

I have a problem with creating script that will create email signature html file. As template I use html file that is already defined. When I setup script that reads username and collects data from AD and puts in html file it works great but that new html file loses diacritics. How can I fix this?

Here is code:

# 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:\Installer\signature"

# 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
$office = $ADUser.physicalDeliveryOfficeName

$namePlaceHolder = "DISPLAY_NAME"
$emailPlaceHolder = "EMAIL"
$jobPlaceHolder = "JOB_TITLE"
$departmentPlaceHolder = "DEPARTMENT"
$phonePlaceHolder = "PHONE"
$rawTemplate = get-content $templateFilePath"\signature_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

# Save it as <username>.html
$fileName = $localSignatureFolder + "\" + $userName + ".html"


# 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
}
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2023-01-17T08:15:02.8133333+00:00

    Hi,

    Please use $signature | Out-File -FilePath $fileName instead of $signature > $fileName.

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Ivan Totar 0 Reputation points
    2023-01-19T09:33:10.18+00:00

    Thank you.

    When I do that replacement it crashes.

    Secound problem is that when I want to setup that signature for new and reply in Outlook I do it over registry, but it doesn't accept that.

    Here is complete code that creates signature copies to Signature map, creates registry keys but not working. You still need to click in Options to choose signature for new and reply.

    # 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 = "\\BLED\Installer\signature"
    $obavest = "To e-sporočilo (z vsemi prilogami) je namenjeno izključno izbranim prejemnikom. Če niste predvideni prejemnik in ste to e-sporočilo prejeli po pomoti, o tem nemudoma obvestite pošiljatelja ter izbrišite izvirno e-sporočilo in priloge brez podvajanja, distribucije ali prenosa vsebine katerikoli osebi ali organizaciji. Nepooblaščena uporaba, objava, shranjevanje in podvajanje vsebine e-sporočila je strogo prepovedano. Prenos e-sporočila ne zagotavlja, da so vsebovane informacije varne ali brez napak, saj so lahko elektronske informacije zastarele ali nepopolne, lahko so prestrežene, poškodovane, izgubljene ali vsebujejo viruse. Express One Si, d.o.o., zavrača kakršnokoli odgovornost za kakršnokoli škodo, povzročeno s prejemom in/ali uporabo tega e-sporočila kot tudi prilog, priloženih v tem e-sporočilu."
    $name_addon = "je vpisano dne 1. 4. 2022 v sodni register Okrožnega sodišča v Ljubljani; ID za DDV: SI 30657008, matična številka: 9101691000."
    $85disclamer = "This email (with any attachments) is intended for the attention of the addressee(s) only. If you are not the intended recipient, please inform the sender straight away before deleting the message without copying, distributing or disclosing its contents to any other person or organisation. Unauthorised use, disclosure, storage or copying is not permitted. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. Express One SI, d.o.o. therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission."
    
    
    # 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.title
    $address = $ADUser.streetAddress
    $phone = $ADUser.mobile
    $office = $ADUser.physicalDeliveryOfficeName
    $FirmName = $ADUser.Company
    $contry = $ADUser.co
    $st0 = $ADUser.st
    $PostalCode = $ADUser.postalCode
    $Obavestilo = $obavest
    $nameAddon = $name_addon
    $77disclamer_eng = $85disclamer
    
    
    $namePlaceHolder = "DISPLAY_NAME"
    $emailPlaceHolder = "EMAIL"
    $jobPlaceHolder = "JOB"
    $addressPlaceHolder = "ADDRESS"
    $phonePlaceHolder = "PHONE"
    $FirmNamePlaceHolder = "FIRM_NAME"
    $contryPlaceHolder = "CO23"
    $st0PlaceHolder = "STATE"
    $PostalCodePlaceHolder = "POSTAL"
    $ObavestiloPlaceHolder = "OBAVESTILO"
    $nameAddonPlaceHolder = "NAME_ADDON"
    $77disclamer_engPlaceHolder = "WDISCLAMER"
    
    $rawTemplate = get-content $templateFilePath"\signature_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 $addressPlaceHolder, $address
    $rawTemplate = $signature
    
    $signature = $rawTemplate -replace $FirmNamePlaceHolder, $FirmName
    $rawTemplate = $signature
    
    $signature = $rawTemplate -replace $contryPlaceHolder, $contry
    $rawTemplate = $signature
    
    $signature = $rawTemplate -replace $st0PlaceHolder, $st0
    $rawTemplate = $signature
    
    $signature = $rawTemplate -replace $PostalCodePlaceHolder, $PostalCode
    $rawTemplate = $signature
    
    $signature = $rawTemplate -replace $ObavestiloPlaceHolder, $obavest
    $rawTemplate = $signature
    
    $signature = $rawTemplate -replace $nameAddonPlaceHolder, $name_addon
    $rawTemplate = $signature
    
    $signature = $rawTemplate -replace $jobPlaceHolder, $job
    $rawTemplate = $signature
    
    $signature = $rawTemplate -replace $77disclamer_engPlaceHolder, $77disclamer_eng
    $rawTemplate = $signature
    
    # Save it as <username>.htm
    $fileName = $localSignatureFolder + "\" + $userName + ".htm"
    
    
    # Create rich text file
    
    
    # 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 section enables signature to Outlook to be placed in new and reply
    # Setting the regkeys for Outlook 2016
    if (test-path "HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\General") 
    {
        get-item -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\General | new-Itemproperty -name Signatures -value signatures -propertytype string -force
        get-item -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\MailSettings | new-Itemproperty -name NewSignature -value $filename -propertytype string -force
        get-item -path HKCU:\\Software\\Microsoft\\Office\\16.0\\Common\\MailSettings | new-Itemproperty -name ReplySignature -value $filename -propertytype string -force
        Remove-ItemProperty -Path HKCU:\\Software\\Microsoft\\Office\\16.0\\Outlook\\Setup -Name "First-Run" -ErrorAction silentlycontinue
    }
    
    # Setting the regkeys for Outlook 2010 - Thank you AJWhite1970 for the 2010 registry keys
    if (test-path "HKCU:\\Software\\Microsoft\\Office\\14.0\\Common\\General") 
    {
        get-item -path HKCU:\\Software\\Microsoft\\Office\\14.0\\Common\\ General | new-Itemproperty -name Signatures -value signatures -propertytype string -force
        get-item -path HKCU:\\Software\\Microsoft\\Office\\14.0\\Common\\ MailSettings | new-Itemproperty -name NewSignature -value $filename -propertytype string -force
        get-item -path HKCU:\\Software\\Microsoft\\Office\\14.0\\Common\\ MailSettings | new-Itemproperty -name ReplySignature -value $filename -propertytype string -force
        Remove-ItemProperty -Path HKCU:\\Software\\Microsoft\\Office\\14.0\\Outlook\\Setup -Name "First-Run" -ErrorAction silentlycontinue
    }
    

  3. Rich Matheisen 47,901 Reputation points
    2023-01-19T19:40:14.57+00:00

    Where are the diacritics? Are then in the signature template file, or in the AD data from the user object?

    Are you not using PowerShell's Get-ADUser cmdlet for some reason? Long ago there was a reason, but that was before the ActiveDirectory module was included with the PowerShell installation. Here's a version of your script that cleans up some unnecessary clutter and uses Get-ADUser (it also checks to see if the user was found in the AD):

    # This is the default signature folder for Outlook
    $localSignatureFolder = "{0}\Microsoft\Signatures" -f $ENV:APPDATA
    # This is a shared folder on your network where the signature template should be
    $templateFilePath = "C:\Installer\signature"
    
    # Get the current logged in username
    $userName = $env:username
    
    # query AD and get an ADUser object with all information
    $props = 'DisplayName','Mail','Description','Department','OfficeTelephone','Office'
    $ADUser = Get-ADUser -filter "samaccountname -eq $($userName)" -Properties $props
    if (-NOT $ADUser){
        # didn't find the samaccountname in the AD
        return
    }
    
    # Now extract all the necessary information for the signature
    $name = $ADUser.DisplayName
    $email = $ADUser.mail
    $job = $ADUser.description
    $department = $ADUser.department
    $phone = $ADUser.OfficeTelephone
    $office = $ADUser.Office            # This is never used!
    
    $namePlaceHolder = "DISPLAY_NAME"
    $emailPlaceHolder = "EMAIL"
    $jobPlaceHolder = "JOB_TITLE"
    $departmentPlaceHolder = "DEPARTMENT"
    $phonePlaceHolder = "PHONE"
    $signature = get-content ("{0}\signature_template.html" -f $templateFilePath) -Encoding UTF8
    
    $signature = $signature -replace $namePlaceHolder,$name
    $signature = $signature -replace $emailPlaceHolder,$email
    $signature = $signature -replace $phonePlaceHolder,$phone
    $signature = $signature -replace $jobPlaceHolder,$job
    $signature = $signature -replace $departmentPlaceHolder,$department
    
    # Save it as <username>.html
    $fileName = "{0}\{1}.html" -f $localSignatureFolder, $userName
    
    # Gets the last update time of the template.
    if(test-path $templateFilePath){
        $templateLastModifiedDate = [datetime](Get-Item -Path $templateFilePath).lastwritetime
    }
    
    # Checks if there is a signature and its last update time
    if(test-path $filename){
        $signatureLastModifiedDate = (Get-Item -Path $filename).lastwritetime
        if((get-date $templateLastModifiedDate) -gt (get-date $signatureLastModifiedDate)){
            $signature | Out-File $fileName -Encoding UTF8
        }
    }else{
        $signature | Out-File $fileName -Encoding UTF8
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.