LoginSignup
0
1

More than 5 years have passed since last update.

Exchange Serverの受信ログをPowerShellから確認する方法

Posted at

準備

mailaddr.txtというファイルに下記のように1行ずつ受信ログを確認したいメールアドレスを記述します。

mailaddr.txt
hoge@example.com
hogehoge@example.jp
hoge+admin@example.co.jp

ログの出力先フォルダを作成しておきます。

mkdir C:\temp\exLog

PowerShellの準備

下記スクリプトは、Exchange Management Shellがあるサーバ上で実行しないとエラーになりますので
Exchange Server上に保存します。

ExchangeLog.ps1
$i=1
$re = "[a-z0-9!#\$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+/=?^_`{|}~-]+)*@"

$exSh = ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto;"

foreach ($l in Get-Content mailaddr.txt) {

    $s = [regex]::Match($l, $re, "IgnoreCase").Value.Replace("@","")

    $exCmd = "Get-MessageTrackingLog -Recipients " + $l + " | Export-Csv C:\temp\exLog\" + $s + ".csv -Encoding Default -NoTypeInformation"
    Start-Process -FIlePath powershell.exe -ArgumentList "-command  $exSh  $exCmd" -Wait 
    $i++
}

Write-Host "処理が完了しました。"

実行

PS C:\Scripts>.\ExchangeLog.ps1

結果


PSComputerName  RunspaceId  Timestamp   ClientIp    ClientHostname  ServerIp    ServerHostname  SourceContext   ConnectorId Source  EventId InternalMessageId   MessageId   Recipients  RecipientStatus TotalBytes  RecipientCount  RelatedRecipientAddress Reference   MessageSubject  Sender  ReturnPath  MessageInfo MessageLatency  MessageLatencyType  EventData
hoge.local  33f3c2d5-e2c9-4f87-86b2-aa5f1   2018/1/25 1:15  XXX.XXX.XXX.XXX hoge.example.com    192.168.1.1 exchange    08D4C9B6B47A7BDD;2018-01-24T16:15:32.793Z;0 HOGE\HOGE   SMTP    RECEIVE 16794221    20180124161532.189ED61664@hoge  System.String[] System.String[] 47904   1           Requests statistics -   hogehoge@example.jp hoge2@example.jp    00A: NTS:       None    System.Collections.Generic.KeyValuePair`2[System.String,System.Object][]

配列で出力されしまう箇所があるため、下記blogで解説されているようにSelectコマンドを利用して出力します。
メッセージ追跡ログを CSV ファイルに出力する方法

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1