forked from raymix/PowerShell-Outlook-Signatures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Outlook Signatures.ps1
205 lines (162 loc) · 9.41 KB
/
Outlook Signatures.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Written by Raimonds Virtoss @ https://github.com/raymix
### Signature location ###
$UNC = "\\FILESERVER\Outlook Signatures"
# Define DATA folder on network share
$DATA = Join-Path $UNC "DATA"
# Define local path to Outlook signatures
$signaturePath = Join-Path $env:APPDATA "Microsoft\Signatures"
# Create signatures folder if doesn't exist yet
if (!(Test-Path $signaturePath)) {New-Item -Path $signaturePath -ItemType Directory}
### AD STUFF ###
# Get current user's details from Active Directory using ADSI
$adsi = [adsisearcher]"(samaccountname=$env:USERNAME)"
$userADProperties = $adsi.FindOne().Properties
$fullName = $userADProperties.displayname
$jobTitle = $userADProperties.title
$mobile = $userADProperties.mobile
$telephone = $userADProperties.telephonenumber
$email = $userADProperties.mail
$SAM = $userADProperties.samaccountname
$DN = $userADProperties.distinguishedname
# You can override above here if you need to do some testing
#$DN = "CN=USERNAME,OU=Users,OU=Location1,OU=Site1,DC=DOMAIN,DC=local"
#$SAM = "USERNAME"
#Allows more dynamic choice by using $phones, best used for users with no phones as it will return empty.
$phones = ""
if ($telephone) { $phones += "<span style='color:#215732;'><strong>T:</strong></span> <a href='tel:#telephone' style='color:#215732;'>$telephone</a>" }
if ($telephone -and $mobile) {$phones += " <span style='color: #BA0C2F;'>|</span> "}
if ($mobile) {$phones += "<span style='color:#215732;'><strong>M:</strong></span> <a href='tel:#mobile' style='color:#215732;'>$mobile</a>"} #"<strong>M:</strong> $($mobile)" }
$templatePath = $UNC
# Find which OU user belongs to and which template folder to read from on FILESERVER
# This is where you will need to do some modifications depending on your environment
# This part extracts LocationX and SiteX from Distinguished Name
$OU = $DN -replace "CN=$($SAM),","" -replace ",DC=DOMAIN,DC=local","" -replace "OU=Users,","" -replace "OU=","" -split ","
# Now reverse the resulted array
$OU = ($OU[($OU.Length-1)..0])
# Take UNC path and add above reversed result to form full path to locate signature files on network share
$($OU | % {$templatePath = Join-Path $templatePath $_})
### LOCAL STUFF ###
#Load existing templates and read hashes from first line
$localHTM = Get-ChildItem $signaturePath -Filter "*.htm"
$localSignatures = $null
$localSignatures = @()
foreach ($htm in $localHTM) {
$lastLine = ((Get-Content $htm.FullName -Last 1) -replace "<!-- ","" -replace " -->","").Split(",") #<!-- MD5=XXXXXXXXX -->
$localSignatures +=, [pscustomobject]@{
Name=$htm.Name
Base=$htm.BaseName
Path=$htm.Directory
SAM=$lastLine[0]
jobTitle=$lastLine[1]
mobile=$lastLine[2]
telephone=$lastLine[3]
email=$lastLine[4]
MD5=$(if ($lastLine -match $SAM) { $lastLine[5] } else { "N/A" })
}
}
#Windows bubble message
function Send-Notification($title, $msg) {
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = $(Join-Path $UNC "DATA\balloon.ico")
$balloon.BalloonTipText = $msg
$balloon.BalloonTipTitle = $title
$balloon.Visible = $true
$balloon.ShowBalloonTip(10000)
$balloon.Dispose()
}
function Write-Signature($md5, $template) {
$HTM_files = $($template.BaseName + "_files")
$HTM_filesNoSpace = $HTM_files -replace " ","%20"
$localHTM_files = Join-Path $signaturePath $HTM_files
$localHTM_tmp = Join-Path $signaturePath $("tmp_" + $template.Name)
$localHTM_tmp2 = Join-Path $signaturePath $("tmp2_" + $template.Name)
$localHTM = Join-Path $signaturePath $template.Name
$localRTF = "$(Join-Path $signaturePath $template.BaseName).rtf"
$localTXT = "$(Join-Path $signaturePath $template.BaseName).txt"
$image = "$(Join-Path $template.Directory $template.BaseName).jpg"
if (Test-Path $localHTM_files) {
Remove-Item $localHTM_files -Recurse -Force
}
New-Item -Path $localHTM_files -ItemType Directory
if (Test-Path $image) {
Copy-Item -Path $image -Destination (Join-Path $localHTM_files "image001.jpg")
}
# Write HTM file, keep the string indented to left
"$((Get-Content $template.FullName -Raw) -replace "#fullName",$fullName -replace "#jobTitle",$jobTitle -replace "#phones",$phones -replace "#mobile",$mobile -replace "#telephone",$telephone -replace "#email",$email -replace "#head",'' -replace "#folder",$HTM_filesNoSpace)" | Out-File $localHTM_tmp -Encoding utf8
# Convert HTM to RTF locally
$wrd = new-object -com word.application
$wrd.visible = $false
$doc = $wrd.documents.open($localHTM_tmp) # needs unused var defined
$opt = 6
$wrd.ActiveDocument.Saveas([ref]$localRTF,[ref]$opt)
$wrd.Quit()
# Convert HTM to TXT and strip all html stuff, tabulators and empty lines
$txt = Get-Content $localHTM_tmp
$txt = $txt -replace "<style.+\/style>","" # Remove styles
$txt = $txt -replace "<[^>]*>","" # HTML tags and comments
$txt = $txt -replace " "," " # HTML strong spaces character
$txt = $txt -replace "­"," " # HTML decimal char?
$txt = $txt.trim() # Tabulators
$txt = $txt | ? {$_.trim() -ne "" } # Empty line breaks
$txt | Out-File $localTXT
# Write HTM file, keep the string indented to left
"$((Get-Content $template.FullName -Raw) -replace "#fullName",$fullName -replace "#jobTitle",$jobTitle -replace "#phones",$phones -replace "#mobile",$mobile -replace "#telephone",$telephone -replace "#email",$email -replace "#folder",$HTM_filesNoSpace)" | Out-File $localHTM_tmp2 -Encoding utf8
$localHTMFromTMP = Get-Content $localHTM_tmp2 -Raw
$xmlns = Get-Content (Join-Path $DATA "xmlns.htm") -Raw
$head = ((Get-Content (Join-Path $DATA "head.htm") -Raw) -replace "#folder",$HTM_filesNoSpace)
$filelist = ((Get-Content (Join-Path $DATA "filelist.xml") -Raw) -replace "#folder",$HTM_filesNoSpace -replace "#baseName","$($template.BaseName -replace " ","%20").htm")
$localHTMFromTMP = $localHTMFromTMP -replace "<!DOCTYPE html>",$xmlns
$localHTMFromTMP = $localHTMFromTMP -replace "#head",$head
$localHTMFromTMP | Out-File $localHTM
# Add n HTML comment at the last line of htm file, this is used to check for changes in AD and template file (md5 file checksum) later
Add-Content $localHTM "<!-- $SAM,$jobTitle,$mobile,$telephone,$email,$md5 -->"
$filelist | Out-File (Join-Path $localHTM_files "filelist.xml")
Copy-Item -Path (Join-Path $DATA "colorschememapping.xml") -Destination $localHTM_files
Copy-Item -Path (Join-Path $DATA "themedata.thmx") -Destination $localHTM_files
Remove-Item $localHTM_tmp -Force
Remove-Item $localHTM_tmp2 -Force
}
function Commit-Signatures($templates) {
foreach ($template in $templates) {
$md5 = (Get-FileHash $template.FullName -Algorithm MD5).Hash
if (Test-Path (Join-Path $signaturePath $template.Name)) { # If template file exists
# Check md5 in hashtables to see if signature is updated and needs replacing
if ($md5 -notin $localSignatures.md5) {
Write-Signature $md5 $template
Send-Notification "Company Signature Updated" "Outlook signature [$($template.BaseName)] has been updated"
} else {
$findChanged = $localSignatures | Where-Object {$_.Name -match $template.Name}
if (($findChanged.SAM -ne $SAM) -or ($findChanged.jobTitle -ne $jobTitle) -or ($findChanged.mobile -ne $mobile) -or ($findChanged.telephone -ne $telephone) -or ($findChanged.email -ne $email)) {
Write-Signature $md5 $template
Send-Notification "Company Signature Details Updated" "Outlook signature [$($template.BaseName)] has been updated to reflect changes of your profile in Active Directory (such as name, email, phone or mobile number)"
}
}
} else { # Template file does not exist
Write-Signature $md5 $template
Send-Notification "Company Signature Added" "A new Outlook signature [$($template.BaseName)] has been added to your Outlook."
}
}
}
# I named variable country templates because our OUs are based on countries. In this public version this variable is for LocationX
$countryTemplates = Get-ChildItem $templatePath -Filter "*.htm"
$defaultTemplates = Get-ChildItem $UNC -Filter "*.htm"
foreach ($ls in $localSignatures) {
if ($ls.MD5 -ne "N/A") {
if ((($ls.Base -notin $countryTemplates.BaseName) -and $countryTemplates.count -gt 0) -or (($ls.Base -in $defaultTemplates.BaseName)-and $countryTemplates.count -gt 0) -or (($ls.Base -notin $defaultTemplates.BaseName)-and $countryTemplates.count -eq 0)){
Remove-Item "$(Join-Path $ls.Path $ls.Base).htm" -Force -ErrorAction SilentlyContinue
Remove-Item "$(Join-Path $ls.Path $ls.Base).rtf" -Force -ErrorAction SilentlyContinue
Remove-Item "$(Join-Path $ls.Path $ls.Base).txt" -Force -ErrorAction SilentlyContinue
Remove-Item "$(Join-Path $ls.Path $ls.Base)_files" -Recurse -Force -ErrorAction SilentlyContinue
Send-Notification "Company Signature Removed" "An Outlook signature [$($ls.Base)] has been removed from your Outlook."
}
}
}
if ($countryTemplates.count -eq 0) {
if ($defaultTemplates.count -gt 0) {
Commit-Signatures $defaultTemplates
}
} else {
Commit-Signatures $countryTemplates
}