PowerShell脚本,用于每10分钟监视IIS日志中的500个错误

PowerShell脚本,用于每10分钟监视IIS日志中的500个错误,iis,powershell,iis-7.5,monitoring,Iis,Powershell,Iis 7.5,Monitoring,我正在尝试设置一个脚本来监视IIS7.5日志中的500个错误。现在我可以让它这样做,但我希望它每30分钟检查一次。很自然,我不想让它警告我之前已经报告的500个错误 从下面的脚本中可以看到,我添加了一个$time变量来考虑这个问题,但是我似乎找不到使用这个变量的方法。任何帮助都将不胜感激 #Set Time Variable -30 $time = (Get-Date -Format hh:mm:ss (Get-Date).addminutes(-30)) # Location of IIS L

我正在尝试设置一个脚本来监视IIS7.5日志中的500个错误。现在我可以让它这样做,但我希望它每30分钟检查一次。很自然,我不想让它警告我之前已经报告的500个错误

从下面的脚本中可以看到,我添加了一个$time变量来考虑这个问题,但是我似乎找不到使用这个变量的方法。任何帮助都将不胜感激

#Set Time Variable -30
$time = (Get-Date -Format hh:mm:ss (Get-Date).addminutes(-30))
# Location of IIS LogFile
$File = "C:\Users\here\Documents\IIS-log\"+"u_ex"+(get-date).ToString("yyMMdd")+".log"
# Get-Content gets the file, pipe to Where-Object and skip the first 3 lines.
$Log = Get-Content $File | where {$_ -notLike "#[D,S-V]*" }
# Replace unwanted text in the line containing the columns.
$Columns = (($Log[0].TrimEnd()) -replace "#Fields: ", "" -replace "-","" -replace "\(","" -replace "\)","").Split(" ")
# Count available Columns, used later
$Count = $Columns.Length
# Strip out the other rows that contain the header (happens on iisreset)
$Rows = $Log | where {$_ -like "*500 0 0*"}
# Create an instance of a System.Data.DataTable
#Set-Variable -Name IISLog -Scope Global
$IISLog = New-Object System.Data.DataTable "IISLog"
# Loop through each Column, create a new column through Data.DataColumn and add it to the DataTable
foreach ($Column in $Columns) {
  $NewColumn = New-Object System.Data.DataColumn $Column, ([string])
  $IISLog.Columns.Add($NewColumn)
}
# Loop Through each Row and add the Rows.
foreach ($Row in $Rows) {
 $Row = $Row.Split(" ")
 $AddRow = $IISLog.newrow()
 for($i=0;$i -lt $Count; $i++) {
 $ColumnName = $Columns[$i]
 $AddRow.$ColumnName = $Row[$i]
}
 $IISLog.Rows.Add($AddRow)
 }
 $IISLog | select time,csuristem,scstatus
好的,在KevinD的帮助下,在PowerGUI中进行了一些尝试和错误,我使它按照我的预期工作。这是成品

#Set Time Variable -30
$time = (Get-Date -Format "HH:mm:ss"(Get-Date).addminutes(-30))
# Location of IIS LogFile
$File = "C:\Users\here\Documents\IIS-log\"+"u_ex"+(get-date).ToString("yyMMdd")+".log"
# Get-Content gets the file, pipe to Where-Object and skip the first 3 lines.
$Log = Get-Content $File | where {$_ -notLike "#[D,S-V]*" }
# Replace unwanted text in the line containing the columns.
$Columns = (($Log[0].TrimEnd()) -replace "#Fields: ", "" -replace "-","" -replace "\(","" -replace "\)","").Split(" ")
# Count available Columns, used later
$Count = $Columns.Length
# Strip out the other rows that contain the header (happens on iisreset)
$Rows = $Log | where {$_ -like "*500 0 0*"}
# Create an instance of a System.Data.DataTable
#Set-Variable -Name IISLog -Scope Global
$IISLog = New-Object System.Data.DataTable "IISLog"
# Loop through each Column, create a new column through Data.DataColumn and add it to the DataTable
foreach ($Column in $Columns) {
  $NewColumn = New-Object System.Data.DataColumn $Column, ([string])
  $IISLog.Columns.Add($NewColumn)
}
# Loop Through each Row and add the Rows.
foreach ($Row in $Rows) {
  $Row = $Row.Split(" ")
  $AddRow = $IISLog.newrow()
  for($i=0;$i -lt $Count; $i++) {
    $ColumnName = $Columns[$i]
    $AddRow.$ColumnName = $Row[$i]
  }
  $IISLog.Rows.Add($AddRow)
  }
  $IISLog | select @{n="Time"; e={Get-Date -Format "HH:mm:ss"("$($_.time)")}},csuristem,scstatus | ? { $_.time -ge $time }
再次感谢你,凯文,你是个好人。希望这段代码能帮助其他人


以下是

尝试将最后一行更改为:

$IISLog | select @{n="DateTime"; e={Get-Date ("$($_.date) $($_.time)")}},csuristem,scstatus | ? { $_.DateTime -ge $time }
在select中,我们将连接日期和时间字段,并将它们转换为日期对象,然后选择此字段大于$time变量的行

您还需要更改$time变量:

$time = (Get-Date).AddMinutes(-30)

这里需要的是DateTime对象,而不是字符串。

Hi-Kev,谢谢您的尝试,但我得到的输出与以前相同。我看到了问题所在。在脚本的第2行中,从$time中删除-Format参数。我将更新我的答案以反映这一点。对不起,伙计,现在没有输出!在你提出建议之前,我确实尝试过将-格式添加到你添加的行中,得到了一些不同的结果,但它们似乎在改变时间并反转结果。在最后一行中,当你进行选择,然后进行筛选时,你肯定想与DateTime对象进行比较。如果你没有得到任何结果,你确定在过去的30分钟内实际上有500个错误吗?绝对肯定有500个错误,我已经确定有500个错误,谢谢你把更正后的脚本分享回来。对于我需要做的事情来说,这是一个很好的开始。