我写了一个非常基本的PowerShell日志尾标脚本,带有过滤器和着色。 一切都很好,除了过滤(通过过滤我的意思是不打印不必要的线)。当脚本读取不再写入的文件时,它会正确过滤掉行。但是当线条不断被写入并且脚本实时读取它时,它有时会打印出一条空行而不是空白行。
$host.ui.RawUI.WindowTitle = "ARMA 3 LOG VIEWER"
$dir = "E:\\SteamLibrary\\steamapps\\common\\Arma 3 Server\\SC"
$filter="*.rpt"
$latest = Get-ChildItem -Path $dir -Filter $filter | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.fullname
function Get-LogColor {
Param([Parameter(Position=0)]
[String]$LogEntry)
process {
if ($LogEntry.Contains("DEBUG") -or $LogEntry.Contains("[A3XAI Monitor]")) {Return "Green"}
elseif ($LogEntry.Contains("WARN") -or $LogEntry.Contains("Warn")) {Return "Yellow"}
elseif ($LogEntry.Contains("ERROR") -or $LogEntry.Contains("Error")) {Return "Red"}
elseif ($Logentry.Contains("infiSTAR.de")) {Return "Magenta"}
else {Return "White"}
}
}
Get-Content $latest.fullname -Wait | where {
$_ -notmatch "strange convex" -and
$_ -notmatch "Error: Bone" -and
$_ -notmatch "wrong size" -and
} | ForEach {Write-Host -ForegroundColor (Get-LogColor $_) $_}