Public/Write-specLogMessage.ps1

function Write-specLogMessage {
    <#
    .SYNOPSIS
        Logs a message to the console with a specified text colour and appends the same message to a log file.
 
    .DESCRIPTION
        The Write-specLogMessage function provides a convenient way to log messages in PowerShell scripts and modules. It allows you
        to specify a message and an optional text colour for display in the console. Additionally, the function appends the
        message, along with a timestamp, to a log file for further analysis and auditing.
 
    .PARAMETER Message
        Specifies the message to be logged. This parameter is mandatory.
 
    .PARAMETER LogPath
        Specifies the path where the log file will be stored. Default value is "$env:Programdata\Intune-PowerShell-Logs".
 
    .PARAMETER LogName
        Specifies the name of the log file. Default value is 'TeamViewerManagedDevice.log'.
 
    .PARAMETER Colour
        Specifies the text colour for displaying the message in the console. Valid values are Consolecolour names (e.g. "Red",
        "Green", "Yellow", etc.). Default value is "White".
 
    .PARAMETER DoNotUseWriteHost
        Prevents the use of Write-Host to display the message in the console. By default, the function uses Write-Host
        unless this switch is present.
 
    .EXAMPLE
        Write-specLogMessage -Message "Task completed successfully!" -Colour Green
        Logs a success message in green colour to the console and appends the message to the log file.
 
    .EXAMPLE
        Write-specLogMessage -Message "Error occurred: Unable to process the request." -DoNotUseWriteHost
        Appends the message to the log file. Will not output the message to the console.
 
    .NOTES
        Author : owen.heaume
        Version : 1.1
 
    #>



    [cmdletbinding()]

    param (
        [Parameter(Mandatory = $true)]
        [string]$Message,

        [Parameter(Mandatory = $false)]
        [string]$LogPath = "$env:ProgramData\Microsoft\IntuneManagementExtension\Logs",

        [Parameter(Mandatory = $false)]
        [string]$LogName = 'TeamViewerManagedDevice.log',

        [Parameter(Mandatory = $false)]
        [validateset ('Black','Blue','Cyan','DarkBlue','DarkCyan','DarkGray','DarkGreen','DarkMagenta','DarkRed','DarkYellow','Gray','Green','Magenta','Red','White','Yellow')]
        [string]$Colour = "White",

        [switch]
        $DoNotUseWriteHost

    )

    begin {
        if (!(test-path $logpath)) {
            throw "$logpath cannot be found. Please use a valid path"
        } else {
            try {
                $finalPath = Join-Path $LogPath -ChildPath $LogName -ea stop
            } catch {
                throw "An error occured trying to join the path $LogPath to $LogName : $_"
            }
        }

        # Reverse date for log clarity
        try {
            $DateAndTime = get-date -format "yyyy/MM/dd HH:mm:ss" -ea stop
        } catch {
            throw "An error occurred trying to obtain the system date and time: $_"
        }
    }

    process {
        try {
            if ($DoNotUseWriteHost.IsPresent) {
                Add-Content -ea Stop -Path $finalPath -Value "$DateAndTime - $Message"
            } else {
                Write-Host $Message -ForegroundColor $Colour
                Add-Content -ea stop -Path $finalPath -Value "$DateAndTime - $Message"
            }
        } catch {
            throw "An error occured trying to write the log: $_"
        }
    }
}