Powershell 为什么我在这个脚本中得到一个空管道错误

Powershell 为什么我在这个脚本中得到一个空管道错误,powershell,Powershell,我是一个学习Powershell的新手,被赋予了让以下代码正常工作的任务,我不断在“Gives Empty Pipeline Error”一行中遇到空管道错误。经过几个小时的研究,我仍然不明白是什么原因造成了这种情况。脚本应该搜索Application.evtx日志并返回过去24小时内的所有错误。我非常感谢任何能为我指明正确方向的帮助。 代码如下: #look for Errors script #Creates Function named CheckLogs Function CheckL

我是一个学习Powershell的新手,被赋予了让以下代码正常工作的任务,我不断在“Gives Empty Pipeline Error”一行中遇到空管道错误。经过几个小时的研究,我仍然不明白是什么原因造成了这种情况。脚本应该搜索Application.evtx日志并返回过去24小时内的所有错误。我非常感谢任何能为我指明正确方向的帮助。 代码如下:

#look for Errors script

#Creates Function named CheckLogs
Function CheckLogs()
{
    # Defines a named parameter $logfile as a string
    param ([string]$logfile)

    if(!$logfile) {write-host "Usage: ""C:\Windows\System32\winevt\Logs\Application.evtx"""; exit}

    # Accesses the file stored in $logfile variable and looks for the string "ERROR" 
    cat $logfile | Select-string "ERROR" -SimpleMatch |select -expand line |

        foreach

        {

            $_ -match '(.+)\s\[(ERROR)\]\S(.+)'| Out-Null

            new-object psobject -Property@{Timestamp=[datetime]$matches[1];Error=$matches[2]}

| #Gives Empty Pipeline Error

            where {$_.timestamp -gt (get-date).AddDays(-1)}

            $error_time=[datetime]($matches[1])

            if ($error_time -gt (Get-Date).AddDays(-1))

            {
                write-output "CRITICAL: There is an error in the log file $logfile around

                    $($error_time.ToShortTimeString( ))"; exit(2)

            }
        }

    write-output "OK: There were no errors in the past 24 hours."

}

CheckLogs "C:\Windows\System32\winevt\Logs\Application.evtx" #Function Call

不能单独将管道
|
字符放在一行上。您可以用
|
结束一行,然后继续下一行的管道

这应该起作用:

new-object psobject -Property@{Timestamp=[datetime]$matches[1];Error=$matches[2]} | 
where {$_.timestamp -gt (get-date).AddDays(-1)}