Challenge - 5 Problems
PowerShell Error Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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: $_" }
Attempts:
2 left
💡 Hint
Look at how the try-catch block handles errors with Write-Output.
✗ Incorrect
The script sets error action to stop, so Get-Item throws a terminating error. The catch block captures it and outputs the error message with Write-Output, showing the error text.
✅ Best Practice
intermediate2: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?
Attempts:
2 left
💡 Hint
Consider how to add timestamps and append to a file safely.
✗ Incorrect
Using try-catch with Add-Content and Get-Date adds timestamps and appends errors to a log file, which is a good logging practice.
❓ Troubleshoot
advanced2:00remaining
Troubleshooting Missing Error Logs
A PowerShell script uses this code to log errors:
Why might the error.log file be empty after a failure?
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'
}Attempts:
2 left
💡 Hint
Consider whether the error from Remove-Item is terminating or non-terminating.
✗ Incorrect
By default, Remove-Item produces a non-terminating error when the specified file does not exist. Non-terminating errors do not trigger the catch block. Therefore, Out-File is never called, and error.log remains empty.
🔀 Workflow
advanced2:00remaining
PowerShell Error Logging Workflow
Which sequence correctly describes a workflow to log errors with timestamps and continue script execution?
Attempts:
2 left
💡 Hint
Think about setting error preference before running commands.
✗ Incorrect
First set error preference to 'Stop' so errors become terminating and are caught. Then run commands in try. Catch logs errors. Then script continues.
🧠 Conceptual
expert2:00remaining
Understanding PowerShell Error Variable Behavior
In PowerShell, what is the difference between using $_ and $Error[0] inside a catch block for error logging?
Attempts:
2 left
💡 Hint
Consider the scope and timing of these variables.
✗ Incorrect
Inside a catch block, $_ contains the current error object, while $Error[0] is the most recent error in the global error array, which may differ if other errors occurred.