Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active November 27, 2024 18:56
Show Gist options
  • Save JustinGrote/863e9e24c5025c3f72e42bb117d9c40e to your computer and use it in GitHub Desktop.
Save JustinGrote/863e9e24c5025c3f72e42bb117d9c40e to your computer and use it in GitHub Desktop.
PowerShell Debugging Examples

Debugging Examples

Maslow's Hierarchy of PowerShell Debugging

Hierarchy

Describe "Debugging Examples" {
It 'Labs get Treats' {
$recommendedTreat = & $PSScriptRoot/Get-DogTreat.ps1 -DogBreed 'Labrador' -SuperBugged
$recommendedTreat.Calories | Should -BeGreaterThan 0
}
}
# Example 1: Using Write-Host for basic debugging
Write-Host 'Debug: Starting script execution' -ForegroundColor Yellow
$someVariable = 'Test Value'
Write-Host "Debug: someVariable = $someVariable" -ForegroundColor Cyan
# Example 2: Using [Console]::WriteLine (useful in non-console contexts)
[Console]::WriteLine('Debug: Direct console output')
[Console]::WriteLine('Debug: Current time: {0}', (Get-Date))
# Example 3: Using Wait-Debugger (PowerShell 5.0+)
$i = 1
while ($i -le 3) {
Write-Output "Current iteration: $i"
Wait-Debugger # Script will break here
$i++
}
# Example 4: Using Set-PSBreakpoint
# Break on line
$breakOnLine = Set-PSBreakpoint -Line 20 -Script $PSCommandPath
# Break when variable changes
$breakOnVariable = Set-PSBreakpoint -Variable someVariable -Mode Write
# Break when a command is called
$breakOnCommand = Set-PSBreakpoint -Command Write-Output
# Break when expression evaluates to true
$breakOnExpression = Set-PSBreakpoint -Script $PSCommandPath -Action {
if ($someVariable -eq 'New Value') { break }
}
# Break on specific script block
$breakOnScriptBlock = Set-PSBreakpoint -Script $PSCommandPath -Action {
Write-Host 'Breakpoint hit!' -ForegroundColor Red
break
} -Line 1 -Column 1
# Break when accessing a property
$breakOnProperty = Set-PSBreakpoint -Script $PSCommandPath -Variable someVariable -Mode Read, Write
# Example function to demonstrate debugging
function Test-Debugging {
Write-Output 'Starting function'
$someVariable = 'New Value'
Write-Output 'Ending function'
}
# Run the function
Test-Debugging
# Cleanup breakpoints
Get-PSBreakpoint | Remove-PSBreakpoint
param(
[Parameter(Mandatory)][string]$DogBreed,
[switch]$Bugged,
[switch]$SuperBugged
)
$DogTreats = @{
'labrador' = @{
Name = 'Large Milk Bone'
Calories = 100
MeatIngredients = @('๐Ÿ—', '๐Ÿฅฉ')
}
'chihuahua' = @{
Name = 'Small Dental Chew'
Calories = 50
MeatIngredients = @('๐Ÿ‘')
}
'german shepherd' = @{
Name = 'Large Rawhide Bone'
Calories = 120
MeatIngredients = @('๐Ÿ—')
}
'poodle' = @{
Name = 'Medium Dental Stick'
Calories = 80
MeatIngredients = @('๐Ÿ‘', '๐Ÿ—')
}
}
$DogTreats | ForEach-Object {
# Add some ๐Ÿงˆ
$PSItem.Calories += 200
}
function Get-๐Ÿฌfor๐Ÿถ ($DogBreed) {
$breed = $DogBreed.ToLower()
if ($DogTreats.ContainsKey($breed)) {
return $DogTreats[$breed]
} else {
return @{
Name = 'Standard Dog Biscuit'
Calories = 70
MeatIngredients = @('๐Ÿ—')
}
}
}
$recommendedTreat = Get-๐Ÿฌfor๐Ÿถ -DogBreed $DogBreed
if ($bugged) {
$recommendedTreat = @{
Name = 'Nasty Gross Things ๐Ÿคฎ'
Calories = 0
MeatIngredients = @('Not ๐Ÿฅฉ', 'Lots of ๐Ÿฅฆ')
}
}
if ($SuperBugged) {
$recommendedTreat.Calories = -1
}
if ($recommendedTreat.Calories -le 0) {
throw "๐Ÿ˜ˆ๐Ÿ˜ˆ๐Ÿ˜ˆDogs Don't Deserve Treats๐Ÿ˜ˆ๐Ÿ˜ˆ๐Ÿ˜ˆ"
}
return $recommendedTreat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment