Created
July 3, 2024 14:24
-
-
Save adbertram/afc8ce0a54627455cbcfffcbf11062f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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