无法使用 Get-MessageTrackingLog as Sender@domain.com.CSV 导出外部电子邮件收件人列表?

Unable to export list of external email recipient using Get-MessageTrackingLog as Sender@domain.com.CSV?

我无法根据 InternalSender.CSV 列表的多个输入导出外部收件人电子邮件地址列表及其电子邮件总数:

Email
John.Larkin@domain.com
Breville.May@domain.com
Katie.Brunetti@domain.com
Anna.Belle@domain.com

这是脚本代码:

Import-PSSession (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://VM-MAILSVR01/PowerShell/ -Authentication Kerberos) -AllowClobber

$resultFile = 'C:\TMP\Results.CSV'
$inputFile = 'C:\TMP\InternalSender.csv'

$trackparams = @{
    $start = "2018-08-01 12:01 AM"
    $end   = "2018-08-21 1:01 PM"  
    ResultSize = 'Unlimited'
    EventId    = 'SEND'
}

$AcceptedEmailDomains = '@' + ((Get-AcceptedDomain).Name -join '|@')

Write-host "`n Time window between $($trackparams.start.ToString("dd/MM/yyyy HH:MM")) until $($trackparams.end.ToString("dd/MM/yyyy HH:MM"))" -ForegroundColor Yellow
Import-Csv $inputFile |
    ForEach-Object{
        $email = $_.email
        Write-host "Email address: $email" -ForegroundColor Green   
        Get-TransportService |
            ForEach-Object {Get-Messagetrackinglog -Server $_.Name @trackparams -Sender $email} |
                # The below line is set for filtering all external emails not in Exchange Accepted Email Domain lists.
                Where-Object { $_.Recipient -notmatch $AcceptedEmailDomains } |
                Group-Object -NoElement Recipient |
                Sort-Object Count -Descending |
                Select Recipient, Count |
                Export-CSV -Path "C:\TMP$($_.email).CSV" -NoTypeInformation
}

错误是:

A null key is not allowed in a hash literal.
At line:7 char:2
+     $start = "2018-08-01 12:01 AM"
    + CategoryInfo          : InvalidOperation: (System.Collections.Hashtable:Hashtable) [], RuntimeException
    + FullyQualifiedErrorId : InvalidNullKey

You cannot call a method on a null-valued expression.
At line:15 char:38
+ Write-host "`n Time window between $($trackparams.start.ToString("dd/MM/yyyy HH: ...
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:15 char:95
+ ... :MM")) until $($trackparams.end.ToString("dd/MM/yyyy HH:MM"))" -ForegroundColor  ...
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

与错误状态一样,问题出在您的哈希定义上。

变化:

$trackparams = @{
    $start = "2018-08-01 12:01 AM"
    $end   = "2018-08-21 1:01 PM"  
    ResultSize = 'Unlimited'
    EventId    = 'SEND'
}

收件人:

$trackparams = @{
    start = "2018-08-01 12:01 AM"
    end   = "2018-08-21 1:01 PM"  
    ResultSize = 'Unlimited'
    EventId    = 'SEND'
}

它应该按预期工作。

请记住,您还可以像这样在哈希中创建 startend datetime-对象:

$trackparams = @{
        start = [datetime]"2018-08-01 12:01 AM"
        end   = [datetime]"2018-08-21 1:01 PM"  
        ResultSize = 'Unlimited'
        EventId    = 'SEND'
}