#variables
$SignatureName = 'Signature'
$SigSource = '\\location\Signature.docx'
$SignatureVersion = "1.0"
$ForceSignature = '0' #'0' = editable ; '1' non-editable and forced.
#Environment variables
$AppData=$env:appdata
$SigPath = '\Microsoft\Signatures'
$LocalSignaturePath = $AppData+$SigPath
$RemoteSignaturePathFull = $SigSource
#Copy file
If (!(Test-Path -Path $LocalSignaturePath\$SignatureVersion))
{
New-Item -Path $LocalSignaturePath\$SignatureVersion -Type Directory
}
Elseif (Test-Path -Path $LocalSignaturePath\$SignatureVersion)
{
Write-Host "Signature already exists, Script will now exit..." -ForegroundColor Yellow
break
}
#Check signature path
if (!(Test-Path -path $LocalSignaturePath)) {
New-Item $LocalSignaturePath -Type Directory
}
#Get Active Directory information for logged in user
$UserName = $env:username
$Filter = "(&(objectCategory=User)(samAccountName=$UserName))"
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.Filter = $Filter
$ADUserPath = $Searcher.FindOne()
$ADUser = $ADUserPath.GetDirectoryEntry()
#Copy signature templates from domain to local Signature-folder
Write-Host "Copying Signatures" -ForegroundColor Green
Copy-Item "$Sigsource" "$LocalSignaturePath\$SignatureName.docx" -Force
#Insert variables from Active Directory to rtf signature-file
$MSWord = New-Object -ComObject word.application
$fullPath = "$LocalSignaturePath\$SignatureName.docx"
$MSWord.Documents.Open($fullPath)
#$MSWord.visible = $true
#User Name $ Designation
Function Update-Sig
{
Param($attribute,$value)
$ReplaceAll = 2 ;$FindContinue = 1;$MatchCase = $False;$MatchWholeWord = $True;$MatchWildcards = $False;$MatchSoundsLike = $False;$MatchAllWordForms = $False
$Forward = $True;$Wrap = $FindContinue;$Format = $False
$FindText = $attribute
$ReplaceText = $value
Write-Host "Finding $attribute ....." -ForegroundColor Green -NoNewline
Write-Host "Replacing with value:$value" -ForegroundColor yellow
$MSWord.Selection.Find.Execute($FindText, $True, $MatchWholeWord, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms, $Forward, $Wrap, $Format, $ReplaceText, $ReplaceAll )
if ($MSWord.Selection.Find.Execute($ReplaceText.ToString())) {
$MSWord.ActiveDocument.Hyperlinks.Add($MSWord.Selection.Range, "mailto:"+$ReplaceText.ToString(), $missing, $missing, $ReplaceText.ToString()) | Out-Null
} }
Update-Sig -attribute "mail" -value "$([string]($ADUser.mail))"
Update-Sig -attribute "DisplayName" -value "$([string]($ADUser.displayName))"
Update-Sig -attribute "title" -value "$([string]($ADUser.title))"
Update-Sig -attribute "TelephoneNumber" -value "$([string]($ADUser.TelephoneNumber))"
#Save new message signature
#Save HTML
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatHTML");
$path = $LocalSignaturePath+'\'+$SignatureName+".htm"
$MSWord.ActiveDocument.saveas([ref]$path, [ref]$saveFormat)
#Save RTF
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatRTF");
$path = $LocalSignaturePath+'\'+$SignatureName+".rtf"
$MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$saveFormat)
#Save TXT
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatText");
$path = $LocalSignaturePath+'\'+$SignatureName+".txt"
$MSWord.ActiveDocument.SaveAs([ref] $path, [ref]$SaveFormat) | Out-Null
$MSWord.ActiveDocument.Close()
$MSWord.Quit()
#Office 2016
If (Test-Path HKCU:'\Software\Microsoft\Office\15.0')
{
Write-host "Setting signature for Office 2016"-ForegroundColor Green
If ($ForceSignature -eq '0')
{
Write-host "Setting signature for Office 2016 as available" -ForegroundColor Green
$MSWord = New-Object -comobject word.application
$EmailOptions = $MSWord.EmailOptions
$EmailSignature = $EmailOptions.EmailSignature
$EmailSignatureEntries = $EmailSignature.EmailSignatureEntries
$EmailSignature.NewMessageSignature="$SignatureName"
$EmailSignature.ReplyMessageSignature="$SignatureName"
}
If ($ForceSignature -eq '1')
{
Write-Host "Setting signature for Office 2016 "
If (!(Get-ItemProperty -Name 'NewSignature' -Path HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -ErrorAction SilentlyContinue))
{
New-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'NewSignature' -Value $SignatureName -PropertyType 'String' -Force
}
If (!(Get-ItemProperty -Name 'ReplySignature' -Path HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -ErrorAction SilentlyContinue))
{
New-ItemProperty HKCU:'\Software\Microsoft\Office\15.0\Common\MailSettings' -Name 'ReplySignature' -Value $SignatureName -PropertyType 'String' -Force
}
}
}
Automatically generates an Outlook Signature based on a User's AD data.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.