title: PowerShell script to monitor IIS logs for 500 errors every 10 minutes
body: <p>I'm trying to set up a script to monitor IIS 7.5 logs fro 500 errors. Now I can get it to do that OK but I would like it to check every 30 minutes. Quite naturally I don't want it to warn me about the previous 500 errors it has already reported.</p>
  
  <p>As you can see from the script below I have added a $time variable to take this into account, however I can't seem to find a way to use this variable. Any help would be appreciated.</p>
  
  <pre><code>#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
  </code></pre>
  
  <p>OK With KevinD's help and PowerGUI with a fair bit of trial and error, I got it working as I expected. Here's the finished product.</p>
  
  <pre><code>#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 }
  </code></pre>
  
  <p>Thanks again Kev you're a good man. Hope this code helps someone else out there.</p>
  
  <p>Here's</p>