How to force implied PowerShell Format-Table to repeat headers for repeated output?

前端 未结 2 374
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 01:55

I am trying to repeat *nix watch functionality as provided by johnrizzo1 here.

function Watch {
    [CmdletBinding(SupportsShouldProcess=$True,C         


        
相关标签:
2条回答
  • 2021-01-17 02:37

    Most likely this has other issues but simply changing

    $cmd.invoke();
    

    to

    $cmd.invoke() | ft;
    

    works for me

    0 讨论(0)
  • 2021-01-17 02:41

    This will work now, but your output is forced to be piped to Format-Table so it will always be in Table format.

    function Watch {
        [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]
        param (
            [Parameter(Mandatory=$False,
                       ValueFromPipeline=$True,
                       ValueFromPipelineByPropertyName=$True)]
            [int]$n = 10,
    
            [Parameter(Mandatory=$True,
                       ValueFromPipeline=$True,
                       ValueFromPipelineByPropertyName=$True)]
            [string]$command
        )
        process {
            $cmd = [scriptblock]::Create($command)
            While($True) {
                Clear-Host
                Write-Host "Command: " $command
                $cmd.Invoke() | Format-Table -HideTableHeaders:$false
                sleep $n
            }
        }
    }
    
    watch -n 1 '$PSVersionTable.PSVersion'
    
    0 讨论(0)
提交回复
热议问题