PowerShell ISE中如何使用“more”


在PowerShell控制台中,你可以使用cmd中的命令 “more.com”,也可以使用Out-Host –Paging,来让管道中的结果在控制台分页显示,用户可以按一个键继续查看下一页。

PS> Get-Process | more
PS> Get-Process | Out-Host -Paging

但是在PowerShell ISE中,这两种方式都不支持,原因是因为ISE不是一个真实的控制台,它本身并没有“可见行数”这个概念,而是一个不受限制的文本缓冲区。

此时为了防止大量结果瞬间流过你的屏幕,可以考虑自定义一个可以兼容ISE的“more

function Out-More 
{
    param 
    (
        $Lines = 10, 
        
        [Parameter(ValueFromPipeline=$true)]
        $InputObject 
    )
    
    begin 
    {
        $counter = 0 
    }
    
    process 
    {
        $counter++ 
        if ($counter -ge $Lines)
        {
            $counter = 0 
            Write-Host 'Press ENTER to continue' -ForegroundColor Yellow 
            Read-Host  
        }
        $InputObject 
    }
}

让你管道中的结果进入Out-More,使用 –Lines指定显示多少行数据纪录以后,让输出暂停:

PS> Get-Process | Out-More -Lines 4
 
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                      
-------  ------    -----      ----- -----   ------     -- -----------                                                                       
    100       9     1436       1592    51            6896 adb
    307      42    22332      12940   123            1424 AllShareFrameworkDMS
     42       4     1396       1284    19            1404 AllShareFrameworkManagerDMS
Press ENTER to continue
 
 
     81       7     1004        724    43            1388 armsvc
    202      25    50376      55320   234     8,06  13720 chrome
   1131      65    68672      93892   361   116,73  13964 chrome
    199      24    53008      52700   225     5,56  14768 chrome
Press ENTER to continue
 
 
    248      26    31348      44984   239    44,31  15404 chrome
    173      20    23756      25540   179     1,27  16492 chrome
    190      22    36316      39208   207     2,81  16508 chrome
    184      23    41800      44212   223     1,77  17244 chrome
Press ENTER to continue

原文链接:Using “more” in the PowerShell ISE

本文链接: https://www.pstips.net/using-more-in-the-powershell-ise.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注