Public/Stop-Script.ps1

function Stop-Script
{
    <#
        .DESCRIPTION
            Write error information to the log file and exit the script with a specified exit code
 
        .PARAMETER ErrorMessage
            The error message to write to the log file
 
        .PARAMETER Exception
            An exception message to write to the log file
 
        .EXAMPLE
            Stop-Script -ErrorMessage "Example action failed to complete" -Exception $PSItem.Exception.Message
 
        .NOTES
            Created by: Jon Anderson
            Modified: 2023-07-10
 
    #>

    [CmdletBinding()]    
    param(
        [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
        [String]$ErrorMessage,
        [Parameter(Mandatory=$false)][ValidateNotNullOrEmpty()]
        [String]$Exception
    )

    Write-LogEntry -Value $ErrorMessage -Severity 3
    if($Exception)
    {
        Write-LogEntry -Value "Exception Message: $Exception" -Severity 3
    }
    exit
}