代码之家  ›  专栏  ›  技术社区  ›  Jay Pete

Powershell:获取内容-等待一组对象并重新绘制显示表

  •  0
  • Jay Pete  · 技术社区  · 9 年前

    Get-Content $logFile -wait | ForEach  { if ($_.Contains("[OK]")){ Write-Host -ForegroundColor Green $_ } elseif ($_.Contains("[FAIL]")){ Write-Host -ForegroundColor Red $_ } elseif ($_.Contains("[INFO]")){ Write-Host -ForegroundColor Yellow $_ } else { Write-Host $_ } } 
    

    这个日志文件将永远不会包含超过100行,所有相关的应用程序之一。15服务。现在,我只是使用日志文件上的上述尾部将这100行输出到屏幕上。

    但我真正想做的是显示一个有15行的表,当我从日志中获得新行时,不断更新该表,以在我从日志中获得新信息时显示新信息。

    我试过搜索这样的表格示例,但没有找到。有可能吗?如果有,请给我一个关于它的链接。

    1 回复  |  直到 9 年前
        1
  •  0
  •   BenH    9 年前

    $log = @()
    Get-Content $logFile -wait |
        ForEach-Object {
        switch ($_) {
            {$_.Contains("[OK]")} {
                $logentry = [pscustomobject]@{
                    'Status'  = 'Success'
                    'Message' = ($_ -split '\[OK\]')[-1]
                }
                $log += $logentry
            }
            {$_.Contains("[FAIL]")} {
                $logentry = [pscustomobject]@{
                    'Status'  = 'Failure'
                    'Message' = ($_ -split '\[FAIL\]')[-1]
                }
                $log += $logentry
            }
            {$_.Contains("[INFO]")} {
                $logentry = [pscustomobject]@{
                    'Status'  = 'Info'
                    'Message' = ($_ -split '\[INFO\]')[-1]
                }
                $log += $logentry
            }
            default {
                $logentry = [pscustomobject]@{
                    'Status'  = 'Unknown'
                    'Message' = $_
                }
                $log += $logentry
            }
        }
        Clear-Host
        foreach ($logentry in $log) {
            switch ($logentry.Status) {
                'Success' { Write-Host -ForegroundColor Green $logentry }
                'Failure' { Write-Host -ForegroundColor Red $logentry }
                'Info' { Write-Host -ForegroundColor Yellow $logentry }
                default { Write-Host $logentry }
            }
        }
    }
    
    $log | Export-CSV C:\Example\path.csv -NoTypeInformation
    
    推荐文章