powershell indentation

后端 未结 6 1642
小蘑菇
小蘑菇 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:57

    Check out this script http://poshcode.org/scripts/3386.html

    If you load up that Write-Verbose wrapper, you can set $WriteHostAutoIndent = $true and then just call Write-Host and it will be indented based on stack depth. So given these functions as you defined them originally:

    function myFn()
    {
       Write-Host "Start of myfn"
       myFnNested
       Write-Host "End of myfn"
    }
    function myFnNested()
    {
       Write-Host "Start of myFnNested"
       Write-Host "End of myFnNested"
    }
    

    With no changes, you can just dot-source a script file with that Write-Host wrapper function in it:

    C:\PS> . C:\Users\Jaykul\Documents\WindowsPowerShell\PoshCode\3386.ps1
    

    And then merely set the preference variable before you call your function:

    C:\PS> $WriteHostAutoIndent = $true
    C:\PS> myFn
      Start of myfn
        Start of myFnNested
        End of myFnNested
      End of myfn
    

    Beautiful indented output, like magic ;-)

提交回复
热议问题