Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于500个错误的Powershell脚本_Powershell - Fatal编程技术网

用于500个错误的Powershell脚本

用于500个错误的Powershell脚本,powershell,Powershell,我希望powershell脚本从多个服务器的IIS日志中获取所有500个条目。我写了一个脚本,在前几个小时从单个服务器获取500个。有人可以检查并帮助我如何获取多个服务器。我拥有的脚本: #Set Time Variable -60 $time = (Get-Date -Format "HH:mm:ss"(Get-Date).addminutes(-60)) # Location of IIS LogFile #$servers = get-content C:\Users\servers.t

我希望powershell脚本从多个服务器的IIS日志中获取所有500个条目。我写了一个脚本,在前几个小时从单个服务器获取500个。有人可以检查并帮助我如何获取多个服务器。我拥有的脚本:

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

# Location of IIS LogFile
#$servers = get-content C:\Users\servers.txt
$File = "\\server\D$\Logs\W3SVC89\"+"u_ex"+(get-date).ToString("yyMMddHH")+".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\Servers\results.csv

假设您的日志文件始终位于同一路径上,并且servers.txt包含您的服务器列表, 您可以读取服务器列表,然后使用foreach循环针对每个服务器执行代码: 类似于为每个服务器创建一个结果文件:

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

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

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

    $File = "\\$_\D$\Logs\W3SVC89\"+"u_ex"+(get-date).ToString("yyMMddHH")+".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\Servers\$_results.csv

 }

请注意,这将在每个服务器上按顺序运行代码,这可能会耗费时间。如果您面临持续时间问题,您可以尝试使用invoke命令和-asjob参数来启动代码asynchronousy

如果您的代码在单个服务器上运行良好,我会将其放入函数中,并将其作为参数传递给服务器。因此,您可以独立于代码管理服务器列表。检查其他可能是唯一的,例如日志路径。最后,如果您采用此方法,请确保使用-appendThank将结果附加到CSV。它正在工作,但有时在为多个服务器运行时被挂起。