我希望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.csvI 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
更多推荐
发布评论