Last active
November 27, 2024 18:56
-
-
Save JustinGrote/863e9e24c5025c3f72e42bb117d9c40e to your computer and use it in GitHub Desktop.
PowerShell Debugging Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Describe "Debugging Examples" { | |
It 'Labs get Treats' { | |
$recommendedTreat = & $PSScriptRoot/Get-DogTreat.ps1 -DogBreed 'Labrador' -SuperBugged | |
$recommendedTreat.Calories | Should -BeGreaterThan 0 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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