1

I cannot export the list of the external recipient email address and its total number of email based on multiple inputs of the InternalSender.CSV list:

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

This is the script code:

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
}

The error is:

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
halfer
  • 19,824
  • 17
  • 99
  • 186
Senior Systems Engineer
  • 1,061
  • 2
  • 27
  • 63

1 Answers1

3

Like the error states, the problem is your hash definition.

Change:

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

To:

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

And it should work as expected.

Keep in mind that you can also make start and end datetime-Objects in the Hash like this:

$trackparams = @{
        start = [datetime]"2018-08-01 12:01 AM"
        end   = [datetime]"2018-08-21 1:01 PM"  
        ResultSize = 'Unlimited'
        EventId    = 'SEND'
}
Paxz
  • 2,959
  • 1
  • 20
  • 34