Generete Outlook Signature with data from AD.
Run as logonscript.
For test run: .\Script.ps1 Initials
###############################################################################
# Author: Kasper Lundgaard Christensen - TODO-IT
# Date: 01-04-2024
# Description: Outlook Signature with function to overwrite user for testing purpose
###############################################################################
# Getting Active Directory information for current user
$user = (([adsisearcher]"(&(objectCategory=User)(samaccountname=$env:username))").FindOne().Properties)
$UserOverwrite = $args[0]
if($UserOverwrite)
{
$user = (([adsisearcher]"(&(objectCategory=User)(samaccountname=$UserOverwrite))").FindOne().Properties)
write-host $UserOverwrite
}
# If the user is not found in Active Directory exit the script
if(!$user) {
exit
}
# Create the signatures folder and sets the name of the signature file
$folderlocation = $Env:appdata + '\\Microsoft\\signatures'
$filename = "AD Signature"
$file = "$folderLocation\\$filename"
# If the folder does not exist create it
if(!(Test-Path -Path $folderlocation )){
New-Item -ItemType directory -Path $folderlocation
}
# Company name and logo
$companyName = "Compant Name"
$logo = "Logo link"
$logoURL = "Logo URL"
$CompanyPhone = "Phone Number"
$slogan_1 = "Slogan"
$considerprint = "Please consider the enviroment before printing this email"
# Get the users properties (These should always be in Active Directory and Unique)
if($user.name.count -gt 0){$displayName = $user.name[0]} # Display Name
if($user.title.count -gt 0){$jobTitle = $user.title[0]} # Job Title
if($user.mail.count -gt 0){$email = $user.mail[0]} # Email Address
if($user.company.count -gt 0){$usercompany = $user.company[0]} # User Company
# write-host $jobTitle
# write-host $usercompany
if($user.mobile.count -gt 0){$mobileNumber = $user.mobile[0]} # Mobile number
if($user.homephone.count -gt 0){$directDial = $user.homephone[0]} # Home Nnumber
if($user.telephonenumber.count -gt 0){$telephone = $user.telephonenumber[0]} # Office number
if($user.wwwhomepage.count -gt 0){$website = $user.wwwhomepage[0]} # Webpage address
# Address
if($user.postofficebox.count -gt 0){$poBox = $user.postofficebox[0]} # PO box number
if($user.physicaldeliveryofficename.count -gt 0){$office = $user.physicaldeliveryofficename} # Office Name
if($user.streetaddress.count -gt 0){$street = $user.streetaddress[0]} # Street address
if($user.l.count -gt 0){$city = $user.l[0]} # City
if($user.st.count -gt 0){$state = $user.st[0]} # State
if($user.postalcode.count -gt 0){$zipCode = $user.postalcode[0]} # Post code / Zip code
# Group Check
$Group = [ADSI]"LDAP://CN=Signature_CompanyLogo,OU=Signature,OU=Security Groups,OU=Users,DC=contoso,DC=dk"
$Group.Member | ForEach-Object {
if ($user.distinguishedname -match $_) {
$logo = "Logo Link"
}
}
# Building Style Sheet
$style =
@"
<style>
p, table, td, tr, a, span {
font-family: Calibri, Helvetica, sans-serif;
font-size: 11pt;
color: #000000;
}
span.blue
{
color: #000000;
}
table {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
}
hr {
border: none;
height: 1px;
background-color: #000000;
color: #000000;
width: 700px;
}
Contact{
width: 150px;
}
table.main {
}
</style>
"@
# Building HTML
$signature =
@"
$("<span>Med venlig hilsen/Kind regards/Mit freundlichen Grüssen</span><br />")
$(if($displayName){"<span><b>"+$displayName+"</b></span><br />"})
$(if($jobTitle){"<span>"+$jobTitle+"</span><br /><br />"})
<p>
<table class='main'>
<tr>
<td colspan='2' style='padding-right: 75px;'>$(if($logo){"<a href='$logoURL'><img src='$logo' /></a>"})</td>
</tr>
<tr>
<table>
$(if($telephone){"<tr><td class='Contact'>Hovednummer: </td><td><a href='tel:$CompanyPhone'>$CompanyPhone</a></td></tr>"})
$(if($mobileNumber){"<tr><td class='Contact'>Mobil: </td><td><a href='tel:+45 $mobileNumber'>+45 $($mobileNumber)</a></td></tr>"})
$(if($email){"<tr><td class='Contact'>Email: </td><td><a href='mailto:$email'>$($email)</a></td></tr>"})
$(if($website){"<tr><td class='Contact'>Website: </td><td><a href='https://$website'>$($website)</a></td></tr>"})
</table>
<table>
<tr>
<br/>
<br/>
<br/>
</tr>
<tr><td colspan='2' style='padding-bottom: 10px;'>
$(if($companyName){ "<b>"+$companyName+"</b><br/>" })
$(if($street){ $street+"<br/>" })
$(if($zipCode){ $zipCode+" " })
$(if($city){ $city })
</td></tr>
<tr>
<td colspan='2'>
<p>$slogan_1</p>
</td>
</tr>
</table>
</tr>
</table>
</p>
<br />
"@
# Save the HTML to the signature file
$style + $signature | out-file "$file.htm" -encoding UTF8
# Build the txt version for none rich text emails
$signature =
@"
Med venlig hilsen/Kind regards/Mit freundlichen Grüssen
$(if($displayName){ $displayName })
$(if($jobTitle){ $jobTitle })
$(if($telephone){"Hovednummer: "+$CompanyPhone})
$(if($mobileNumber){"Mobil: +45 "+$mobileNumber})
$(if($email){"Email: "+$email})
$(if($companyName){ $companyName })
$(if($street){ $street })
$(if($zipCode){ $zipCode }) $(if($city){ $city })
"@
# Output the text to the signatures folder
$signature | out-file "$file.txt" -encoding Default
# 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
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
}