0

我为一家拥有一些负载平衡的 Web 服务器的公司工作。我创建了一个 PowerShell 脚本,它针对我们所有的 IIS 服务器并提取 500 个错误条目,然后提取客户端 IP 地址,并显示该客户端 IP 地址导致 500 错误的次数(是的,我知道我们可以为此使用 LogParser - 但这是帮助学习一些基本 PowerShell 技能的借口)。

现在,我的输出显示了 Web 服务器名称、计数和 IP 地址。理想情况下,我希望计数汇总所有 Web 服务器的计数并显示 IP 地址。我已经包含了下面的代码,包括实际输出和所需的输出。

$source      = "WebServer"
$destination = "LogServer01"
$date        = "190122"

if (Test-Path \\$destination\Drive$\Temp\LogFiles\IISLogs\"IISLogs-for-$($date)".txt) {
    Remove-Item \\$destination\Drive$\Temp\LogFiles\IISLogs\"IISLogs-for-$($date)".txt  
}
for ($i = 1; $i -lt 13; $i++) {
    if ($i -lt 10) {
        $serverName = "$($source)0$($i)"
    } else {
        $serverName = "$($source)$($i)"
    }
    # Grabbing the logs
    Get-Content -Path \\$serverName\D$\LogFiles\WebSite\W3SVC20000\"u_ex$($date)*.log" |
        Where-Object { $_.Contains(" 500")} |
        ForEach-Object { $_.Split(" ")[15] } |
        Group-Object |
        Where-Object { $_.Count -gt 1 } |
        Where-Object { $_.Name -ne "-" } |
        Sort-Object Count -Descending |
        Format-Table Count, Name >> \\$destination\D$\Temp\LogFiles\IISLogs\"IISLogs-for-$($date)".txt
};

期望的输出:

计数名称
17 186.0.25.8
15 202.58.5.16
12 96.58.1.58

实际输出:

Webserver01 计数名称
              5 186.0.25.8
              3 202.58.5.16
              2 96.58.1.58
Webserver02 计数名称
              4 186.0.25.8
              2 202.58.5.16
              1 96.58.1.58
Webserver03 计数名称
              4 186.0.25.8
              1 202.58.5.16
              2 96.58.1.58
4

2 回答 2

0

简单方法:将服务器名称添加为计算属性:

... |
Group-Object -NoElement |
Select-Object @{n='ServerName';e={$serverName}}, Count, Name |
...

但是,这不会给你完全想要的输出。相反,你会得到:

ServerName 计数名称
---------- ----- ----
网络服务器01 5 186.0.25.8
网络服务器01 3 202.58.5.16
网络服务器01 2 96.58.1.58
网络服务器02 4 186.0.25.8
网络服务器02 2 202.58.5.16
网络服务器02 1 96.58.1.58
网络服务器03 4 186.0.25.8
网络服务器03 1 202.58.5.16
网络服务器03 2 96.58.1.58

但是,我认为这种格式更可取,因为通过替换Format-Table ... >> ...Export-Csv可以将数据导出为其他应用程序可以轻松读取的格式。

如果您希望输出与您的问题完全相同,则需要创建自定义输出,例如通过格式运算符( -f)。

$data = Get-Content -Path ... |
        ...
        Sort-Object Count -Descending

'{0,-15} Count  Name' -f $serverName | Set-Content 'C:\output.txt'
$data | ForEach-Object {
    '{0,-15}  {1,-5}  {2}' -f '', $_.Count, $_.Name
} | Add-Content 'C:\output.txt'
于 2019-01-24T00:59:58.327 回答
0

这是我想出的解决方案,我所要做的就是在 Format-Table cmdlet 中添加 -Group By "Name" 并将 -Property 设置为 "Count",这是我为感兴趣的人准备的代码:

$source         =       "Webserver"
$destination    =       "LogServer01" 
$date           =       "190122"
if (Test-Path \\$destination\D$\Temp\LogFiles\IISLogs\"IISLogs-for-$($date)".txt) {
  Remove-Item \\$destination\D$\Temp\LogFiles\IISLogs\"IISLogs-for-$($date)".txt  
}
for($i = 1; $i -lt 13; $i++)
    {if ($i -lt 10)
        {
            $serverName = "$($source)0$($i)"
        }
     else {
            $serverName = "$($source)$($i)"
        }
        Get-Content -Path \\$serverName\D$\LogFiles\WebSite\W3SVC20000\"u_ex$($date)*.log" |
        Where-Object {   $_.Contains(" 500")} |
        ForEach-Object {$_.split(" ")[15]} |
        Group-Object | ## Grouping the data by name column (IP Address)
        Where-Object {$_.Count -gt 1} |
        Where-Object {$_.Name -ne "-"} |
        Sort-Object Count -Descending |
        Format-Table -GroupBy Name -Property Count >> \\$destination\D$\Temp\LogFiles\IISLogs\"IISLogs-for-$($date)".txt
            };
于 2019-01-24T19:00:41.257 回答