Repository: microsoft/CSS-Exchange
File: Admin/MonitorExchangeAuthCertificate/ConfigurationAction/Register-AuthCertificateRenewalTask.ps1 (PowerShell)

Code before:
                    Unregister-ScheduledTask -TaskPath $($t.TaskPath) -TaskName $($t.TaskName) -Confirm:$false -ErrorAction Stop
                } else {
                    Write-Host ("What if: Will unregister scheduled task: '$($t.TaskName)' by running 'Unregister-ScheduledTask'")
                }
                Write-Verbose ("Scheduled task: $($t.TaskName) successfully unregistered")
            }
        } catch {
            Write-Verbose ("The scheduled task already exists and we were unable to unregister it - Exception $($Error[0].Exception.Message)")
            Invoke-CatchActionError $CatchActionFunction
            return $false
        }
    }

    if (($WhatIfPreference) -or
        (Test-Path -Path $fullPathToScript)) {
        $passwordAsPlaintextString = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))

        $schTaskTrigger = New-ScheduledTaskTrigger -Daily -At $DailyRuntime

        $newScheduledTaskParams = @{
            Execute          = "powershell.exe"
            WorkingDirectory = "$($WorkingDirectory)"
        }

        $basicArgumentParameters = "-NonInteractive -NoLogo -NoProfile -Command `".\$($ScriptName) -ValidateAndRenewAuthCertificate `$true -IgnoreUnreachableServers `$$($IgnoreOfflineServers) -IgnoreHybridConfig `$$($IgnoreHybridConfig)"
        if (-not([System.String]::IsNullOrEmpty($SendEmailNotificationTo))) {
            if ($TrustAllCertificates) {
                $newScheduledTaskParams.Add("Argument", "$($basicArgumentParameters) -SendEmailNotificationTo $($SendEmailNotificationTo) -TrustAllCertificates -Confirm:`$false`"")
            } else {
                $newScheduledTaskParams.Add("Argument", "$($basicArgumentParameters) -SendEmailNotificationTo $($SendEmailNotificationTo) -Confirm:`$false`"")
            }
        } else {
            $newScheduledTaskParams.Add("Argument", "$($basicArgumentParameters) -Confirm:`$false`"")
        }

        $schTaskAction = New-ScheduledTaskAction @newScheduledTaskParams

        $registerSchTaskParams = @{
            TaskName    = $TaskName
            Trigger     = $schTaskTrigger
            Action      = $schTaskAction
            Description = $TaskDescription
            RunLevel    = "Highest"
            User        = $Username
            Password    = $passwordAsPlaintextString
            Force       = $true
            ErrorAction = "Stop"
        }

        try {
            Write-Verbose ("Scheduled Task: $($TaskName) successfully created")

Review comment (suggestion):
$SendEmailNotificationTo is [string[]] but this check pretends it's [string]. This will not work consistently. For example, if you provide an actual [string[]] where all the values are empty:

![image](https://user-images.githubusercontent.com/4518572/218325666-f38777ce-a2fe-4306-aae2-3305f9bb38ba.png)


Code after:
                if (-not($WhatIfPreference)) {
                    Unregister-ScheduledTask -TaskPath $($t.TaskPath) -TaskName $($t.TaskName) -Confirm:$false -ErrorAction Stop
                } else {
                    Write-Host ("What if: Will unregister scheduled task: '$($t.TaskName)' by running 'Unregister-ScheduledTask'")
                }
                Write-Verbose ("Scheduled task: $($t.TaskName) successfully unregistered")
            }
        } catch {
            Write-Verbose ("The scheduled task already exists and we were unable to unregister it - Exception $($Error[0].Exception.Message)")
            Invoke-CatchActionError $CatchActionFunction
            return $false
        }
    }

    if (($WhatIfPreference) -or
        (Test-Path -Path $fullPathToScript)) {
        $passwordAsPlaintextString = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))

        $schTaskTrigger = New-ScheduledTaskTrigger -Daily -At $DailyRuntime

        $newScheduledTaskParams = @{
            Execute          = "powershell.exe"
            WorkingDirectory = "$($WorkingDirectory)"
        }

        $basicArgumentParameters = "-NonInteractive -NoLogo -NoProfile -Command `".\$($ScriptName) -ValidateAndRenewAuthCertificate `$true -IgnoreUnreachableServers `$$($IgnoreOfflineServers) -IgnoreHybridConfig `$$($IgnoreHybridConfig)"
        if ($null -ne $SendEmailNotificationTo) {
            if ($TrustAllCertificates) {
                $newScheduledTaskParams.Add("Argument", "$($basicArgumentParameters) -SendEmailNotificationTo $([string]::Join(", ", $SendEmailNotificationTo)) -TrustAllCertificates -Confirm:`$false`"")
            } else {
                $newScheduledTaskParams.Add("Argument", "$($basicArgumentParameters) -SendEmailNotificationTo $([string]::Join(", ", $SendEmailNotificationTo)) -Confirm:`$false`"")
            }
        } else {
            $newScheduledTaskParams.Add("Argument", "$($basicArgumentParameters) -Confirm:`$false`"")
        }

        $schTaskAction = New-ScheduledTaskAction @newScheduledTaskParams

        $registerSchTaskParams = @{
            TaskName    = $TaskName
            Trigger     = $schTaskTrigger
            Action      = $schTaskAction
            Description = $TaskDescription
            RunLevel    = "Highest"
            User        = $Username
            Password    = $passwordAsPlaintextString
            Force       = $true
            ErrorAction = "Stop"
        }

        try {