ExO-MailboxAndInboxRuleForwardingReport.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
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
<#PSScriptInfo
.VERSION 1.0.1
.GUID 4e794dab-0e07-43ed-bbd9-ec685be3421a
.AUTHOR Soren Lindevang
.COMPANYNAME
.COPYRIGHT
.TAGS PowerShell Exchange Online Office 365 Reporting Report Forwarding ForwardingRules InboxRules SMTPForwarding Mailbox MailboxForwarding Azure Automation
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>


<#
.SYNOPSIS
    Generate Report of Mailbox and Inbox Rules Forwarding Mails to External Recipients.
 
.DESCRIPTION
    Searches for mailbox forwarding and inbox rules that forward to external recipients.
     
    Option to send a CSV report over email.
     
    Designed for execution in Azure Automation.
 
    Check out the GitHub Repo for more information: https://github.com/soren-cloud/ExO-MailboxAndInboxRuleForwardingReport
 
.PARAMETER AutomationPSCredentialName
    Name of the Automation Credential used when connecting to Exchange Online.
 
    The Account should at least have "Audit Log" rights in the Exchange Online tenant.
 
    Example: Exchange Online Service Account
 
.PARAMETER ExcludeExternalDomain
    External recipient domains to be excluded when searching for external forwarding.
 
    Example 1: ['domain.com']
    Example 2: ['domainA.com','domainB.com']
 
.PARAMETER ExcludeExternalEmailAddress
    External recipient email adresses to be excluded when searching for external forwarding.
 
    Example 1: ['abc@domain.com']
    Example 2: ['abc@domain.com','xyz@domain.com']
 
.PARAMETER SendMailboxForwardingReport
     If this switch is present, the script sends an email with a CSV file attached, if mailbox forwarding is detected.
      
     If used, please do modify the 'SendMailReport' variables in the 'Declarations' area.
 
     Example 1: true
     Example 2: false
 
.PARAMETER SendInboxRuleForwardingReport
     If this switch is present, the script sends an email with a CSV file attached, if any inbox rules are detected.
      
     If used, please do modify the 'SendMailReport' variables in the 'Declarations' area.
 
     Example 1: true
     Example 2: false
 
.PARAMETER EnableVerbose
     If this switch is present, 'VerbosePreference' will be set to "Continue" in the script.
      
     Build-in Verbose switch is not supported by Azure Automation (yet).
      
     Example 1: true
     Example 2: false
 
     Default value = false
 
.INPUTS
    N/A
 
.OUTPUTS
    N/A
 
.NOTES
    Version: 1.0.1
    Author: Soren Greenfort Lindevang
    Creation Date: 15.05.2018
    Purpose/Change: - Added 'EnableVerbose' switch
                    - Improved error handling
                    - Improved output formatting
                    - Other fixes
 
    Version: 1.0
    Author: Soren Greenfort Lindevang
    Creation Date: 16.04.2018
    Purpose/Change: Initial script development
   
.EXAMPLE
    N/A
#>

[cmdletbinding()]
param (
    [Parameter(
        Mandatory=$true)]
        [string]$AutomationPSCredentialName,
    [Parameter(
        Mandatory=$false)]
        [string[]]$ExcludeExternalDomain,
    [Parameter(
        Mandatory=$false)]
        [string[]]$ExcludeExternalEmailAddress,
    [Parameter(
        Mandatory=$false)]
        [switch]$SendMailboxForwardingReport,
    [Parameter(
        Mandatory=$false)]
        [switch]$SendInboxRuleForwardingReport,
    [Parameter(
        Mandatory=$false)]
        [switch]$EnableVerbose     
)


#-----------------------------------------------------------[Functions]------------------------------------------------------------

# Test if script is running in Azure Automation
function Test-AzureAutomationEnvironment
    {
    if ($env:AUTOMATION_ASSET_ACCOUNTID)
        {
        Write-Verbose "This script is executed in Azure Automation"
        }
    else
        {
        $ErrorMessage = "This script is NOT executed in Azure Automation."
        throw $ErrorMessage
        }
    }

