Skip to content

Instantly share code, notes, and snippets.

@adbertram
Created July 3, 2024 14:24
Show Gist options
  • Save adbertram/afc8ce0a54627455cbcfffcbf11062f8 to your computer and use it in GitHub Desktop.
Save adbertram/afc8ce0a54627455cbcfffcbf11062f8 to your computer and use it in GitHub Desktop.
function Write-CustomLog {
param([string]$Message)
$callerInfo = (Get-PSCallStack)[1]
$scriptName = Split-Path -Leaf $callerInfo.ScriptName
$lineNumber = $callerInfo.ScriptLineNumber
$functionName = $callerInfo.FunctionName
$logEntry = "{0:yyyy-MM-dd HH:mm:ss} - {1}:{2} - {3} - {4}" -f `
(Get-Date), $scriptName, $lineNumber, $functionName, $Message
Add-Content -Path ".\script_log.txt" -Value $logEntry
}
# Usage in a script or function
function Test-LoggingFunction {
Write-CustomLog "This is a test log entry"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment