Skip to content

Commit 339ef6b

Browse files
committed
Update Exchange-2016-MessageTracking-Gui.ps1
1 parent 30ebaf2 commit 339ef6b

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

PowerShell-Admin-Toys/Exchange-2016-MessageTracking-Gui.ps1

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030

3131
#Requires -Version 5.1
3232

33-
#region Embedded Resource
34-
35-
#endregion Embedded Resource
36-
3733
#region Variables
3834
#region XAML markup
3935
[xml]$Xaml = '<Window
@@ -111,16 +107,18 @@
111107
</Style>
112108
<Style TargetType="{x:Type DataGrid}">
113109
<Setter Property="Grid.Row" Value="2" />
110+
<Setter Property="HorizontalAlignment" Value="Stretch"/>
111+
<Setter Property="VerticalAlignment" Value="Stretch"/>
114112
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
115-
<Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
113+
<Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
116114
<Setter Property="AutoGenerateColumns" Value="True"/>
117115
<Setter Property="IsReadOnly" Value="True"/>
118116
<Setter Property="GridLinesVisibility" Value="All"/>
119117
<Setter Property="CanUserReorderColumns" Value="False"/>
120118
<Setter Property="RowHeaderWidth" Value="0"/>
121119
<Setter Property="HeadersVisibility" Value="Column"/>
122120
<Setter Property="ColumnWidth" Value="*"/>
123-
<Setter Property="Visibility" Value="Collapsed"/>
121+
<Setter Property="Visibility" Value="Collapsed"/>
124122
</Style>
125123
<Style TargetType="{x:Type StackPanel}" x:Key="TextBoxPanel">
126124
<Setter Property="Orientation" Value="Horizontal" />
@@ -238,7 +236,8 @@
238236
<TextBox Name="StartDateTextBox"
239237
Grid.Column="1"
240238
MaxLength="19"
241-
TabIndex="1" />
239+
TabIndex="1"
240+
Tag="Start" />
242241
</Grid>
243242
</StackPanel>
244243
<StackPanel Style="{StaticResource TextBoxPanel}">
@@ -255,7 +254,8 @@
255254
<TextBox Name="EndDateTextBox"
256255
Grid.Column="1"
257256
MaxLength="19"
258-
TabIndex="2" />
257+
TabIndex="2"
258+
Tag="End" />
259259
</Grid>
260260
</StackPanel>
261261
<StackPanel Style="{StaticResource TextBoxPanel}">
@@ -271,7 +271,8 @@
271271
272272
<TextBox Name="SenderTextBox"
273273
Grid.Column="1"
274-
TabIndex="3" />
274+
TabIndex="3"
275+
Tag="Sender" />
275276
</Grid>
276277
</StackPanel>
277278
<StackPanel Style="{StaticResource TextBoxPanel}">
@@ -287,7 +288,8 @@
287288
288289
<TextBox Name="RecipientTextBox"
289290
Grid.Column="1"
290-
TabIndex="4" />
291+
TabIndex="4"
292+
Tag="Recipients" />
291293
</Grid>
292294
</StackPanel>
293295
<StackPanel Style="{StaticResource TextBoxPanel}">
@@ -303,7 +305,8 @@
303305
304306
<TextBox Name="SubjectTextBox"
305307
Grid.Column="1"
306-
TabIndex="5" />
308+
TabIndex="5"
309+
Tag="MessageSubject" />
307310
</Grid>
308311
</StackPanel>
309312
<StackPanel Style="{StaticResource TextBoxPanel}">
@@ -320,7 +323,8 @@
320323
321324
<ComboBox Name="EventIdTextBox"
322325
Grid.Column="1"
323-
TabIndex="6">
326+
TabIndex="6"
327+
Tag="EventId" >
324328
</ComboBox>
325329
</Grid>
326330
</StackPanel>
@@ -354,10 +358,10 @@
354358
$MailEventId = [ordered]@{
355359
"All events" = "ANY"
356360
"DEFER: message delivery was delayed" = "DEFER"
357-
"DELIVER: message was delivered" = "DELIVER"
361+
"DELIVER: message was delivered" = "DELIVER"
358362
"DROP: message was dropped" = "DROP"
359363
"DSN: delivery status notification created" = "DSN"
360-
"RECEIVE: message was received to SMTP" = "RECEIVE"
364+
"RECEIVE: message was received to SMTP" = "RECEIVE"
361365
"SEND: A message was sent by SMTP" = "SEND"
362366
}
363367
$GitHubPage = "https://github.com/Inestic/PowerShell-Admin-Toys"
@@ -417,26 +421,46 @@ function Get-ExchangeSnapin {
417421

418422
function Start-MessageTracking {
419423

420-
[String]$TrackingCommand = [String]::Format("Get-MessageTrackingLog -Start {0} -End {1}", $StartDateTextBox.Text, $EndDateTextBox.Text)
421-
$SenderTextBox, $RecipientTextBox, $SubjectTextBox
422-
424+
[String]$TrackingCommand = [String]::Format("Get-MessageTrackingLog -Start ""{0}"" -End ""{1}""", $StartDateTextBox.Text, $EndDateTextBox.Text)
425+
$SenderTextBox, $RecipientTextBox, $SubjectTextBox | ForEach-Object -Process {
426+
if ($_.Text -ne [string]::Empty) { $TrackingCommand += " -{0} ""{1}"""-f $_.Tag, $_.Text }
427+
}
423428

424-
#if ([string]::Empty -ne $SenderTextBox.Text) {$TrackingCommand += " -Sender ""{0}"""-f $SenderTextBox.Text}
429+
if ($EventIdTextBox.SelectedIndex -ne 0) { $TrackingCommand += " -{0} {1}"-f $EventIdTextBox.Tag, $MailEventId[$EventIdTextBox.SelectedIndex] }
430+
$TrackedMessage = New-Object System.Collections.ArrayList($null)
431+
Get-ExchangeServer | ForEach-Object -Process {
432+
$StatusBarTextBlock.Text = "Started tracking mail"
433+
[String]$ServerCommand = '{0} -Server "{1}" -ResultSize Unlimited'-f $TrackingCommand, $_.Name
434+
Invoke-Expression -Command $ServerCommand | ForEach-Object -Process {
435+
$Property = [ordered]@{}
436+
$Property.TimeStamp = $_.Timestamp
437+
$Property.Server = $_.ServerHostname.ToLower()
438+
$Property.EventId = $_.EventId
439+
$Property.Sender = $_.Sender.ToLower()
440+
$Property.Recipients = [string]::join(",",$_.recipients).ToLower()
441+
$Property.Subject = $_.MessageSubject
442+
$Property.RecipientCount = $_.RecipientCount
443+
[Void]$TrackedMessage.Add((New-Object -TypeName PSObject -Property $Property))
444+
}
445+
}
425446

447+
$StatusBarTextBlock.Text = "Found: {0}"-f $TrackedMessage.Count
448+
if ($TrackedMessage.Count -gt 0)
449+
{
450+
$FoundMailDataGrid.ItemsSource = $TrackedMessage | Sort-Object -Property Timestamp -Descending
451+
$FoundMailDataGrid.Visibility = "Visible"
452+
}
426453

427-
428454
}
429455
#endregion Functions
430456

431457
Add-Type -AssemblyName PresentationFramework
432458

433459
$Gui = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $Xaml))
434-
$Xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object -Process {
435-
Set-Variable -Name ($_.Name) -Value $Gui.FindName($_.Name)
436-
}
437-
438-
$StartDateTextBox.Text = [DateTime]::Today
439-
$EndDateTextBox.Text = [DateTime]::Today.AddSeconds(-1)
460+
$Xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object -Process { Set-Variable -Name ($_.Name) -Value $Gui.FindName($_.Name) }
461+
$Today = [DateTime]::Today
462+
$StartDateTextBox.Text = "{0} {1}"-f $Today.ToShortDateString(), $Today.ToLongTimeString()
463+
$EndDateTextBox.Text = "{0} {1}"-f $Today.ToShortDateString(), $Today.AddDays(1).AddSeconds(-1).ToLongTimeString()
440464
$EventIdTextBox.ItemsSource = $MailEventId.Keys
441465
$EventIdTextBox.SelectedItem = $EventIdTextBox.ItemsSource | Select-Object -First 1
442466
$Window.add_Loaded({Get-ExchangeSnapin})

0 commit comments

Comments
 (0)