【问题标题】:Why "Get-Content" is not refreshing contents live?为什么“Get-Content”不能实时刷新内容?
【发布时间】:2016-04-19 13:09:04
【问题描述】:

我有一个日志文件并使用命令Get-Content myLog.log –Wait 来显示该文件的内容,就像使用linux 等效的tail 一样。

我注意到内容没有像我从tail 知道的那样实时更新。内容仅在特定时间间隔后刷新。如何更改间隔,我想我必须为Wait 传递一个参数,以便它知道等待多长时间?

【问题讨论】:

    标签: windows powershell tail


    【解决方案1】:

    Get-Content cmdlet 不公开属性或函数来设置刷新间隔。您可能必须自己做:

    $linesPrinted = 0;
    
    while ($true) 
    { 
        $content = Get-Content myLog.log
        $currentLineCount = $content | Measure-Object -Line | select -expand Lines
    
        if ($currentLineCount -gt $linesPrinted)
        {
            $content[$linesPrinted .. $currentLineCount]
            $linesPrinted = $currentLineCount
        }
    
    
        Sleep -Milliseconds 100 
    }
    

    【讨论】:

    • 我必须以哪种格式保存此脚本才能使其可执行?
    • 要测试你的脚本,你应该开始Powershell_ISE(最好是管理员)。将脚本复制并粘贴到Script Pane 并按F5 执行它。稍后,您可以将脚本保存为ps1 文件并使用powershell 执行。
    • 谢谢,但是您的代码不能按预期工作,它总是从一开始就完全加载文件。
    • 是的,它现在完美运行 :) 谢谢!是否可以使用您的代码创建一个模块,以便我们可以像这样使用它:wintail path/to/file
    猜你喜欢
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 2016-04-18
    • 1970-01-01
    相关资源
    最近更新 更多