Powershell脚本可存在500个错误(Powershell Script for 500 errors)

编程入门 行业动态 更新时间:2024-10-24 22:22:03
Powershell脚本可存在500个错误(Powershell Script for 500 errors)

我希望power shell脚本从多个服务器的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

I want power shell script to fetch all 500 entries from IIS logs from multiple servers. I have written a script that fetches 500 from single servers for previous hours. Could someone check and help me how I can go for fetching multiple servers. Script that I have:

#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-command和-asjob参数来启动代码异步

Assuming your logfile is always on the same path, and that servers.txt contains you server list, you can read the server list then execute your code against each one using a foreach loop : something like that ( a result file is create for each server) :

#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 }

Note that this will run your code sequentially on each of your server wich can be time consumming. If you are facing duration issue, you can try to use invoke-command and the -asjob parameter in order to launch you code asynchronoulsy

更多推荐

本文发布于:2023-08-01 11:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357934.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   错误   Powershell   errors   Script

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!