Hi, I wrote a nice debug function that provides the name of function from which it's called. The code is:
Function Write-Log {
$callerName = (Get-Variable MyInvocation -Scope 1).Value.MyCommand.Name
$stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$line = "$stamp [$level`]: $callerName $Message"
Write-Host $line -ForegroundColor $color
}
Works great when called from inside .ps1 script; however I started refactoring code so that some functions are moved to a .psm1 module - and this is where the function fails. Instead of printing the function name, it only provides the name of module.
How can I fix this function so that it only shows the function name, or scope & function name?
CodePudding user response:
Thank you leeharvey1. Posting your suggestions as an answer to help other community members.
From your module, you can try using $callerName = "{0} - {1}:" -f (Get-PSCallStack)[1].Location, (Get-PSCallStack)[1].FunctionName
.