@@ -251,6 +251,18 @@ function ConstructHeader
251251 Write-Output $Dictionary
252252}
253253
254+ function CreateHeaderObject
255+ {
256+ $Obj = New-Object PSObject - Property @ {
257+ " QueryString" = " "
258+ " ResponseString" = " "
259+ " RequestHeader" = @ {}
260+ " ResponseHeader" = @ {}
261+ }
262+
263+ Write-Output $Obj
264+ }
265+
254266function GetHTTPRequestInformation
255267{
256268 param (
@@ -271,29 +283,35 @@ function GetHTTPRequestInformation
271283
272284
273285 $HTTPTraffic = @ ()
286+ $HeaderObject = CreateHeaderObject
274287 # Parse relevant information and store in readable/accessible format
275- for ($I = 0 ; $I -lt $RequestAndResponseEvents.length - 1 ; $I = $I + 2 )
288+ for ($I = 0 ; $I -lt $RequestAndResponseEvents.length ; $I ++ )
276289 {
277- if ($RequestAndResponseEvents [$I ].ID -eq 403 -and $RequestAndResponseEvents [$I + 1 ].ID -eq 404 )
278- {
279- $QueryString = $RequestAndResponseEvents [$I ].RemoteProperties[4 ] + $RequestAndResponseEvents [$I ].RemoteProperties[5 ] + $RequestAndResponseEvents [$I ].RemoteProperties[6 ]
280- $ResponseString = $RequestAndResponseEvents [$I + 1 ].RemoteProperties[3 ] + " " + $RequestAndResponseEvents [$I + 1 ].RemoteProperties[4 ]
290+ $CurrentID = $RequestAndResponseEvents [$I ].ID
281291
282- $Request = ConstructHeader - Event $HeaderEvents [$I ]
283- $Response = ConstructHeader - Event $HeaderEvents [$I + 1 ]
292+ if ($CurrentID -eq 403 )
293+ {
294+ $HeaderObject.QueryString = $RequestAndResponseEvents [$I ].RemoteProperties[4 ] + $RequestAndResponseEvents [$I ].RemoteProperties[5 ] + $RequestAndResponseEvents [$I ].RemoteProperties[6 ]
295+ $HeaderObject.RequestHeader = ConstructHeader - Event $HeaderEvents [$I ]
296+
297+ }
298+ else # Event is a 404
299+ {
300+ $HeaderObject.ResponseString = $ResponseString = $RequestAndResponseEvents [$I ].RemoteProperties[3 ] + " " + $RequestAndResponseEvents [$I ].RemoteProperties[4 ]
301+ $HeaderObject.ResponseHeader = ConstructHeader - Event $HeaderEvents [$I ]
302+ }
284303
285- $Obj = New-Object PSObject - Property @ {
286- " QueryString" = $QueryString
287- " ResponseString" = $ResponseString
288- " RequestHeader" = $Request
289- " ResponseHeader" = $Response
290- }
291- $HTTPTraffic += $Obj
304+ if (($CurrentID -eq 404 ) -or $I -eq ($RequestAndResponseEvents.length - 1 ) -or ($RequestAndResponseEvents [$I + 1 ].ID -eq 403 ))
305+ {
306+ # Begin reconstructing next header if current event is 404 (response), at the end of events list, or the next event represents a request
307+ $HTTPTraffic += $HeaderObject
308+ $HeaderObject = CreateHeaderObject # Clear object for next iteration of loop
292309 }
293310
294- else
311+ if (( $I % 2 -eq 0 -and $CurrentID -eq 404 ) -or ( $I % 2 -eq 1 -and $CurrentID -eq 403 ))
295312 {
296- Write-Error " Unable to match request and response headers"
313+ # Expecting each 403 to be followed by a 404. Each 403 should have an even index and each 404 should have an odd index in the list.
314+ Write-Warning " Unable to match request and response headers"
297315 }
298316 }
299317
@@ -316,17 +334,60 @@ function AggregateOutputObject
316334
317335 [parameter (Mandatory = $true , Position = 2 )]
318336 [AllowEmptyCollection ()]
319- [PSObject []]$Headers )
337+ [PSObject []]$Headers ,
338+
339+ [parameter (Mandatory = $false , Position = 3 )]
340+ [bool ]$AddHeaders )
320341
321342 $Output = New-Object PSObject - Property @ {
322343 " CorrelationID" = $CorrID
323344 " Events" = $Events
324- " Headers" = $Headers
325345 }
346+ if ($AddHeaders )
347+ {
348+ $Output | Add-Member Headers $Headers
349+ }
350+
326351 Write-Output $Output
327352}
328353
354+ function Write-ADFSEventsSummary
355+ {
356+ # Create Table object
357+ $table = New-Object system.Data.DataTable " SummaryTable"
358+
359+ # Define Columns
360+ $col1 = New-Object system.Data.DataColumn Time, ([string ])
361+ $col2 = New-Object system.Data.DataColumn EventID, ([string ])
362+ $col3 = New-Object system.Data.DataColumn Details, ([string ])
363+ $col4 = New-Object system.Data.DataColumn CorrelationID, ([string ])
364+ $col5 = New-Object system.Data.DataColumn Machine, ([string ])
365+ $col6 = New-Object system.Data.DataColumn Log, ([string ])
366+ $table.columns.add ( $col1 )
367+ $table.columns.add ( $col2 )
368+ $table.columns.add ( $col3 )
369+ $table.columns.add ( $col4 )
370+ $table.columns.add ( $col5 )
371+ $table.columns.add ( $col6 )
372+
373+ foreach ($Event in $input.Events ){
374+ # Create a row
375+ $row = $table.NewRow ()
376+
377+ $row.Time = $Event.TimeCreated
378+ $row.EventID = $Event.Id
379+ $row.Details = $Event.Message
380+ $row.CorrelationID = $Event.CorrelationID
381+ $row.Machine = $Event.MachineName
382+ $row.Log = $Event.LogName
383+
384+ # Add the row to the table
385+ $table.Rows.Add ($row )
386+
387+ }
329388
389+ return $table
390+ }
330391
331392function Get-ADFSEvents
332393{
@@ -345,48 +406,56 @@ function Get-ADFSEvents
345406 time conversions will be based on the UTC of these values.
346407
347408 . EXAMPLE
348- Get-ADFSEvents -Logs Security, Admin, Debug -CorrelationID 669bced6-d6ae-4e69-889b-09ceb8db78c9 -Servers LocalHost, MyServer
409+ Get-ADFSEvents -Logs Security, Admin, Debug -CorrelationID 669bced6-d6ae-4e69-889b-09ceb8db78c9 -Server LocalHost, MyServer
410+ . Example
411+ Get-ADFSEvents -CorrelationID 669bced6-d6ae-4e69-889b-09ceb8db78c9 -Headers
349412 . EXAMPLE
350- Get-ADFSEvents -Logs Admin -AllWithHeaders -Servers LocalHost
413+ Get-ADFSEvents -Logs Admin -All
351414 . EXAMPLE
352- Get-ADFSEvents -Logs Debug, Security -AllWithoutHeaders -Servers LocalHost, Server1, Server2
415+ Get-ADFSEvents -Logs Debug, Security -All -Headers -Server LocalHost, Server1, Server2
353416 . Example
354- Get-ADFSEvents -Logs Debug -StartTime $start -EndTime $End -server localhost
417+ Get-ADFSEvents -Logs Debug -StartTime (Get-Date -Date ("2017-09-14T18:37:26.910168700Z")) -EndTime (Get-Date) -Headers
355418
356419 #>
357420
358421
359422 # Provide either correlation id, 'All' parameter, or time range along with logs to be queried and list of remote servers
360423 [CmdletBinding (DefaultParameterSetName = ' CorrelationIDParameterSet' )]
361424 param (
362- [parameter (Mandatory = $true , Position = 0 )]
425+ [parameter (Mandatory = $false , Position = 0 )]
363426 [ValidateSet (" Admin" , " Debug" , " Security" )]
364- [string []]$Logs ,
427+ [string []]$Logs = @ ( " Security " , " Admin " ) ,
365428
366429 [parameter (Mandatory = $true , Position = 1 , ParameterSetName = " CorrelationIDParameterSet" )]
367430 [ValidateNotNullOrEmpty ()]
368431 [string ]$CorrelationID ,
369432
370- [parameter (Mandatory = $true , Position = 1 , ParameterSetName = " AllEventsWithoutHeaderSet" )]
371- [switch ]$AllWithoutHeaders ,
372-
373- [parameter (Mandatory = $true , Position = 1 , ParameterSetName = " AllEventsWithHeaderSet" )]
374- [switch ]$AllWithHeaders ,
433+ [parameter (Mandatory = $true , Position = 1 , ParameterSetName = " AllEventsSet" )]
434+ [switch ]$All ,
375435
376436 [parameter (Mandatory = $true , Position = 1 , ParameterSetName = " AllEventsByTimeSet" )]
377437 [DateTime ]$StartTime ,
378438
379439 [parameter (Mandatory = $true , Position = 2 , ParameterSetName = " AllEventsByTimeSet" )]
380440 [DateTime ]$EndTime ,
381441
382- [parameter (Mandatory = $true , ValueFromPipeline = $True , ValueFromPipelineByPropertyName = $True )]
383- [string []]$Server
442+ [parameter (Mandatory = $false )]
443+ [switch ]$Headers ,
444+
445+ [parameter (Mandatory = $false , ValueFromPipeline = $True , ValueFromPipelineByPropertyName = $True )]
446+ [string []]$Server = " LocalHost"
384447 )
385448
386449 Begin
387450 {
388451 $ServerList = @ ()
389452 $HashTable = @ {}
453+ $Var = [ref ] [System.Guid ]::NewGuid()
454+ if ($CorrelationID -ne " " -and ! [System.Guid ]::TryParse($CorrelationID , $Var )){ # Validate provided Correlation ID is a valid GUID
455+ Write-Error " Invalid correlation id. Please provide valid input"
456+ Break
457+ }
458+
390459 if ($StartTime -ne $null -and $EndTime -ne $null )
391460 {
392461 $ByTime = $true
@@ -409,7 +478,7 @@ function Get-ADFSEvents
409478 {
410479 $Session = New-PSSession - ComputerName $Server
411480 $Events += QueryDesiredLogs - CorrID $CorrelationID - Session $Session - ByTime $ByTime - Start $StartTime.ToUniversalTime () - End $EndTime.ToUniversalTime ()
412- if ($CorrelationID -ne " " )
481+ if ($CorrelationID -ne " " -and $Headers )
413482 {
414483 $HTTPInformation += GetHTTPRequestInformation - CorrID $CorrelationID - Session $Session
415484 }
@@ -447,7 +516,7 @@ function Get-ADFSEvents
447516
448517 else # Events gathered for a single correlation id
449518 {
450- AggregateOutputObject - CorrID $CorrelationID - Events $Events - Headers $HTTPInformation
519+ AggregateOutputObject - CorrID $CorrelationID - Events $Events - Headers $HTTPInformation - AddHeader $Headers .IsPresent
451520 }
452521
453522 }
@@ -457,12 +526,8 @@ function Get-ADFSEvents
457526 # Print the result of gathering events for all correlation ids
458527 foreach ($EventList in $HashTable.Values )
459528 {
460- if ($AllWithoutHeaders )
461- {
462- Write-Output $EventList
463- }
464529
465- else { # Gather headers for each correlation id from each server
530+ if ( $Headers ) { # Gather headers for each correlation id from each server
466531 foreach ($Machine in $ServerList )
467532 {
468533 $HTTPInformation = @ ()
@@ -482,12 +547,13 @@ function Get-ADFSEvents
482547 Remove-PSSession $Session
483548 }
484549 }
485- }
486- AggregateOutputObject - CorrID $EventList [0 ].CorrelationID - Events $EventList - Headers $HTTPInformation
550+ }
487551 }
552+ AggregateOutputObject - CorrID $EventList [0 ].CorrelationID - Events $EventList - Headers $HTTPInformation - AddHeaders $Headers.IsPresent
488553 }
489554 }
490555
491556
492557}
493- Export-ModuleMember - Function Get-ADFSEvents
558+ Export-ModuleMember - Function Get-ADFSEvents
559+ Export-ModuleMember - Function Write-ADFSEventsSummarys
0 commit comments