I have the below PowerShell code to set the email signature in Outlook, but somehow there is one issue/bug that is still persisting.
Code: Select all
$templateFile = '\\domain.com\SYSVOL\domain.com\scripts\My Custom Signature.docx'
$templateFileNoMobile = '\\domain.com\SYSVOL\domain.com\scripts\My Custom Signature - No MobilePhone.docx'
$defaultTelephone = '555 12345'
$signatureName = 'My Custom Signature' ## Local signature name
Function Set-DocText($WordObject, $FindText, $ReplaceWith)
{
$replaceAll = 2
$findContinue = 1
$matchCase = $false
$matchWholeWord = $true
$matchWildcards = $false
$matchSoundsLike = $false
$matchAllWordForms = $false
$forward = $true
$wrap = 1
$format = $false
[void]$WordObject.Selection.Find.Execute($FindText, $matchCase, $matchWholeWord, $matchWildcards, $matchSoundsLike, $matchAllWordForms, $forward, $wrap, $format, $ReplaceWith, $replaceAll)
}
#Get Active Directory information for current user
$sysInfo = New-Object -ComObject 'ADSystemInfo'
$userDN = $sysInfo.GetType().InvokeMember('UserName', 'GetProperty', $null, $sysInfo, $null)
$adUser = [ADSI]"LDAP://$($userDN)"
[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($sysInfo)
$signatureDir = "${ENV:AppData}\Microsoft\Signatures"
$signatureFile = "$($signatureDir)\$($signatureName).docx"
If (Test-Path -Path $signatureDir -PathType Leaf) { Remove-Item -Path $signatureDir -Force }
If (-not (Test-Path -Path $signatureDir)) { New-Item -Path $signatureDir -Itemtype Directory -Force | Out-Null }
Copy-Item -Path $(If ($ADUser.mobile) { $templateFile }
Else { $templateFileNoMobile }) -Destination $signatureFile -Force
$msWord = New-Object -ComObject Word.Application
[void]$msWord.Documents.Open($signatureFile)
Set-DocText -WordObject $msWord -FindText 'AD_DisplayName' -ReplaceWith $ADUser.DisplayName.ToString()
Set-DocText -WordObject $msWord -FindText 'AD_Title' -ReplaceWith $ADUser.title.ToString()
Set-DocText -WordObject $msWord -FindText 'AD_Telephone' -ReplaceWith $(If ($ADUser.telephoneNumber) { $ADUser.telephoneNumber.ToString() }
Else { $defaultTelephone })
If ($ADUser.mobile) { Set-DocText -WordObject $msWord -FindText 'AD_MobilePhone' -ReplaceWith $ADUser.mobile.ToString() }
# Save Signature files in multiple extensions (needed for Outlook)
$saveAs = @{
'htm' = 'wdFormatHTML'
'rtf' = 'wdFormatRTF'
'txt' = 'wdFormatText'
}
ForEach ($ext In $saveAs.Keys)
{
$saveFormat = [Microsoft.Office.Interop.Word.WdSaveFormat]::($saveAs[$ext])
$path = $signatureFile -replace 'docx$', $ext
$msWord.ActiveDocument.SaveAs([ref]$path, [ref]$saveFormat)
}
$msWord.ActiveDocument.Close()
# Set signature as default
$emailOptions = $msWord.EmailOptions
$emailSignature = $emailOptions.EmailSignature
$emailSignatureEntries = $emailSignature.EmailSignatureEntries
$emailSignature.NewMessageSignature = $signatureName
$emailSignature.ReplyMessageSignature = $signatureName
$msWord.Quit()
[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($msWord)
This is the content of the MS_Word document:
Code: Select all
$ADDisplayName
$ADTitle
$ADTelephoneNumber
$ADMobilePhone
1. Recreate the MS Word .DOCX template manually, even without any display image - FAILED, the Firstname.LastName@domain.com is displayed.
2. Run the Set-EmailSignature.PS1 manually - WORKS perfectly fine, but this needs to be executed as Login Script.
3. Running it as Login Script User Configuration (Enabled) > Policies > Windows Settings > Scripts > Logon - FAILED, the Firstname.LastName@domain.com is displayed.
Any help would be greatly appreciated.