Skip to content

Instantly share code, notes, and snippets.

@JPRuskin
Last active March 3, 2026 10:16
Show Gist options
  • Select an option

  • Save JPRuskin/4119e27b20f40d7c3c4db699e9f9bcb2 to your computer and use it in GitHub Desktop.

Select an option

Save JPRuskin/4119e27b20f40d7c3c4db699e9f9bcb2 to your computer and use it in GitHub Desktop.
An unpublished module to parse Chocolatey logs and display them usefully, to aid analysis.

Flake (a module for viewing Chocolatey logs)

Installation

Download the files from this gist and put them in a folder on your machine.

You should then be able to run Import-Module .\path\to\Flake.psd1.

Usage

Get-ChocolateyCall

image

This should produce a list of commands that you have run, along with their start time and exit code. E represents a command that threw an exception or otherwise is incomplete.

You can see all configuration at the time of a given command by using the GetConfiguration() method.

You can access the full log for a given call by expanding Output.

Get-ChocolateyLog

This somewhat colourised, trimmed down, output can also be displayed by using Get-ChocolateyLog. This will output everything in your log in a slightly formatted way. The main differences are that multi-line logs only show up as one line (so you don't see all of the settings unless you dig into them), and it only displays a formatted timestamp and a colourised message by default.

image

Debug is in purple, errors are in red, warnings are in yellow, and info (stdout) is in white. NuGet and Verbose lines are coloured in gray, because they are infrequently helpful but take up a lot of space. You can filter against the stream property as follows:

Get-ChocolateyLog | Where Stream -eq 'Error'
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<SelectionSets>
<SelectionSet>
<Name>ChocolateyLogTypes</Name>
<Types>
<TypeName>ChocolateyLog</TypeName>
<TypeName>ChocolateyCall</TypeName>
</Types>
</SelectionSet>
</SelectionSets>
<!-- ################ CONTROL DEFINITIONS ################ -->
<Controls>
<Control>
<Name>ChocolateyLog-GroupingFormat</Name>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<Frame>
<LeftIndent>4</LeftIndent>
<CustomItem>
<Text>ProcessID: </Text>
<ExpressionBinding>
<ScriptBlock>
$_.ProcessID
</ScriptBlock>
</ExpressionBinding>
<NewLine/>
<Text>Duration:</Text>
<ExpressionBinding>
<ScriptBlock>
"?"
</ScriptBlock>
</ExpressionBinding>
<NewLine/>
</CustomItem>
</Frame>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</Control>
</Controls>
<ViewDefinitions>
<View>
<Name>ChocolateyCalls</Name>
<ViewSelectedBy>
<TypeName>ChocolateyCall</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>StartTime</Label>
<Width>19</Width>
<!--Alignment>Left</Alignment-->
</TableColumnHeader>
<TableColumnHeader>
<Label>Exit</Label>
<Width>5</Width>
</TableColumnHeader>
<TableColumnHeader>
<Label>Command</Label>
<!--Width>11</Width-->
<!--Alignment>Left</Alignment-->
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>StartTime</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Result</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
$_.Command.Replace("""$($env:ChocolateyInstall)\choco.exe""",'choco')
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>ChocolateyLogs</Name>
<ViewSelectedBy>
<TypeName>ChocolateyLog</TypeName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>ProcessId</PropertyName>
<CustomControlName>ChocolateyLog-GroupingFormat</CustomControlName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Timestamp</Label>
<Width>19</Width>
<Alignment>Left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Message</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Timestamp</PropertyName>
<Alignment>Left</Alignment>
</TableColumnItem>
<TableColumnItem>
<!--PropertyName>Message</PropertyName-->
<ScriptBlock>
$M = $_
$Colour = switch ($_.Stream) {
#"INFO" {0} # Default
"DEBUG" {35} # Magenta
"WARN" {93} # Bright Yellow
"ERROR" {91} # Bright Red
#"NUGET" {90} # Bright Black (Gray)
#"VERBOSE" {90} # Bright Black (Gray)
default {
if ($M.Message -match '^(VERBOSE:|\[NuGet\])') {
90 # Bright Black (Gray)
} else {
0 # Default
}
}
}
$SetColour = "$([char]27)[$($Colour)m"
$Reset = "$([char]27)[0m"
"$SetColour$($_.Message)$Reset"
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
#
# Module manifest for module 'Flake'
#
# Generated by: @jpruskin
#
# Generated on: 02/08/2023
#
@{
# Script module or binary module file associated with this manifest.
RootModule = 'Flake.psm1'
# Version number of this module.
ModuleVersion = '0.0.1'
# Supported PSEditions
# CompatiblePSEditions = @()
# ID used to uniquely identify this module
GUID = '63b67481-b96d-45ce-aa6d-87c3054b6552'
# Author of this module
Author = '@jpruskin'
# Company or vendor of this module
CompanyName = 'Unknown'
# Copyright statement for this module
Copyright = '(c) @jpruskin. All rights reserved.'
# Description of the functionality provided by this module
Description = 'A module to display Chocolatey Logs'
# Minimum version of the PowerShell engine required by this module
# PowerShellVersion = ''
# Name of the PowerShell host required by this module
# PowerShellHostName = ''
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''
# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = @(
"ChocolateyLog.Format.ps1xml"
)
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = '*'
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = '*'
# Variables to export from this module
VariablesToExport = '*'
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = '*'
# DSC resources to export from this module
# DscResourcesToExport = @()
# List of all modules packaged with this module
# ModuleList = @()
# List of all files packaged with this module
# FileList = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()
# A URL to the license for this module.
# LicenseUri = ''
# A URL to the main website for this project.
# ProjectUri = ''
# A URL to an icon representing this module.
# IconUri = ''
# ReleaseNotes of this module
# ReleaseNotes = ''
# Prerelease string of this module
Prerelease = ''
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
# External dependent modules of this module
# ExternalModuleDependencies = @()
} # End of PSData hashtable
} # End of PrivateData hashtable
# HelpInfo URI of this module
# HelpInfoURI = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
}
function Get-ChocolateyCall {
[CmdletBinding(DefaultParameterSetName="All")]
param(
[Parameter(ValueFromPipeline)]
[string[]]$Path = (Join-Path $env:ChocolateyInstall "logs\chocolatey.log"),
[uint16]$Last
)
process {
Resolve-Path $Path | Get-ChocolateyLog | Group-Object ProcessID | ForEach-Object {
[ChocolateyCall]@{
StartTime = $_.Group[0].Timestamp
ProcessID = $_.Name
Command = $_.Group.Message.Where({$_ -like "Command line*"}, 1) -replace "^Command line: (""$([regex]::Escape($env:ChocolateyInstall)))?\\(choco)(\.exe"")?",'$2'
Output = $_.Group
Result = if ($_.Group.Message[-1] -match "^Exiting with \d+$") {$_.Group.Message[-1] -replace "^Exiting with "} else {"E"}
}
} | Sort-Object StartTime
}
}
class ChocolateyCall {
[datetime]$StartTime
hidden [uint32]$ProcessID
[string]$Command
$Result
[ChocolateyLog[]]$Output
hidden [hashtable] $configValues
[hashtable] GetConfiguration () {
if ($this.configValues) {return $this.configValues}
$Configuration = @{}
$StringConfiguration = $this.Output.Where{$_.Stream -eq 'Debug' -and $_.Message.StartsWith('Configuration: ')}.Message
$StringConfiguration.TrimStart("Configuration: ").Split("|").Where{-not [string]::IsNullOrWhiteSpace($_)}.Trim().ForEach{
if ($_ -match "(?<Key>.+)='(?<Value>.+)'") {
foreach ($Segment in $Matches.Key.Split('.')) {
}
$Configuration[$Matches.Key] = $Matches.Value
}
}
$this.configValues = $Configuration
return $this.configValues
}
}
class ChocolateyLog {
[datetime]$Timestamp
[uint32]$ProcessID
hidden [string]$Stream
[string]$Message
hidden [string]$Line
hidden [string]$File
hidden [uint16]$Index
static [datetime] DateTimeFromTimestamp ([string]$Timestamp) {
return [datetime]::ParseExact($Timestamp, 'yyyy-MM-dd HH:mm:ss,fff', [CultureInfo]::CurrentCulture)
}
ChocolateyLog ([hashtable]$Match) {
$this.Timestamp = [ChocolateyLog]::DateTimeFromTimestamp($Match.Timestamp)
$this.ProcessID = $Match.ProcessID
$this.Stream = $Match.Stream
$this.Message = $Match.Message.Trim()
$this.Line = $Match[0]
}
ChocolateyLog ([System.Text.RegularExpressions.Match]$Match) {
# Can't rely on these, but equally don't love hardcoded indexes from the regex. Hm.
# $this.Timestamp = [datetime]::ParseExact($Match.Timestamp, 'yyyy-MM-dd HH:mm:ss,fff', [CultureInfo]::CurrentCulture)
# $this.ProcessID = $Match.ProcessID
# $this.Stream = $Match.Stream
# $this.Message = $Match.Message
# $this.Line = $Match.Value
$this.Timestamp = [ChocolateyLog]::DateTimeFromTimestamp($Match.Groups.Value[1])
$this.ProcessID = $Match.Groups.Value[2]
$this.Stream = $Match.Groups.Value[3]
$this.Message = $Match.Groups.Value[4].Trim()
$this.Line = $Match.Groups.Value[0]
}
# ChocolateyLog ([Microsoft.PowerShell.Commands.MatchInfo]$Match) {}
ChocolateyLog ([string]$Line) {
$this.Line = $Line
if ($this.Line -match $script:Pattern) {
$this.Timestamp = [ChocolateyLog]::DateTimeFromTimestamp($Matches.Timestamp)
$this.ProcessID = $Matches.ProcessID
$this.Stream = $Matches.Stream
$this.Message = $Matches.Message.Trim()
$this.Line = $Matches[0]
}
}
}
$script:Pattern = -join @(
"^"
"(?<Timestamp>\d{4}-\d{2}-\d{2}\W+\d{2}:\d{2}:\d{2},\d{3})\W+"
"(?<ProcessID>\d+)\W+"
"\[(?<Stream>INFO|DEBUG|WARN|ERROR) ?\]"
" - "
"(?<Message>.*)"
'$'
)
function Get-ChocolateyLog {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
[string[]]$Path = (Join-Path $env:ChocolateyInstall "logs\chocolatey.log")
)
begin {
$MultilinePattern = -join @(
"(?m)" # Multiline
"^" # We want to match from the start of a log line
"(?<Timestamp>\d{4}-\d{2}-\d{2}\W+\d{2}:\d{2}:\d{2},\d{3})\W+" # There should be a timestamp, in a known format
"(?<ProcessID>\d+)\W+" # Then a string of digits (processId)
"\[(?<Stream>INFO|DEBUG|WARN|ERROR) ?\]" # Then whichever stream the command was on
" - " # a separator
"(?<Message>(?:.|\n)*?)\n" # and then a message that can span multiple lines
"(?=^\d{4}-\d{2}-\d{2}\W+\d{2}:\d{2}:\d{2},\d{3}\W+\d+\W+\[|\Z)" # Finally, we should see the next log, or the end of the file
)
}
process {
# We use Get-Content -Raw instead of Select-String -Path to be able to match multiline
[ChocolateyLog[]](Get-Content -Path $Path -Raw | Select-String -Pattern $MultilinePattern -AllMatches).Matches
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment