0

I am looking a way to get output the pointer (line number) from running powershell script and write into a selfmade Log-File.

function DebugMode {

param([int]$linenumber,[String]$message)
$Date = Get-Date -Format 'yyyyMMdd'
$logfilepath ="$scriptpath\Debug_$Date.log"

$Date  + " - "+ $linenumber + " - " +$message |  Out-File -FilePath $logfilepath -Append}

1 Answer 1

2

You might use the Get-PSCallStack cmdlet for this:

function Write-Log ($Message) {
    $linenumber = (Get-PSCallStack)[1].ScriptLineNumber
    $Date = Get-Date -Format 'yyyyMMdd'
    $Date  + " - "+ $linenumber + " - " +$message
}

Write-Log 'test'
20230104 - 7 - test
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.