需要Powershell脚本帮助从多个服务器的iis日志文件中读取500个错误

时间:2015-11-04 15:09:58

标签: powershell iis-7.5

#Set Time Variable -60
$time = (Get-Date -Format "HH:mm:ss"(Get-Date).addminutes(-60))

# Location of IIS LogFile
$servers = get-content C:\Users\AXL3011\Desktop\servers.txt

$servers| foreach{
    #inside the foreach loop $_ will represent the current server

    $File = "\\$_\E$\IISLogs\W3SVC1\*.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="DateTime"; e={Get-Date ("$($_.date) $($_.time)")}},sip,csuristem,scstatus | ? { $_.DateTime -ge $time } | Out-File C:\Users\AXL3011\Desktop\$_results.csv
}

====== 我收到如下错误:

PS C:\Users\AXL3011\Desktop> C:\Users\AXL3011\Desktop\abc1.ps1
You cannot call a method on a null-valued expression.
At C:\Users\AXL3011\Desktop\abc1.ps1:36 char:24
+       $Row = $Row.Split <<<< (" ")
    + CategoryInfo          : InvalidOperation: (Split:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (2:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (3:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (4:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (5:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (6:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (7:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (8:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (9:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (10:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (11:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (12:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (13:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (14:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (15:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (16:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (17:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (18:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (19:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (20:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\AXL3011\Desktop\abc1.ps1:40 char:36
+         $AddRow.$ColumnName = $Row[ <<<< $i]
    + CategoryInfo          : InvalidOperation: (21:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

0 个答案:

没有答案