0
0
PowerShellscripting~20 mins

Error logging patterns in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Error Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
PowerShell Error Logging Output
What is the output of this PowerShell script when the command fails?
$ErrorActionPreference = 'Stop'
try {
  Get-Item 'C:\nonexistentfile.txt'
} catch {
  Write-Output "Error caught: $_"
}
PowerShell
$ErrorActionPreference = 'Stop'
try {
  Get-Item 'C:\nonexistentfile.txt'
} catch {
  Write-Output "Error caught: $_"
}
AError caught: Cannot find path 'C:\nonexistentfile.txt' because it does not exist.
BGet-Item : Cannot find path 'C:\nonexistentfile.txt' because it does not exist.
CNo output, script runs silently.
DError caught: The term 'Get-Item' is not recognized as the name of a cmdlet.
Attempts:
2 left
💡 Hint
Look at how the try-catch block handles errors with Write-Output.
Best Practice
intermediate
2:00remaining
Best Practice for Logging Errors in PowerShell
Which option is the best practice for logging errors in a PowerShell script to a file with timestamps?
AUse Write-Error with Out-File to append errors: Write-Error $_ | Out-File errors.log -Append
BUse Write-Output inside catch block without timestamps: Write-Output $_
CUse Write-Host to display errors and redirect output to a file: Write-Host $_ > errors.log
DUse try-catch and inside catch use Add-Content with timestamp: Add-Content errors.log "$(Get-Date): $_"
Attempts:
2 left
💡 Hint
Consider how to add timestamps and append to a file safely.
Troubleshoot
advanced
2:00remaining
Troubleshooting Missing Error Logs
A PowerShell script uses this code to log errors:
try {
  Remove-Item 'C:\temp\file.txt'
} catch {
  $_ | Out-File 'error.log'
}

Why might the error.log file be empty after a failure?
PowerShell
try {
  Remove-Item 'C:\temp\file.txt'
} catch {
  $_ | Out-File 'error.log'
}
AThe script lacks permissions to write to error.log, so file stays empty.
BRemove-Item does not throw an error if the file is missing, so catch never runs.
COut-File overwrites the file but $_ is an error object, not a string, so nothing is written.
DThe error variable $_ is empty because errors are non-terminating by default.
Attempts:
2 left
💡 Hint
Consider whether the error from Remove-Item is terminating or non-terminating.
🔀 Workflow
advanced
2:00remaining
PowerShell Error Logging Workflow
Which sequence correctly describes a workflow to log errors with timestamps and continue script execution?
A1,2,3,4
B2,1,3,4
C1,3,2,4
D2,3,1,4
Attempts:
2 left
💡 Hint
Think about setting error preference before running commands.
🧠 Conceptual
expert
2:00remaining
Understanding PowerShell Error Variable Behavior
In PowerShell, what is the difference between using $_ and $Error[0] inside a catch block for error logging?
A$_ is only available outside catch blocks; $Error[0] is only available inside catch blocks.
B$_ and $Error[0] always contain the exact same error object inside catch blocks.
C$_ contains the current error object in catch; $Error[0] is the most recent error in the global error array, which may differ.
D$_ is a string of the error message; $Error[0] is the full error object with details.
Attempts:
2 left
💡 Hint
Consider the scope and timing of these variables.