I\'m writing a large script that deploys an application. This script is based on several nested function calls.
Is there any way to \"ident\" the output based on the dep
You could use a wrapper function around write-host
which used $MyInvocation
to determine the stack depth to create a number of spaces to prefix the message.
Combine this with the -scope ‹n›
parameter of Get-Variable
to pull out each calling level… something like the showstack
function adapted from Windows PowerShell In Action (Payette, 1st Ed):
function ShowStack {
trap { continue; }
0..100 | Foreach-Object {
(Get-Variable -scope $_ 'MyInvocation').Value.PositionMessage -replace "`n"
}
}
You'll need the maximum value of $_
in the pipeline before Get-Variable
fails for scope count being too high.