Private/Write-LogEntry.ps1
<#
.SYNOPSIS Writes a timestamped message to a log file. .DESCRIPTION The Write-LogEntry function writes a formatted log entry to a specified or default log file. Each entry includes the current timestamp and the username running the script. This function is useful for tracking script activity, debugging, or generating audit trails. By default, logs are written to `PSCitrixPowerBi.log` in the user's Documents folder, but a custom path can be specified. .PARAMETER Message The message content to log. This is a mandatory string and should describe the event, error, or operation being logged. .PARAMETER LogPath Optional. The path to the log file where the entry will be written. If not specified, it defaults to `$env:USERPROFILE\Documents\PSCitrixPowerBi.log`. .EXAMPLE # Write a log message to the default log file Write-LogEntry -Message "VDI reboot process started" .EXAMPLE # Write a message to a custom log file path Write-LogEntry -Message "All services verified" -LogPath "C:\Logs\CitrixOps.log" .NOTES Each log entry is prepended with a timestamp and the username of the session running the script. This ensures traceability and ease of troubleshooting. #> function Write-LogEntry { param ( [Parameter(Mandatory)] [string]$Message, [string]$LogPath = "$env:USERPROFILE\Documents\PSCitrixPowerBi.log" ) # Custom timestamp and current user $timestamp = Get-Date -Format "MM/dd/yyyy HH:mm:ss" $user = $env:USERNAME # Build log line $entry = "[$timestamp] [$user] $Message" # Ensure log file exists if (-not (Test-Path -Path $LogPath)) { New-Item -ItemType File -Path $LogPath -Force | Out-Null } # Write log entry Add-Content -Path $LogPath -Value $entry } |