计算结果总和

时间:2019-02-16 00:09:26

标签: powershell sum

当我运行以下未汇总的代码时,我将得到如下正确的结果。

$StartDate = Get-Date "2/14/2019";
$EndDate = $StartDate.AddDays(1);
[string] $email = xxxx@xxx.com;

$ExchServers = Get-ExchangeServer | where {
    $_.isHubTransportServer -eq $true -or
    $_.isMailboxServer -eq $true
} | Select-Object Name
$Servers = $ExchServers.Name 
foreach ($server in $Servers) {
    Get-MessageTrackingLog -Server $server -Start $StartDate -End $EndDate -ResultSize unlimited -Eventid “receive” -Source "storedriver" -Sender $email |
        Group-Object -Property Sender |
        Select-Object Count |
        sort Count -Desc |
        ft -Auto
    Get-MessageTrackingLog -Server $server -Start $StartDate -End $EndDate -ResultSize unlimited -Eventid “deliver” -Source "storedriver" -Recipients $email |
        Group-Object -Property Sender |
        Select-Object Count |
        sort Count -Desc |
        ft -Auto
}
Count
------
4

Count
------
2
2
1
1
1
1

已发送:4;收到:8

但是,当我尝试将其与| ForEach { $totalRsvd++ }

进行汇总时
$StartDate = Get-Date "2/14/2019"
$StartDate
$EndDate = $StartDate.AddDays(1)
[string] $email = xxxx@xxx.com;

$totalSent = 0;
$totalRsvd = 0;
$ExchServers = Get-ExchangeServer | where {
    $_.isHubTransportServer -eq $true -or
    $_.isMailboxServer -eq $true
} | Select-Object Name
$Servers = $ExchServers.Name 
foreach ($server in $Servers){
    Get-MessageTrackingLog -Server $server -Start $StartDate -End $EndDate -ResultSize unlimited -Eventid “receive” -Source "storedriver" -Sender $email |
        Group-Object -Property Sender |
        Select-Object Count | sort Count -Desc |
        ft -Auto |
        ForEach { $totalSent++ } 
    Get-MessageTrackingLog -Server $server -Start $StartDate -End $EndDate -ResultSize unlimited -Eventid “deliver” -Source "storedriver" -Recipients $email |
        Group-Object -Property Sender |
        Select-Object Count |
        sort Count -Desc |
        ft -Auto |
        ForEach { $totalRsvd++ }
}
Write-Host "Email Qty sent: ", $totalSent; 
Write-Host "Email Qty Received: ", $totalRsvd 
Email Qty sent:  5
Email Qty Received:  10

结果不正确。

0 个答案:

没有答案