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

标签 powershell scripting office365 exchange-server scripting-language

我无法根据 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'
}

它应该按预期工作。

请记住,您还可以像这样在Hash中创建startend datetime -Objects:
$trackparams = @{
        start = [datetime]"2018-08-01 12:01 AM"
        end   = [datetime]"2018-08-21 1:01 PM"  
        ResultSize = 'Unlimited'
        EventId    = 'SEND'
}

关于powershell - 无法使用Get-MessageTrackingLog作为Sender@domain.com.CSV导出外部电子邮件收件人的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51942921/

相关文章:

powershell - 可以获取在 PowerShell 中执行的最后一个命令的计数吗?

azure - PowerShell - 将导入的 csv 列添加到新导出的 csv 中

powershell - 如何从阵列中删除 powershell

linux - 使用变量写入文件而不重复

bash - 在同一文件夹终端脚本中复制多个同名文件

oauth-2.0 - Office 365 - 使用非交互式服务应用程序处理 IMAP 基本身份验证的停用

git - "git unable to read current working directory no error"- 豪华 git (windows 10)

linux - 如何将输出格式化为列?

python-3.x - 如何在 Python 的 Microsoft O365 日历中将日期作为查询传递给 get_events()

rest - 通过 Office 365 REST API 标记消息?