Repository navigation
refactor(helpers): move the log, diagnostic and Graph helpers out #101
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
base: main
Are you sure you want to change the base?
Changes from all commits
d4036b5
f2a85c5
3bebe0e
b0a2f33
6c636f0
65ba5c3
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,77 @@ | ||
| function Compress-IslFolder { | ||
| <# | ||
| .SYNOPSIS | ||
| Writes the files of a folder into a new zip archive, with forward slashes in the entry names. | ||
|
|
||
| .DESCRIPTION | ||
| Windows PowerShell 5.1 runs on .NET Framework, whose ZipFile.CreateFromDirectory writes | ||
| entry names with backslashes, and such an archive does not open as folders on another | ||
| platform. This adds one entry for each file below the folder, named by its path relative to | ||
| the folder with forward slashes, on every host. Empty folders are not kept. The archive must | ||
| not exist yet. It does not ask ShouldProcess: the command that calls it has asked for the | ||
| whole package, and it writes even when -WhatIf or -Confirm reached it through the | ||
| preferences. A failure ends the function with its error. An archive that this call created | ||
| and could not fill is removed, so a failure leaves no archive that looks complete; one that | ||
| was there before the call is left as it is. | ||
|
|
||
| .PARAMETER Folder | ||
| The full path of the folder to compress, without a trailing separator. | ||
|
|
||
| .PARAMETER Destination | ||
| The full path of the zip archive to create. | ||
|
|
||
| .EXAMPLE | ||
| $entryCount = Compress-IslFolder -Folder $staging -Destination $zip | ||
|
|
||
| Writes the staging folder of a diagnostic package into its zip and returns the number of | ||
| entries. | ||
|
|
||
| .INPUTS | ||
| None. | ||
|
|
||
| .OUTPUTS | ||
| System.Int32. The number of entries written. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([int])] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [string]$Folder, | ||
|
|
||
| [Parameter(Mandatory)] | ||
| [string]$Destination | ||
| ) | ||
|
|
||
| $archive = $null | ||
| $count = 0 | ||
| try { | ||
| Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop | ||
| $files = @(Get-ChildItem -LiteralPath $Folder -Recurse -File -ErrorAction Stop) | ||
| $archive = [System.IO.Compression.ZipFile]::Open($Destination, 'Create') | ||
| foreach ($file in $files) { | ||
| $relative = $file.FullName.Substring($Folder.Length).TrimStart('\', '/') -replace '\\', '/' | ||
| $null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile( | ||
| $archive, $file.FullName, $relative) | ||
| $count++ | ||
| } | ||
| } catch { | ||
| $failure = $_ | ||
| if ($null -ne $archive) { | ||
| # Disposing writes the central directory, which makes the part archive a valid zip | ||
| $partArchive = $archive | ||
| $archive = $null | ||
| try { | ||
| $partArchive.Dispose() | ||
| [System.IO.File]::Delete($Destination) | ||
| } catch { | ||
| # The compression's own failure is the one to report; the archive that stays is named | ||
| $reason = $_.Exception.Message | ||
| Write-Warning "The incomplete archive at $Destination could not be removed: $reason" | ||
| } | ||
| } | ||
| $PSCmdlet.ThrowTerminatingError($failure) | ||
| } finally { | ||
| if ($null -ne $archive) { $archive.Dispose() } | ||
| } | ||
| $count | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| function ConvertTo-IslNamedGroupPattern { | ||
| <# | ||
| .SYNOPSIS | ||
| Renames the capturing groups of a regular expression so that several fit into one pattern. | ||
|
|
||
| .DESCRIPTION | ||
| The agent-log reader joins every event pattern into one alternation and must still tell | ||
| which pattern matched and read its own captures. Each capturing group becomes a named group, | ||
| the prefix followed by its number from 1. A group that does not capture, such as (?:...) or | ||
| (?<name>...), is left as it is, and so is a parenthesis that is escaped or inside a character | ||
| class. | ||
|
|
||
| In the joined pattern every group has a name, and .NET numbers groups across the whole | ||
| expression, so a numbered backreference such as \1 would refer to a group of another | ||
| pattern. A backreference \n or \k<n> outside a character class, whose n is one of the | ||
| pattern's own groups, is therefore written as \k<prefix n>. A larger number is left as it | ||
| is, as .NET reads it on its own, and so is a conditional that names a group by number, such | ||
| as (?(1)...), which no event pattern uses. | ||
|
|
||
| .PARAMETER Pattern | ||
| The regular expression. | ||
|
|
||
| .PARAMETER Prefix | ||
| The start of the new group names, such as e3g. | ||
|
|
||
| .EXAMPLE | ||
| ConvertTo-IslNamedGroupPattern -Pattern '^exit code is (-?\d+)' -Prefix 'e3g' | ||
|
|
||
| Returns @{ Pattern = '^exit code is (?<e3g1>-?\d+)'; Count = 1 }. | ||
|
|
||
| .INPUTS | ||
| None. | ||
|
|
||
| .OUTPUTS | ||
| System.Collections.Hashtable. Pattern, the renamed expression, and Count, the number of | ||
| groups that were renamed. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([hashtable])] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [string]$Pattern, | ||
|
|
||
| [Parameter(Mandatory)] | ||
| [string]$Prefix | ||
| ) | ||
|
|
||
| # A backreference can come before the last group, so the text is kept in pieces and a | ||
| # backreference is resolved once the number of groups is known | ||
| $pieces = [System.Collections.Generic.List[object]]::new() | ||
| $text = [System.Text.StringBuilder]::new() | ||
| $backreference = [regex]::new('\G\\(?:([1-9][0-9]*)|k<([1-9][0-9]*)>)') | ||
| $count = 0 | ||
| $inClass = $false | ||
| for ($index = 0; $index -lt $Pattern.Length; $index++) { | ||
| $character = $Pattern[$index] | ||
| if ($character -eq '\' -and $index + 1 -lt $Pattern.Length) { | ||
| $reference = $null | ||
| if (-not $inClass) { $reference = $backreference.Match($Pattern, $index) } | ||
| if ($null -ne $reference -and $reference.Success) { | ||
| $pieces.Add($text.ToString()) | ||
| $null = $text.Clear() | ||
| $digits = $reference.Groups[2].Value | ||
| if ($reference.Groups[1].Success) { $digits = $reference.Groups[1].Value } | ||
| # More digits than nine is more groups than any pattern has | ||
| $number = [int]::MaxValue | ||
| if ($digits.Length -le 9) { $number = [int]$digits } | ||
| $pieces.Add(@{ Number = $number; Text = $reference.Value }) | ||
| $index += $reference.Length - 1 | ||
| continue | ||
| } | ||
| $null = $text.Append($character).Append($Pattern[$index + 1]) | ||
| $index++ | ||
| continue | ||
| } | ||
| if ($inClass) { | ||
| if ($character -eq ']') { $inClass = $false } | ||
| $null = $text.Append($character) | ||
| continue | ||
| } | ||
| if ($character -eq '[') { $inClass = $true } | ||
| $opensCapture = $character -eq '(' -and ($index + 1 -ge $Pattern.Length -or $Pattern[$index + 1] -ne '?') | ||
|
Collaborator
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. Pre-existing (not from this change, not blocking): after renaming, a numbered backreference in a pattern ( 🤖 Generated with Claude Code
Collaborator
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. Fixed in 65ba5c3. A 🤖 Generated with Claude Code |
||
| if ($opensCapture) { | ||
| $count++ | ||
| $null = $text.Append("(?<$Prefix$count>") | ||
| continue | ||
| } | ||
| $null = $text.Append($character) | ||
| } | ||
| $pieces.Add($text.ToString()) | ||
|
|
||
| $named = [System.Text.StringBuilder]::new() | ||
| foreach ($piece in $pieces) { | ||
| if ($piece -is [string]) { | ||
| $null = $named.Append($piece) | ||
| } elseif ($piece['Number'] -le $count) { | ||
| $null = $named.Append("\k<$Prefix$($piece['Number'])>") | ||
| } else { | ||
| $null = $named.Append($piece['Text']) | ||
| } | ||
| } | ||
| @{ Pattern = $named.ToString(); Count = $count } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| function Copy-IslSharedFile { | ||
| <# | ||
| .SYNOPSIS | ||
| Copies a file that another process holds open for writing. | ||
|
|
||
| .DESCRIPTION | ||
| The agent keeps its current log open while it writes to it. A stream that allows only | ||
| other readers cannot open such a file, so the source is opened for reading with write and | ||
| delete sharing allowed, and the copy is the file as it stands at that moment. Delete sharing | ||
| lets a writer that holds the file with delete access open it, and lets the file be renamed | ||
| or removed while it is copied, as a log is when it rotates. A file that is at the | ||
| destination is replaced. It does not ask ShouldProcess: the command that calls it has asked | ||
| for the whole package, and it copies even when -WhatIf or -Confirm reached it through the | ||
| preferences. A failure ends the function with its error; a copy that failed after the | ||
| destination was created is removed, so no part of a file is left there. | ||
|
|
||
| .PARAMETER Source | ||
| The full path of the file to copy. | ||
|
|
||
| .PARAMETER Destination | ||
| The full path of the copy. Its folder must exist. | ||
|
|
||
| .EXAMPLE | ||
| Copy-IslSharedFile -Source $log.FullName -Destination $copy | ||
|
|
||
| Copies a log of the agent that the agent is still writing. | ||
|
|
||
| .INPUTS | ||
| None. | ||
|
|
||
| .OUTPUTS | ||
| None. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([void])] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [string]$Source, | ||
|
|
||
| [Parameter(Mandatory)] | ||
| [string]$Destination | ||
| ) | ||
|
|
||
| $in = $null | ||
| $out = $null | ||
| try { | ||
| $in = [System.IO.File]::Open($Source, 'Open', 'Read', 'ReadWrite, Delete') | ||
| $out = [System.IO.File]::Create($Destination) | ||
| $in.CopyTo($out) | ||
| } catch { | ||
| $failure = $_ | ||
| if ($null -ne $out) { | ||
| $partCopy = $out | ||
| $out = $null | ||
| try { | ||
| $partCopy.Dispose() | ||
| [System.IO.File]::Delete($Destination) | ||
| } catch { | ||
| # The copy's own failure is the one to report; the part copy that stays is named | ||
| Write-Warning "The part copy at $Destination could not be removed: $($_.Exception.Message)" | ||
| } | ||
| } | ||
| $PSCmdlet.ThrowTerminatingError($failure) | ||
| } finally { | ||
| if ($null -ne $out) { $out.Dispose() } | ||
| if ($null -ne $in) { $in.Dispose() } | ||
| } | ||
| } |
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.
Pre-existing (not from this change, not blocking): when
CreateEntryFromFilefails part-way (a file locked or removed in the staging folder, a full disk), thefinallydisposes the archive and leaves a valid but incomplete zip atDestination.Export-IntuneAgentDiagnostichas already removed the previous package under-Forceand then ends with an error, so the path holds a zip that opens and looks like a package. Fix: in thecatch, removeDestinationwhen this function created it (dispose first), and say in the help that a failure leaves no archive.🤖 Generated with Claude Code
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.
Fixed in 65ba5c3. When filling the archive fails, the
catchdisposes the archive and deletes the file this call created. A failure atZipFile.Open, such as an archive that is there already, leaves that file alone, as the existing test checks. If removing the part archive fails as well, a warning names it, and the compression's own error is the one the function ends with. The help says so. The new test holds one staged file open for writing, so it cannot be added, and checks that no zip is left.🤖 Generated with Claude Code