Repository navigation
fix(logs)!: end the agent-log commands with terminating errors #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| function New-IslErrorRecord { | ||
| <# | ||
| .SYNOPSIS | ||
| Builds the error record a function ends with. | ||
|
|
||
| .DESCRIPTION | ||
| A failure the module detects itself is raised with $PSCmdlet.ThrowTerminatingError() and an | ||
| error record that has an ID and a category of its own, so that a caller can tell one failure | ||
| from another without matching the message. This function builds that record: the exception is | ||
| of the type named, carries the message, and wraps the error that caused it when there is one. | ||
|
|
||
| The record is returned, not raised. The function that detected the failure raises it, so that | ||
| the error names that function. | ||
|
|
||
| The function does not fail for any message, ID or type. It is called in the argument of | ||
| ThrowTerminatingError, and an error raised here would end only that statement: the calling | ||
| function would go on as if it had detected nothing. An exception type that cannot be built | ||
| from a message and an inner exception is therefore replaced by InvalidOperationException. | ||
|
|
||
| .PARAMETER Message | ||
| What failed and, where the caller can act on it, what to do. | ||
|
|
||
| .PARAMETER ErrorId | ||
| The ID of this failure, in PascalCase and without spaces. It is the first part of the | ||
| FullyQualifiedErrorId; PowerShell adds the name of the function that raises the record. | ||
|
|
||
| .PARAMETER Category | ||
| The category of the error. | ||
|
|
||
| .PARAMETER TargetObject | ||
| What the function was working on when it failed, such as a path or a name. | ||
|
|
||
| .PARAMETER ExceptionType | ||
| The type of the exception, one that has a constructor taking a message and an inner | ||
| exception. The default is System.InvalidOperationException. | ||
|
|
||
| .PARAMETER InnerException | ||
| The exception that caused the failure, kept as the inner exception. | ||
|
|
||
| .EXAMPLE | ||
| $record = New-IslErrorRecord -Message "$Path exists; use -Force" -ErrorId 'PathExists' -Category ResourceExists | ||
| $PSCmdlet.ThrowTerminatingError($record) | ||
|
|
||
| Ends the calling function with an error whose ID starts with PathExists. | ||
|
|
||
| .EXAMPLE | ||
| $recordSplat = @{ Message = 'No such event'; ErrorId = 'UnknownEvent'; Category = 'InvalidArgument' } | ||
| # The value of the bad argument is the target, and the exception says that it was an argument | ||
| New-IslErrorRecord @recordSplat -TargetObject $name -ExceptionType ([System.ArgumentException]) | ||
|
|
||
| A record for a bad argument. | ||
|
|
||
| .INPUTS | ||
| None. | ||
|
|
||
| .OUTPUTS | ||
| System.Management.Automation.ErrorRecord. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([System.Management.Automation.ErrorRecord])] | ||
| [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', | ||
| Justification = 'Builds an error record and changes nothing.')] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [string]$Message, | ||
|
|
||
| [Parameter(Mandatory)] | ||
| [string]$ErrorId, | ||
|
|
||
| [Parameter(Mandatory)] | ||
| [System.Management.Automation.ErrorCategory]$Category, | ||
|
|
||
| [Parameter()] | ||
| [AllowNull()] | ||
| [object]$TargetObject, | ||
|
|
||
| [Parameter()] | ||
| [type]$ExceptionType = [System.InvalidOperationException], | ||
|
|
||
| [Parameter()] | ||
| [System.Exception]$InnerException | ||
| ) | ||
|
|
||
| $exception = $null | ||
| try { | ||
| $exception = $ExceptionType::new($Message, $InnerException) -as [System.Exception] | ||
| } catch { | ||
| # The type has no such constructor. The record is still built: see the description | ||
| Write-Debug "No $($ExceptionType.FullName) could be built for error $ErrorId." | ||
| } | ||
| if ($null -eq $exception) { | ||
| $exception = [System.InvalidOperationException]::new($Message, $InnerException) | ||
| } | ||
|
|
||
| [System.Management.Automation.ErrorRecord]::new($exception, $ErrorId, $Category, $TargetObject) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,14 @@ function Export-IntuneAgentDiagnostic { | |
| if ([System.IO.Path]::GetExtension($Path) -ne '.zip') { $Path = "$Path.zip" } | ||
| $zip = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) | ||
| if ((Test-Path -LiteralPath $zip) -and -not $Force) { | ||
| throw "$zip exists; use -Force to overwrite it" | ||
| $recordSplat = @{ | ||
| Message = "$zip exists; use -Force to overwrite it" | ||
| ErrorId = 'DiagnosticPackageExists' | ||
| Category = [System.Management.Automation.ErrorCategory]::ResourceExists | ||
| TargetObject = $zip | ||
| ExceptionType = [System.IO.IOException] | ||
| } | ||
| $PSCmdlet.ThrowTerminatingError((New-IslErrorRecord @recordSplat)) | ||
| } | ||
| if (-not $LogPath) { | ||
| $programData = if ($env:ProgramData) { $env:ProgramData } else { 'C:\ProgramData' } | ||
|
|
@@ -186,6 +193,10 @@ function Export-IntuneAgentDiagnostic { | |
| } | ||
| $entryCount = Compress-Staging -Folder $staging -Destination $zip | ||
| } | ||
| catch { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| # Whatever failed, the package is not complete: the command ends without a result | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| finally { | ||
| Remove-Item -LiteralPath $staging -Recurse -Force -ErrorAction SilentlyContinue | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,14 @@ function Get-IntuneAgentLog { | |
| $knownEvents = @($script:IslAgentLogEvents | ForEach-Object { $_.Event }) | ||
| foreach ($name in $EventName) { | ||
| if ($name -notin $knownEvents) { | ||
| throw "Unknown event '$name'. Get-IntuneAgentLog -ListEvent shows the names" | ||
| $recordSplat = @{ | ||
| Message = "Unknown event '$name'. Get-IntuneAgentLog -ListEvent shows the names" | ||
| ErrorId = 'UnknownAgentLogEvent' | ||
| Category = [System.Management.Automation.ErrorCategory]::InvalidArgument | ||
| TargetObject = $name | ||
| ExceptionType = [System.ArgumentException] | ||
| } | ||
| $PSCmdlet.ThrowTerminatingError((New-IslErrorRecord @recordSplat)) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -72,18 +79,31 @@ function Get-IntuneAgentLog { | |
| AgentExecutor = 'AgentExecutor*.log' | ||
| All = '*.log' | ||
| } | ||
| # Each path is one item of the input. One that cannot be resolved is reported, and the others | ||
| # are still read | ||
| $unresolved = 0 | ||
| $files = foreach ($item in $Path) { | ||
| $resolved = (Resolve-Path -LiteralPath $item -ErrorAction Stop).ProviderPath | ||
| try { | ||
| $resolved = (Resolve-Path -LiteralPath $item -ErrorAction Stop).ProviderPath | ||
| } catch { | ||
| $unresolved++ | ||
| $PSCmdlet.WriteError($_) | ||
| continue | ||
| } | ||
| if (Test-Path -LiteralPath $resolved -PathType Container) { | ||
| foreach ($name in $Log) { | ||
| Get-ChildItem -LiteralPath $resolved -Filter $filters[$name] -File | ForEach-Object { $_.FullName } | ||
| Get-ChildItem -LiteralPath $resolved -Filter $filters[$name] -File | | ||
| ForEach-Object { $_.FullName } | ||
| } | ||
| } | ||
| else { $resolved } | ||
| } | ||
| $files = @($files | Sort-Object -Unique) | ||
| if (-not $files) { | ||
| Write-Warning "No log files under $($Path -join ', ')" | ||
| # When no path could be resolved, the errors have said so already | ||
| if ($unresolved -lt @($Path).Count) { | ||
| Write-Warning "No log files under $($Path -join ', ')" | ||
| } | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -110,7 +130,14 @@ function Get-IntuneAgentLog { | |
| continue | ||
| } | ||
| Write-Verbose "Reading $file" | ||
| $kept = @(ConvertFrom-IslCmTraceLog -Path $file @parseSplat) | ||
| try { | ||
| $kept = @(ConvertFrom-IslCmTraceLog -Path $file @parseSplat) | ||
| } catch { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A log that cannot be parsed still ends the whole command, as the |
||
| # A log that cannot be read or parsed is one item of the input too: it is reported | ||
| # and gives no entries, and the other logs are still read | ||
| $PSCmdlet.WriteError($_) | ||
| continue | ||
| } | ||
| if (-not $kept.Count) { continue } | ||
| $classified = @(Get-IslAgentLogEvent -Message @($kept | ForEach-Object { $_.Message })) | ||
| for ($index = 0; $index -lt $kept.Count; $index++) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,13 @@ function Get-IntuneAgentTimeline { | |
| if ($Id) { $logSplat.Id = $Id } | ||
| if ($PSBoundParameters.ContainsKey('After')) { $logSplat.After = $After } | ||
| if ($PSBoundParameters.ContainsKey('Before')) { $logSplat.Before = $Before } | ||
| $entries = @(Get-IntuneAgentLog @logSplat) | ||
| # A failure of the read ends this command too: without the catch it would end only this | ||
| # statement, and the timelines would be built from nothing | ||
| try { | ||
| $entries = @(Get-IntuneAgentLog @logSplat) | ||
| } catch { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This guard changes nothing a test of behaviour can see. Without it the command goes on with no entries and returns no timelines, which is also what it returns when it stops here; the 11 tests of this command passed with the guard removed. It is here because the rule has to hold everywhere to hold where it matters, and |
||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
|
|
||
| # App names travel in the policy list the agent logs; nothing else names a policy | ||
| $names = @{} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An earlier form of this function validated the ID with a pattern and ended with an error for a type it could not build. Both made it possible for the function to fail in the argument of
ThrowTerminatingError, which ends that statement only: the caller would have gone on past the failure it had just detected. So it accepts anything and falls back toInvalidOperationException. The typo that validation would have caught is caught by the tests that assert each ID.