powershell indentation

后端 未结 6 1637
小蘑菇
小蘑菇 2021-02-14 12:05

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

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-14 12:56

    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.

提交回复
热议问题