-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomateSignatureInOutlook.ps1
More file actions
761 lines (725 loc) · 30.2 KB
/
AutomateSignatureInOutlook.ps1
File metadata and controls
761 lines (725 loc) · 30.2 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
<#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Name: Automate Email Signature Setup in Outlook Using PowerShell
Version: 2.0
Website: o365reports.com
~~~~~~~~~~~~~~~~~
Script Highlights:
~~~~~~~~~~~~~~~~~
1. The script automatically verifies and installs the Exchange PowerShell module (if not installed already) upon your confirmation.
2. Automates email signature deployment and makes it scheduler friendly.
3. Provides the option to create default or custom text signatures.
4. Provides the option to configure default or custom signatures using HTML templates.
5. Automates email signature setup for all mailboxes or bulk mailboxes.
6. Automates email signature configuration for user mailboxes alone.
7. Exports signature deployment status to a CSV file.
8. Supports certificate-based authentication (CBA) too.
For detailed script execution: https://o365reports.com/2024/07/10/automate-email-signature-setup-in-outlook-using-powershell/
Change Log:
~~~~~~~~~~~
v1.0 (July 10, 2024)- Script created
V2.0 (Jan 18, 2025)- Error handling added to enabling PostponeRoamingSignatureUntilLater param.
------------------------------------------------------------------------------------------------------------#>
#Block for passing params
[CmdletBinding(DefaultParameterSetName = 'NoParams')]
param
(
[Parameter()]
[string]$Organization,
[Parameter()]
[string]$ClientId,
[Parameter()]
[string]$CertificateThumbprint,
[Parameter()]
[string]$UserPrincipalName,
[Parameter()]
[string]$Password,
[Parameter()]
[switch]$TaskScheduler
)
#--------------------------------------Block For Module Availability Verification and Installation--------------------------------------------
$Module = (Get-Module ExchangeOnlineManagement -ListAvailable)
if ($Module.count -eq 0)
{
Write-Host `n`Exchange Online PowerShell module is not available
$Confirm = Read-Host `n`Are you sure you want to install module? [Y] Yes [N] No
if ($Confirm -match "[yY]")
{
Write-Host "`n`Installing Exchange Online PowerShell module" -ForegroundColor Red
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
Import-Module ExchangeOnlineManagement
}
else
{
Write-Host `n`EXO module is required to connect Exchange Online. Please install module using Install-Module ExchangeOnlineManagement cmdlet. -ForegroundColor Yellow
exit
}
}
#-------------------------------------------------------------------------Block For Connecting to Exchangeonline ---------------------------------------------------------------------------
if($TaskScheduler)
{
Write-Host "Setup email signature in Outlook - Scheduled task started" -ForegroundColor Cyan
}
Write-Host `n`Connecting to Exchange Online... -ForegroundColor Green
if ($UserPrincipalName -ne "" -and $Password -ne "")
{
$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential ($UserPrincipalName,$SecurePassword)
Connect-ExchangeOnline -Credential $UserCredential -ShowBanner:$false
}
elseif ($Organization -ne "" -and $ClientId -ne "" -and $CertificateThumbprint -ne "")
{
Connect-ExchangeOnline -AppId $ClientId -CertificateThumbprint $CertificateThumbprint -Organization $Organization -ShowBanner:$false
}
elseif (-not $TaskScheduler)
{
Connect-ExchangeOnline -ShowBanner:$false
}
else
{
Write-Host "`n`The script will exit as the Required parameters are not included." -ForegroundColor Red
exit
}
$InputsFolderPath = Join-Path $PSScriptRoot -ChildPath "StoredUserInputs.csv"
if (-not $TaskScheduler)
{
if ((Test-Path -Path $InputsFolderPath))
{
Remove-Item -Path $InputsFolderPath
}
}
else
{
$Inputs = Import-Csv -Path $InputsFolderPath
$Index = 0
}
#------------------------Block for getting the Confirmation from user to enable the PostponeRoamingSignaturesUntilLater parameter if not already enabled------------------------------------
if ((-not (Get-OrganizationConfig).PostponeRoamingSignaturesUntilLater) -and (-not $TaskScheduler))
{
if (-not $Enable_PostponeRoamingSignatureUntilLater)
{
Write-Host "`n`To add a signature for users, first enable 'PostponeRoamingSignatureUntilLater' in the organization's settings." -ForegroundColor Yellow
Write-Host "`n`1. Enable it." -ForegroundColor Cyan
Write-Host "`n`2. Continue without enabling." -ForegroundColor Cyan
$UserConfirmation = Read-Host "`n`Enter Your choice"
}
while ($true)
{
if ($Enable_PostponeRoamingSignatureUntilLater -or $UserConfirmation -eq 1)
{
Set-OrganizationConfig -PostponeRoamingSignaturesUntilLater $true
if($?)
{
Write-Host "`n`PostponeRoamingSignatureUntilLater parameter enabled" -ForegroundColor Green
break;
}
else
{
Write-Host "Error occurred. Unable to enable PostPoneRoamingSignaturesUntilLater.Please try again" -ForegroundColor Red
Exit;
}
}
elseif ($UserConfirmation -eq 2)
{
Write-Host '`n`Without Enabling it, Signature can be added but not deployed to Users MailBox' -ForegroundColor Yellow
break;
}
else
{
Write-Host "`n`Enter the correct input" -ForegroundColor Red
$UserConfirmation = Read-Host
continue;
}
}
}
elseif (-not $TaskScheduler)
{
Write-Host "`n`PostponeRoamingSignatureUntilLater parameter already enabled" -ForegroundColor Green
$UserConfirmation = 1
}
#--------------------------------------------Function to preview the HTML Signaute in your browser and get conformation to use that HTML template--------------------------------------------
function Preview-Signature ($FilePath)
{
$FileExtension = [System.IO.Path]::GetExtension($FilePath)
if ($FileExtension -eq ".html" -or $FileExtension -eq ".htm")
{
$HTMLFilePath = $FilePath
}
else
{
Write-Host "`n`The script will terminate as the file isn't in HTML format" -ForegroundColor Red
Disconnect-ExchangeOnline -Confirm:$false
exit
}
$Title = "Confirmation"
$Question = "Do you want to preview the HTML Signature?"
$Choices = "&Yes","&No"
$Decision = $Host.UI.PromptForChoice($Title,$Question,$Choices,1)
if ($Decision -eq 0) {
Start-Process $HTMLFilePath
}
Write-Host @"
`n`Are you sure to deploy the signature with this template? [Y] Yes [N] No
"@ -ForegroundColor Cyan
$UserChoice = Read-Host "`n`Enter your choice"
while ($true)
{
if ($UserChoice -match "[Y]")
{
break
}
elseif ($UserChoice -match "[N]")
{
Write-Host "`n`Exiting the script..."
Disconnect-ExchangeOnline
exit
}
else
{
Write-Host "`n`Enter the correct input" -ForegroundColor Red
$UserChoice = Read-Host
}
}
return
}
#---------------------------------------------function to get the users to assign an signature------------------------------------------------
function Get-UsersForAssignSignature
{
if (-not $TaskScheduler)
{
Write-Host "`n`User-Based Email Signature Deployment:" -ForegroundColor Yellow
Write-Host @"
`n`1. Assign signature to all mailboxs.
`n`2. Assign signature to specific mailboxs (Import CSV).
`n`3. Assign signature to user mailboxs.
"@ -ForegroundColor Cyan
$ImportUsersType = Read-Host "`n`Enter your choice"
}
else
{
$ImportUsersType = $Inputs[$Index++].Input
}
while ($true)
{
if (($ImportUsersType -eq 1 -or $ImportUsersType -eq 2 -or $ImportUsersType -eq 3) -and (-not $TaskScheduler))
{
$ImportUsersType | Select-Object @{ Name = 'Input'; Expression = { $_ } } | Export-Csv -Path $InputsFolderPath -NoTypeInformation -Append
}
if ($ImportUsersType -eq 1)
{
$UsersCollection = Get-EXOMailbox -ResultSize Unlimited | Select-Object UserPrincipalName | Sort-Object -Property UserPrincipalName -Unique -ErrorAction Stop
$Output = $Output + "to $($UsersCollection.count) Users in the tenant..."
Write-Host $Output
}
elseif ($ImportUsersType -eq 2)
{
if (-not $TaskScheduler)
{
Write-Host "`n`Enter the csv file Path without quotation (Eg: C:\UsersCollection.csv)" -ForegroundColor Cyan
$Path = Read-Host
}
else
{
$Path = $Inputs[$Index++].Input
}
while ($true)
{
try
{
$Headers = (Import-Csv -Path $Path | Get-Member -MemberType NoteProperty).Name
$UsersCollection = Import-Csv -Path $Path | Select-Object -Property UserPrincipalName | Sort-Object -Property UserPrincipalName -Unique -ErrorAction Stop
if ('UserPrincipalName' -in $Headers -and $UsersCollection.count -ge 1)
{
if (-not $TaskScheduler)
{
$Path | Select-Object @{ Name = 'Input'; Expression = { $_ } } | Export-Csv -Path $InputsFolderPath -NoTypeInformation -Append
}
$Output = $Output + "to $($UsersCollection.count) users in the CSV file..."
Write-Host $Output
break;
}
else
{
Write-Host "`n`The file is empty or does not contain the UserPrincipalName column." -ForegroundColor Red
$Path = Read-Host "`n`Enter correct file path"
continue
}
}
catch
{
Write-Host "`n`You have entered an invalid Path. Enter the correct Path (Eg: C:\UsersCollection.csv)" -ForegroundColor Red
$Path = Read-Host
}
}
}
elseif ($ImportUsersType -eq 3)
{
$UsersCollection = Get-EXOMailbox -RecipientTypeDetails UserMailBox -ResultSize Unlimited | Select-Object UserPrincipalName | Sort-Object -Property UserPrincipalName -Unique -ErrorAction Stop
$Output = $Output + "to $($UsersCollection.count) Users in the tenant..."
Write-Host $Output
}
else
{
Write-Host "`n`Enter the correct input : " -ForegroundColor Red
$ImportUsersType = Read-Host
continue;
}
return $UsersCollection
}
}
#-------------------------------------------------------------Function to get the content of the HTML with the specified Path----------------------------------------------------------
function Get-HTMLContent
{
Write-Host "`n`Enter the HTML file Path without quotation (eg: C:\HTMLfile.html)" -ForegroundColor Cyan
$Path = Read-Host
while ($true)
{
try
{
$HTMLcontent = Get-Content -Path $Path -Raw -ErrorAction Stop
if ($HTMLcontent.Length -eq 0)
{
Write-Host '`n`You have Provided an empty file' -ForegroundColor Red
$Path = Read-Host '`n`Enter the valid HTML file Path'
continue
}
break;
}
catch
{
Write-Host "`n`You have entered an invalid Path. Enter the correct Path (eg: C:\HTMLfile.html)" -ForegroundColor Red
$Path = Read-Host
}
}
Preview-Signature ($Path)
return [string]$HTMLcontent
}
#-------------------------------------------------------------Function to get the user required fields for custom template----------------------------------------------------------
function Get-RequiredFieldsFromUser
{
Write-Host @"
`n`You can set up signature using the below fields. If a field has a value in the admin center, that value will be included in the signature.
`n`1. Display Name
`n`2. Email Address
`n`3. Mobile Phone
`n`4. Business Phone
`n`5. Fax
`n`6. Department
`n`7. Title
`n`8. Office
`n`9. Address (Street Address, City, State or Province, Zip Code, Country or Region)
`n`10. StreetAddress
`n`11. City
`n`12. StateOrProvince
`n`13. PostalCode
`n`14. CountryOrRegion
"@ -ForegroundColor Yellow
if ($UserChoice -eq 3)
{
Write-Host "`n`Enter the field numbers from the list above that you would like to include in the text signature.(Eg: 1,5,6,14)" -ForegroundColor Cyan
}
else
{
Write-Host "`n`Provide the field numbers that need to be change in your HTML signature with users information present in admin center (Eg: 1,5,6,14)" -ForegroundColor Cyan
}
$FieldNumbers = Read-Host "`n`Enter Your choice"
if (-not $TaskScheduler -and $UserChoice -eq 3)
{
$FieldNumbers | Select-Object @{ Name = 'Input'; Expression = { $_ } } | Export-Csv -Path $InputsFolderPath -NoTypeInformation -Append
}
$Numbers = $FieldNumbers -split ','
return $Numbers
}
#-----------------------------------------------------------------Function to get the user address ----------------------------------------------------------------------
function Generate-UserAddress ($UserDetails)
{
if ($UserDetails.StreetAddress)
{
$Address += $UserDetails.StreetAddress + ", "
}
if ($UserDetails.City)
{
$Address += $UserDetails.City + ", "
}
if ($UserDetails.StateOrProvince)
{
$Address += $UserDetails.StateOrProvince + ",<br>"
}
if ($UserDetails.PostalCode)
{
$Address += $UserDetails.PostalCode + ", "
}
if ($UserDetails.CountryOrRegion)
{
$Address += $UserDetails.CountryOrRegion + "."
}
return $Address
}
#---------------------------------------------------------------Function to generate and Assign default text Signature to the users-------------------------------------------------------------------
function Deploy-DefaultTextSignature
{
$UsersCollection = Get-UsersForAssignSignature
$Count = 1
$TotalCount = $UsersCollection.count
$CurrentTime = Get-Date -Format "yyyyMMdd_HHmmss"
$SignatureLog_FilePath = Join-Path $PSScriptRoot -ChildPath "$CurrentTime-SignatureDeployment_Details.csv"
foreach ($User in $UsersCollection)
{
try {
$UserDetails = Get-User -Identity $User.UserPrincipalName -ErrorAction Stop
if ($UserDetails.DisplayName -ne "")
{
$UserTextSignature += "$($UserDetails.DisplayName)<br>"
}
if ($UserDetails.UserPrincipalName -ne "")
{
$UserTextSignature += "$($UserDetails.UserPrincipalName)<br>"
}
if ($UserDetails.Title -ne "")
{
$UserTextSignature += "$($UserDetails.Title)<br>"
}
if ($UserDetails.MobilePhone -ne "")
{
$UserTextSignature += "$($UserDetails.MobilePhone)<br>"
}
if ($UserDetails.Phone)
{
$UserTextSignature += "$($UserDetails.Phone)<br>"
}
$UserTextSignature += Generate-UserAddress ($UserDetails)
Set-MailboxMessageConfiguration -Identity $UserDetails.UserPrincipalName -SignatureHTML $UserTextSignature -AutoAddSignature $true -AutoAddSignatureOnMobile $true -AutoAddSignatureOnReply $true -ErrorAction Stop
$DeploymentStatus = "Successful"
}
catch
{
$ErrorMessage = $_.Exception.Message
$DeploymentStatus = "Unsuccessful : $ErrorMessage "
Write-Host "`n`Signature deployment for $($User.UserPrincipalName) is Unsucessful -Error Occured" -ForegroundColor Red
}
$AuditData = [pscustomobject]@{ UserPrincipalName = $User.UserPrincipalName; DeploymentStatus = $DeploymentStatus }
$AuditData | Export-Csv -Path $SignatureLog_FilePath -NoTypeInformation -Append
Write-Progress -Activity "Deploying Signature : $($Count) of $($TotalCount) | Current User : $($User.UserPrincipalName)"
$Count++
$UserTextSignature = ""
}
Disconnect_ExchangeOnline_Safely
}
#---------------------------------------------------------------Function to generate and Assign inbuild HTML Signature to the users-------------------------------------------------------------------
function Deploy-InbuiltHTMLSignature
{
if (-not $TaskScheduler)
{
$Filepath = Join-Path $PSScriptRoot "Build-InTemplate.html"
try
{
$DefaultHTML = Get-Content $Filepath -ErrorAction Stop
}
catch
{
Write-Host "`n`Inbuilt HTMLFile is not available in the current directory" -ForegroundColor Red
Write-Host "`n`Exiting the Script..."
Disconnect-ExchangeOnline
exit
}
Preview-Signature $Filepath
Write-Host "`n`You can include upcoming fields in the signature by entering a value or simply pressing Enter to skip." -ForegroundColor Yellow
$LogoLink = Read-Host "`n`Enter your company's logo link ( https:// )"
if ($LogoLink -eq "")
{
$DefaultHTML = $DefaultHTML.Replace('<img width="125px" src="https://cdn0.iconfinder.com/data/icons/logos-microsoft-office-365/128/Microsoft_Office-07-1024.png">','')
}
else
{
$DefaultHTML = $DefaultHTML.Replace('https://cdn0.iconfinder.com/data/icons/logos-microsoft-office-365/128/Microsoft_Office-07-1024.png',$LogoLink)
}
$WebLink = Read-Host "`n`Enter your company's website link ( https:// )"
if ($WebLink -eq "")
{
$DefaultHTML = $DefaultHTML.Replace('<a href="%%WebLink%%" target="_blank">%%WebLink%%</a>','')
}
else
{
$DefaultHTML = $DefaultHTML.Replace('%%WebLink%%',$WebLink)
}
$FaceBook = Read-Host "`n`Enter your company's FaceBook link ( https:// )"
if ($FaceBook -eq "")
{
$DefaultHTML = $DefaultHTML.Replace('<a href="%%FaceBook%%" target="_blank"><img style="height:22px; width:26px;" src="https://cdn0.iconfinder.com/data/icons/social-flat-rounded-rects/512/facebook-1024.png"></a>','')
}
else
{
$DefaultHTML = $DefaultHTML.Replace('%%FaceBook%%',$FaceBook)
}
$Twitter = Read-Host "`n`Enter your company's Twitter link ( https:// )"
if ($Twitter -eq "")
{
$DefaultHTML = $DefaultHTML.Replace('<a href="%%Twitter%%" target="_blank"><img style="height:22px; width:26px;" src="https://cdn2.iconfinder.com/data/icons/threads-by-instagram/24/x-logo-twitter-new-brand-contained-1024.png"></a>','')
}
else
{
$DefaultHTML = $DefaultHTML.Replace('%%Twitter%%',$Twitter)
}
$YouTube = Read-Host "`n`Enter your company's YouTube link ( https:// )"
if ($YouTube -eq "")
{
$DefaultHTML = $DefaultHTML.Replace('<a href="%%YouTube%%" target="_blank"><img style="height:22px; width:26px;" src="https://cdn0.iconfinder.com/data/icons/web-social-and-folder-icons/512/YouTube.png"></a>','')
}
else
{
$DefaultHTML = $DefaultHTML.Replace('%%YouTube%%',$YouTube)
}
$LinkedIN = Read-Host "`n`Enter your company's LinkedIn link ( https:// )"
if ($LinkedIN -eq "")
{
$DefaultHTML = $DefaultHTML.Replace('<a href="%%LinkedIN%%" target="_blank"><img style="height:22px; width:26px;" src="https://cdn2.iconfinder.com/data/icons/social-media-2285/512/1_Linkedin_unofficial_colored_svg-1024.png"></a>','')
}
else
{
$DefaultHTML = $DefaultHTML.Replace('%%LinkedIN%%',$LinkedIN)
}
$DisCord = Read-Host "`n`Enter your company's DisCord link ( https:// )"
if ($DisCord -eq "")
{
$DefaultHTML = $DefaultHTML.Replace('<a href="%%Discord%%" target="_blank"><img style="height:22px; width:26px;" src="https://cdn2.iconfinder.com/data/icons/gaming-platforms-squircle/250/discord_squircle-1024.png"></a>','')
}
else
{
$DefaultHTML = $DefaultHTML.Replace('%%DisCord%%',$DisCord)
}
$DefaultHTML = [string]$DefaultHTML
$DefaultHTML | Select-Object @{ Name = 'Input'; Expression = { $_ } } | Export-Csv -Path $InputsFolderPath -NoTypeInformation -Append
}
else
{
$DefaultHTML = $Inputs[$Index++].Input
}
$UsersCollection = Get-UsersForAssignSignature
$Count = 1
$TotalCount = $UsersCollection.count
$CurrentTime = Get-Date -Format "yyyyMMdd_HHmmss"
$SignatureLog_FilePath = Join-Path $PSScriptRoot -ChildPath "$CurrentTime-SignatureDeployment_Details.csv"
foreach ($User in $UsersCollection)
{
try
{
$UserDetails = Get-User -Identity $User.UserPrincipalName -ErrorAction Stop
$Address = Generate-UserAddress ($UserDetails)
$UserHTMLSignature = $DefaultHTML -replace "%%DisplayName%%",$UserDetails.DisplayName -replace "%%Title%%",$UserDetails.Title -replace "%%Email%%",$UserDetails.UserPrincipalName -replace "%%MobilePhone%%",$UserDetails.MobilePhone -replace "%%BusinessPhone%%",$UserDetails.Phone -replace "%%CompanyName%%",$UserDetails.Office -replace "%%Address%%",$Address
Set-MailboxMessageConfiguration -Identity $UserDetails.UserPrincipalName -SignatureHTML $UserHTMLSignature -AutoAddSignature $true -AutoAddSignatureOnMobile $true -AutoAddSignatureOnReply $true -ErrorAction Stop
$DeploymentStatus = "Successful"
}
catch
{
$ErrorMessage = $_.Exception.Message
$DeploymentStatus = "Unsuccessful : $ErrorMessage "
Write-Host "`n`Signature deployment for $($User.UserPrincipalName) is Unsucessful -Error Occured" -ForegroundColor Red
}
$AuditData = [pscustomobject]@{ UserPrincipalName = $User.UserPrincipalName; DeploymentStatus = $DeploymentStatus }
$AuditData | Export-Csv -Path $SignatureLog_FilePath -NoTypeInformation -Append
Write-Progress -Activity "Deploying Signature : $($Count) of $($TotalCount) | Current User : $($User.UserPrincipalName)"
$Count++
}
Disconnect_ExchangeOnline_Safely
}
#---------------------------------------------------------------Function to generate and Assign custom text Signature to the users-------------------------------------------------------------------
function Deploy-CustomTextSignature
{
if (-not $TaskScheduler)
{
$Numbers = Get-RequiredFieldsFromUser
}
else
{
$Numbers = $Inputs[$Index++].Input -split ','
}
$UsersCollection = Get-UsersForAssignSignature
$Count = 1
$TotalCount = $UsersCollection.count
$CurrentTime = Get-Date -Format "yyyyMMdd_HHmmss"
$SignatureLog_FilePath = Join-Path $PSScriptRoot -ChildPath "$CurrentTime-SignatureDeployment_Details.csv"
$UsersFields = @{ '1' = 'DisplayName'; '2' = 'UserPrincipalName'; '3' = 'MobilePhone'; '4' = 'Phone'; '5' = 'Fax'; '6' = 'Department'; '7' = 'Title'; '8' = 'Office'; '9' = 'Address'; '10' = 'StreetAddress'; '11' = 'City'; '12' = 'StateOrProvince'; '13' = 'PostalCode'; '14' = 'CountryOrRegion' }
foreach ($User in $UsersCollection)
{
try
{
$UserDetails = Get-User -Identity $User.UserPrincipalName -ErrorAction Stop
$UserTextSignature = ""
foreach ($Number in $Numbers) {
if ($UsersFields[$Number]) {
if ($Number -eq "9") {
$Address = Generate-UserAddress ($UserDetails)
if ($Address -ne "") {
$UserTextSignature += "$($Address)<br>"
}
}
elseif ($UserDetails.($UsersFields[$Number]) -ne "") {
$UserTextSignature += "$($UserDetails.($UsersFields[$Number]))<br>"
}
}
}
Set-MailboxMessageConfiguration -Identity $UserDetails.UserPrincipalName -SignatureHTML $UserTextSignature -AutoAddSignature $true -AutoAddSignatureOnMobile $true -AutoAddSignatureOnReply $true -ErrorAction Stop
$DeploymentStatus = "Successful"
}
catch
{
$ErrorMessage = $_.Exception.Message
$DeploymentStatus = "Unsuccessful : $ErrorMessage "
Write-Host "`n`Signature deployment for $($User.UserPrincipalName) is Unsucessful -Error Occured" -ForegroundColor Red
}
$AuditData = [pscustomobject]@{ UserPrincipalName = $User.UserPrincipalName; DeploymentStatus = $DeploymentStatus }
$AuditData | Export-Csv -Path $SignatureLog_FilePath -NoTypeInformation -Append
Write-Progress -Activity "Deploying Signature : $($Count) of $($TotalCount) | Current User : $($User.UserPrincipalName)"
$Count++
}
Disconnect_ExchangeOnline_Safely
}
#---------------------------------------------------------------Function to generate and Assign custom HTML Signature to the users-------------------------------------------------------------------
function Deploy-CustomHTMLSignature
{
if (-not $TaskScheduler)
{
$HTMLSignature = Get-HTMLContent
$Numbers = Get-RequiredFieldsFromUser
$AddressUsed = $false
$UsersFields = @{ '1' = 'DisplayName'; '2' = 'EmailAddress'; '3' = 'MobilePhone'; '4' = 'BussinessPhone'; '5' = 'FaxNumber'; '6' = 'Department'; '7' = 'Title'; '8' = 'Office'; '9' = 'Address'; '10' = 'StreetAddress'; '11' = 'City'; '12' = 'StateOrProvince'; '13' = 'PostalCode'; '14' = 'CountryOrRegion' }
Write-Host "`n`We need the current values of the following fields to update them with user-specific details." -ForegroundColor Yellow
foreach ($Number in $Numbers) {
if ($UsersFields[$Number])
{
Write-Host "`n`Enter the $($UsersFields[$Number]) as it appears in the provided HTML:" -ForegroundColor Cyan
while ($true)
{
$ValueInHTML = Read-Host
if ($ValueInHTML -ne "" )
{
break
}
else
{
Write-Host "`n`Enter the valid information" -ForegroundColor Red
}
}
if ($Number -eq '9')
{
$AddressUsed = $true
}
if ($ValueInHTML -match "(&|&)")
{
$ValueInHTML = $ValueInHTML -replace "(&|&)","&"
}
$HTMLSignature = $HTMLSignature -replace [regex]::Escape($ValueInHTML),"%%$($UsersFields[$Number])%%"
}
}
$HTMLSignature | Select-Object @{ Name = 'Input'; Expression = { $_ } } | Export-Csv -Path $InputsFolderPath -NoTypeInformation -Append
}
else
{
$HTMLSignature = $Inputs[$Index++].Input
if ($HTMLSignature.Contains('%%Address%%'))
{
$AddressUsed = $true
}
}
$UsersCollection = Get-UsersForAssignSignature
$Count = 1
$TotalCount = $UsersCollection.count
$CurrentTime = Get-Date -Format "yyyyMMdd_HHmmss"
$SignatureLog_FilePath = Join-Path $PSScriptRoot -ChildPath "$CurrentTime-SignatureDeployment_Details.csv"
foreach ($User in $UsersCollection)
{
try
{
$UserDetails = Get-User -Identity $User.UserPrincipalName -ErrorAction Stop
if ($AddressUsed) {
$Address = Generate-UserAddress ($UserDetails)
}
$UserSignature = $HTMLSignature.Replace('%%DisplayName%%',$UserDetails.DisplayName).Replace('%%EmailAddress%%',$UserDetails.UserPrincipalName).Replace('%%MobilePhone%%',$UserDetails.MobilePhone).Replace('%%BussinessPhone%%',$UserDetails.Phone).Replace('%%FaxNumber%%',$UserDetails.Fax).Replace('%%Department%%',$UserDetails.Department).Replace('%%Title%%',$UserDetails.Title).Replace('%%Office%%',$UserDetails.Office).Replace('%%Address%%',$Address).Replace('%%StreetAddress%%',$UserDetails.StreetAddress).Replace('%%City%%',$UserDetails.City).Replace('%%StateOrProvince%%',$UserDetails.StateOrProvince).Replace('%%PostalCode%%',$UserDetails.PostalCode).Replace('%%CountryOrRegion%%',$UserDetails.CountryOrRegion)
Set-MailboxMessageConfiguration -Identity $UserDetails.UserPrincipalName -SignatureHTML $UserSignature -AutoAddSignature $true -AutoAddSignatureOnMobile $true -AutoAddSignatureOnReply $true -ErrorAction Stop
$DeploymentStatus = "Successful"
}
catch
{
$ErrorMessage = $_.Exception.Message
$DeploymentStatus = "Unsuccessful : $ErrorMessage "
Write-Host "`n`Signature deployment for $($User.UserPrincipalName) is Unsucessful -Error Occured" -ForegroundColor Red
}
$AuditData = [pscustomobject]@{ UserPrincipalName = $User.UserPrincipalName; DeploymentStatus = $DeploymentStatus }
$AuditData | Export-Csv -Path $SignatureLog_FilePath -NoTypeInformation -Append
Write-Progress -Activity "Deploying Signature : $($Count) of $($TotalCount) | Current User : $($User.UserPrincipalName)"
$Count++
}
Disconnect_ExchangeOnline_Safely
}
#-------------------function to verify the status of Signature Assign to user and process the auditlog csv file and disconnect the exchangeonline module-------------------------
function Disconnect_ExchangeOnline_Safely
{
if (-not ($UserConfirmation -eq 1))
{
Write-Host "`n`The signature has been added and can be deployed later by enabling 'PostponeRoamingSignatureUntilLater'."
}
Write-Host "`n`Script Execution Completed"
Disconnect-ExchangeOnline -Confirm:$false
Write-Host "`n`The Signature Deployment Status Report available in:" -NoNewline -ForegroundColor Yellow
Write-Host $SignatureLog_FilePath
if(-not $TaskScheduler)
{
$Title = "Confirmation"
$Question = "Do you want to View the Signature Deployment Status Report?"
$Choices = "&Yes","&No"
$Decision = $Host.UI.PromptForChoice($Title,$Question,$Choices,1)
if ($Decision -eq 0) {
Start-Process $SignatureLog_FilePath
}
}
exit
}
#---------------------------------------Block to call the function as per the user Choice----------------------------
$Output = ""
if (-not $TaskScheduler)
{
Write-Host "`n`Signature configuration choices:" -ForegroundColor Yellow
Write-Host @"
`n`1. Create a Text Signature using Default Fields
`n`2. Create an HTML Signature using an In-built HTML Template
`n`3. Create a Text Signature with custom Fields
`n`4. Create an HTML Signature with a user-defined Template
"@ -ForegroundColor Cyan
$UserChoice = Read-Host "`n`Enter Your choice"
}
else
{
$UserChoice = $Inputs[$Index++].Input
}
while ($true)
{
if (($UserChoice -eq 1 -or $UserChoice -eq 2 -or $UserChoice -eq 3 -or $UserChoice -eq 4) -and (-not $TaskScheduler))
{
$UserChoice | Select-Object @{ Name = 'Input'; Expression = { $_ } } | Export-Csv -Path $InputsFolderPath -NoTypeInformation -Append
}
if ($UserChoice -eq 1)
{
$Output = "`n`Adding Default Text Signature "
Deploy-DefaultTextSignature
}
elseif ($UserChoice -eq 2)
{
$Output = "`n`Adding In-build HTML Signature "
Deploy-InbuiltHTMLSignature
}
elseif ($UserChoice -eq 3)
{
$Output = "`n`Adding Custom Text Signature "
Deploy-CustomTextSignature
}
elseif ($UserChoice -eq 4)
{
$Output = "`n`Adding Custom HTML Signature "
Deploy-CustomHTMLSignature
}
else
{
Write-Host "`n`Enter the correct input" -ForegroundColor Red
$UserChoice = Read-Host
continue;
}
}