# Connect to Exchange Online
function Connect-ExchangeOnline 
    {
    param ($Credential,$Commands)
    try
        {
        Write-Output "Connecting to Exchange Online"
        Get-PSSession | Remove-PSSession       
        $Session = New-PSSession â€“ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential `
            -Authentication Basic -AllowRedirection
        Import-PSSession -Session $Session -DisableNameChecking:$true -AllowClobber:$true -CommandName $Commands | Out-Null
        }
    catch 
        {
        Write-Error -Message $_.Exception
        Stop-AutomationScript -Status Failed
        }
    Write-Verbose "Successfully connected to Exchange Online"
    }

# Disconnect to Exchange Online
function Disconnect-ExchangeOnline 
    {
    try
        {
        Write-Output "Disconnecting from Exchange Online"
        Get-PSSession | Remove-PSSession       
        }
    catch 
        {
        Write-Error -Message $_.Exception
        Stop-AutomationScript -Status Failed
        }
    Write-Verbose "Successfully disconnected from Exchange Online"
    }

# Stop Automation Script
function Stop-AutomationScript
    {
    param(
        [ValidateSet("Failed","Success")]
        [string]
        $Status = "Success"
        )
    Write-Output ""
    Disconnect-ExchangeOnline
    if ($Status -eq "Success")
        {
        Write-Output "Script successfully completed"
        }
    elseif ($Status -eq "Failed")
        {
        Write-Output "Script stopped with an Error"
        }
    Break
    }

Function Get-MailboxForwardingToExternal
    {
    [cmdletbinding()]
    param (
        [Parameter(
            Mandatory=$true)]
            [Alias('Mailboxes')]
            [psobject]$MailboxPSObject,
        [Parameter(
            Mandatory=$true)]
            [Alias('Recipients')]
            [psobject]$RecipientPSObject,
        [Parameter(
            Mandatory=$true)]
            [Alias('InternalDomains')]
            [psobject]$AcceptedDomainPSobject,
        [Parameter(
            Mandatory=$false)]
            [string[]]$ExcludeExternalDomain,
        [Parameter(
            Mandatory=$false)]
            [string[]]$ExcludeExternalEmailAddress
    )
    Begin
        {
        Write-Verbose "Beginning Get-Get-MailboxForwardingToExternal function"
        Write-Verbose "$(($MailboxPSObject | Measure-Object).Count) objects in 'MailboxPSObject'"
        }

    Process
        {
        Write-Verbose "Processing Objects in 'MailboxPSObject'"
        $count = $null
        foreach ($Mailbox in $MailboxPSObject) 
            {
            $count++
            $WriteOutput = $false
            Write-Verbose "Object $count of $(($MailboxPSObject | Measure-Object).Count)"
            Write-Verbose "PrimarySmtpAddress: '$($Mailbox.PrimarySmtpAddress)'"

            # ForwardingAddress
            if ($Mailbox.ForwardingAddress)
                {
                $ForwardingRecipient = $RecipientPSObject | Where-Object {$_.Identity -eq $Mailbox.ForwardingAddress}
                $ForwardingRecipientType = $ForwardingRecipient.RecipientType
                Write-Verbose "ForwardingAddress: $($ForwardingRecipient.Identity)"
                Write-Verbose "ForwardingAddress Type: $ForwardingRecipientType"
                if ($ForwardingRecipientType -eq "MailContact")
                    {
                    $EmailAddress = ($ForwardingRecipient.ExternalEmailAddress -split "SMTP:")[1].Trim("]")
                    $EmailAddressDomain = ($EmailAddress -split "@")[1]
                    if (($AcceptedDomains.DomainName -notcontains $EmailAddressDomain) -and 
                        ($ExcludeExternalEmailAddress -notcontains $EmailAddress) -and
                        ($ExcludeExternalDomain -notcontains $EmailAddressDomain))
                        {
                        $ForwardingHash = $null
                        $ForwardingHash = [ordered]@{
                            PrimarySmtpAddress              = $Mailbox.PrimarySmtpAddress
                            DisplayName                     = $Mailbox.DisplayName
                            ExternalForwardingAddress       = $EmailAddress
                            MailboxForwardingType           = "ForwardingAddress"
                            }
                        $Object = New-Object PSObject -Property $ForwardingHash
                        Write-Verbose "Writing Output"
                        Write-Output $Object
                        }
                    else
                        {
                        Write-Verbose "$EmailAddress - Internal Domain, Excluded Domain or Excluded Email Address"
                        }
                    }
                else
                    {
                    Write-Verbose "Skipping this type of recipient"
                    }
                }
            else
                {
                Write-Verbose "ForwardingAddress: null"
                }

            # ForwardingSmtpAddress
            if ($Mailbox.ForwardingSmtpAddress)
                {
                $MailboxForwardingSmtpAddress = $Mailbox.ForwardingSmtpAddress
                Write-Verbose "ForwardingSmtpAddress: $MailboxForwardingSmtpAddress"
                $EmailAddress = ($MailboxForwardingSmtpAddress -split "SMTP:")[1].Trim("]")
                $EmailAddressDomain = ($EmailAddress -split "@")[1]
                if (($AcceptedDomains.DomainName -notcontains $EmailAddressDomain) -and 
                    ($ExcludeExternalEmailAddress -notcontains $EmailAddress) -and
                    ($ExcludeExternalDomain -notcontains $EmailAddressDomain))
                    {
                    $ForwardingHash = $null
                    $ForwardingHash = [ordered]@{
                        PrimarySmtpAddress              = $Mailbox.PrimarySmtpAddress
                        DisplayName                     = $Mailbox.DisplayName
                        ExternalForwardingAddress       = $EmailAddress
                        MailboxForwardingType           = "ForwardingSmtpAddress"
                        }
                    $Object = New-Object PSObject -Property $ForwardingHash
                    Write-Verbose "Writing Output"
                    Write-Output $Object
                    }
                else
                    {
                    Write-Verbose "$EmailAddress - Internal Domain, Excluded Domain or Excluded Email Address"
                    }

                }
            else
                {
                Write-Verbose "ForwardingSMTPAddress: null"
                }
            }
        }

    End
        {
        Write-Verbose "End of Get-InboxRuleForwardingToExternal function"
        }
    }

Function Get-InboxRuleForwardingToExternal
    {
    [cmdletbinding()]
    param (
        [Parameter(
            Mandatory=$true)]
            [Alias('Mailboxes')]
            [psobject]$MailboxPSObject,
        [Parameter(
            Mandatory=$true)]
            [Alias('InternalDomain')]
            [psobject]$AcceptedDomainPSobject,
        [Parameter(
            Mandatory=$false)]
            [string[]]$ExcludeExternalDomain,
        [Parameter(
            Mandatory=$false)]
            [string[]]$ExcludeExternalEmailAddress
    )
    Begin
        {
        Write-Verbose "Beginning Get-InboxRuleForwardingToExternal function"
        Write-Verbose "$(($MailboxPSObject | Measure-Object).Count) objects in 'MailboxPSObject'"
        }

    Process
        {
        Write-Verbose "Processing Objects in 'MailboxPSObject'"
        $count = $null
        foreach ($Mailbox in $MailboxPSObject) 
            {
            $count++
            Write-Verbose "Object $count of $(($MailboxPSObject | Measure-Object).Count)"
            Write-Verbose "PrimarySmtpAddress: '$($Mailbox.PrimarySmtpAddress)'"
            $ForwardingRules = $null
            try
                {
                $Rules = Get-InboxRule -Mailbox $Mailbox.PrimarySmtpAddress
                }
            catch
                {
                Write-Error -Message $_.Exception
                }
            $ForwardingRules = $Rules | Where-Object {$_.ForwardTo -or $_.ForwardAsAttachmentTo}           
            foreach ($Rule in $ForwardingRules)
                {
                $Recipients = @()
                $Recipients = $Rule.ForwardTo | Where-Object {$_ -match "SMTP"}
                $Recipients += $Rule.ForwardAsAttachmentTo | Where-Object {$_ -match "SMTP"}
     
                $ExternalRecipients = @()
 
                foreach ($Recipient in $Recipients) 
                    {
                    $EmailAddress = ($Recipient -split "SMTP:")[1].Trim("]")
                    $EmailAddressDomain = ($EmailAddress -split "@")[1]
                    if (($ExcludeExternalEmailAddress -notcontains $EmailAddress) -and
                        ($AcceptedDomains.DomainName -notcontains $EmailAddressDomain) -and
                        $ExcludeExternalDomain -notcontains $EmailAddressDomain)
                        {
                        $ExternalRecipients += $EmailAddress
                        }
                    else
                        {
                        Write-Verbose "$EmailAddress - Internal Domain, Excluded Domain or Excluded Email Address"
                        }
                    }
                if ($ExternalRecipients) 
                    {
                    $ExternalRecipientsString = $ExternalRecipients -join ", "
                    Write-Verbose "Rule '$($Rule.Name)' forwards to '$ExternalRecipientsString'"
 
                    $RuleHash = $null
                    $RuleHash = [ordered]@{
                        PrimarySmtpAddress = $Mailbox.PrimarySmtpAddress
                        DisplayName        = $Mailbox.DisplayName
                        RuleId             = $Rule.Identity
                        RuleName           = $Rule.Name
                        RuleDescription    = $Rule.Description
                        ExternalRecipients = $ExternalRecipientsString 
                        }
                    $Object = New-Object PSObject -Property $RuleHash
                    Write-Verbose "Writing Output"
                    Write-Output $Object
                    }
                else
                    {
                    Write-Verbose "Rule '$($Rule.Name)' does not contain external forwarding"
                    }
                }
            }
        }

    End
        {
        Write-Verbose "End of Get-InboxRuleForwardingToExternal function"
        }
    }


#----------------------------------------------------------[Declarations]----------------------------------------------------------

# General Send Report Variables
$ReportSmtpServer = "smtp.office365.com"
$ReportSmtpPort = 587
$ReportSmtpPSCredentialName = $AutomationPSCredentialName
$ReportSmtpFrom = "serviceaccount@domain.com"
$ReportSmtpTo = "someone@domain.com"

# SendInboxRuleForwardingReport Variables
$SendInboxRuleForwardingReportSubject = "Report: Inbox Rules with Forwarding to External Recipients"
$SendInboxRuleForwardingReportBody = "CSV file attached, containing Inbox Rule Information" 

# SendMailboxForwardingReport Variables
$SendMailboxForwardingReportSubject = "Report: Mailbox Forwarding with External Recipients"
$SendMailboxForwardingReportBody = "CSV file attached, containing Mailbox Forwarding Information" 


#-----------------------------------------------------------[Execution]-----------------------------------------------------------

# Check if script is executed in Azure Automation
Test-AzureAutomationEnvironment

Write-Output "::: Parameters :::"
Write-Output "AutomationPSCredentialName: $AutomationPSCredentialName"
Write-Output "ExcludeExternalDomain: $ExcludeExternalDomain"
Write-Output "ExcludeExternalEmailAddress: $ExcludeExternalEmailAddress"
Write-Output "SendMailboxForwardingReport: $SendMailboxForwardingReport"
Write-Output "SendInboxRuleForwardingReport: $SendInboxRuleForwardingReport"
Write-Output "EnableVerbose: $EnableVerbose"
Write-Output ""

# Handle Verbose Preference
if ($EnableVerbose -eq $true)
    {
    $VerbosePreference = "Continue"
    }

# Get AutomationPSCredential
Write-Output "::: Connection :::"
try
    {
    Write-Output "Importing Automation Credential"
    $Credential = Get-AutomationPSCredential -Name $AutomationPSCredentialName -ErrorAction Stop
    }
catch 
    {
    Write-Error -Message $_.Exception
    Stop-AutomationScript -Status Failed
    }
Write-Verbose "Successfully imported credentials"

# Connect to Exchange Online
Connect-ExchangeOnline -Credential $Credential -Commands "Get-AcceptedDomain","Get-Mailbox","Get-InboxRule","Get-Recipient"
Write-Output ""

# Import Accepted Domains
Write-Verbose ":::Import Accepted Domains:::"
try
    {
    Write-Verbose "Importing List of Accepted Domains"
    $AcceptedDomains = Get-AcceptedDomain -ErrorAction Stop
    }
catch 
    {
    Write-Error -Message $_.Exception
    Stop-AutomationScript -Status Failed
    }
Write-Verbose "Successfully Imported List of Accepted Domains"

# Import All Mailboxes
try
    {
    Write-Verbose "Importing List of Mailboxes"
    $Mailboxes = Get-Mailbox -ResultSize Unlimited -ErrorAction Stop `
        | Select-Object Identity,DisplayName,PrimarySmtpAddress,ForwardingAddress,ForwardingSmtpAddress
    }
catch 
    {
    Write-Error -Message $_.Exception
    Stop-AutomationScript -Status Failed
    }

if (!$Mailboxes)
    {
    $ErrorMessage = "No Mailboxes Found!"
    Stop-AutomationScript -Status Failed
    }

Write-Verbose "Successfully Imported List of Mailboxes"
Write-Verbose ""

# Import All Recipients
Write-Verbose "::: Import Exchange Recipient List :::"
try
    {
    Write-Verbose "Importing List of Recipients"
    $Recipients = Get-Recipient -ResultSize Unlimited -ErrorAction Stop `
        | Select-Object Identity,RecipientType,ExternalEmailAddress
    }
catch 
    {
    Write-Error -Message $_.Exception
    Stop-AutomationScript -Status Failed
    }
Write-Verbose "Successfully Imported List of Recipients"
Write-Verbose ""

# Process Mailboxes
Write-Output "::: Analyzing Mailbox Forwarding :::"
Write-Output "Mailboxes in Scope for Search: $(($Mailboxes | Measure-Object).Count)"

$MailboxForwarding = Get-MailboxForwardingToExternal -MailboxPSObject $Mailboxes -RecipientPSObject $Recipients -AcceptedDomainPSobject $AcceptedDomains `
    -ExcludeExternalEmailAddress $ExcludeExternalEmailAddress -ExcludeExternalDomain $ExcludeExternalDomain

Write-Output "Mailbox Forwarding to External Recipients: $(($MailboxForwarding | Measure-Object).Count)"
if ($MailboxForwarding)
    {
    Write-Output $($MailboxForwarding | fl)
    }
Write-Output ""

# Inbox Rules
Write-Output "::: Analyzing Inbox Rules :::"
Write-Output "Mailboxes in Scope for Search: $(($Mailboxes | Measure-Object).Count)"

$RulesWithForwarding = Get-InboxRuleForwardingToExternal -MailboxPSObject $Mailboxes -AcceptedDomainPSobject $AcceptedDomains `
    -ExcludeExternalEmailAddress $ExcludeExternalEmailAddress -ExcludeExternalDomain $ExcludeExternalDomain

Write-Output "Inbox Rules Forwarding to External Recipients: $(($RulesWithForwarding | Measure-Object).Count)"
Write-Output $RulesWithForwarding
Write-Output ""

# Send Mail Report
if (($SendMailboxForwardingReport -and $MailboxForwarding) -or ($SendInboxRuleForwardingReport -and $RulesWithForwarding))
    {
    Write-Output "::: Send Mail Report :::"
    Write-Output "Importing Automation Credential"
    try
        {
        $ReportSmtpPSCredential = Get-AutomationPSCredential -Name $ReportSmtpPSCredentialName -ErrorAction Stop
        }
    catch 
        {
        Write-Error -Message $_.Exception
        Stop-AutomationScript -Status Failed
        }
    $ReportTime = Get-Date -Format "MM-dd-yyyy_HH-mm-ss"
    if ($SendMailboxForwardingReport -and $MailboxForwarding)
        {
        Write-Output "Generate Mailbox Forwarding CSV file"
        try
            {
            $CSVFileName = "MailboxForwarding_" + $ReportTime + ".csv"
            $CSVFilePath = $env:TEMP + "\" + $CSVFileName
            $MailboxForwarding | Export-CSV -LiteralPath $CSVFilePath -Encoding Unicode -NoTypeInformation -Delimiter "`t" -ErrorAction Stop
            }
        catch 
            {
            Write-Error -Message $_.Exception
            }
        Write-Output "Send e-mail to '$ReportSmtpTo'"
        try
            {
            Send-MailMessage -To $ReportSmtpTo -From $ReportSmtpFrom -Subject $SendMailboxForwardingReportSubject `
                -Body $SendMailboxForwardingReportBody -BodyAsHtml -Attachments $CSVFilePath -SmtpServer $ReportSmtpServer `
                -Port $ReportSmtpPort -UseSsl -Credential $ReportSmtpPSCredential -ErrorAction Stop
            }
        catch 
            {
            Write-Error -Message $_.Exception
            }
        }
    if ($SendInboxRuleForwardingReport -and $RulesWithForwarding)
        {
        Write-Output "Generate Inbox Rule Forwarding CSV file"
        try
            {
            $CSVFileName = "InboxForwardingRules_" + $ReportTime + ".csv"
            $CSVFilePath = $env:TEMP + "\" + $CSVFileName
            $RulesWithForwarding | Export-CSV -LiteralPath $CSVFilePath -Encoding Unicode -NoTypeInformation -Delimiter "`t" -ErrorAction Stop
            }
        catch 
            {
            Write-Error -Message $_.Exception
            }
        Write-Output "Send e-mail to '$ReportSmtpTo'"
        try
            {
            Send-MailMessage -To $ReportSmtpTo -From $ReportSmtpFrom -Subject $SendInboxRuleForwardingReportSubject `
                -Body $SendInboxRuleForwardingReportBody -BodyAsHtml -Attachments $CSVFilePath -SmtpServer $ReportSmtpServer `
                -Port $ReportSmtpPort -UseSsl -Credential $ReportSmtpPSCredential -ErrorAction Stop
            }
        catch 
            {
            Write-Error -Message $_.Exception
            }
        }
    }


# Script Completed
Stop-AutomationScript -Status Success