Bird
0
0

Identify the error in this PowerShell error logging snippet:

medium📝 Troubleshoot Q14 of 15
PowerShell - Error Handling
Identify the error in this PowerShell error logging snippet:
try {
  Get-Content 'C:\missing.txt'
} catch {
  Write-Error "$(Get-Date): File missing"
  Add-Content error.log "File missing"
}
ACatch block should not write to error stream
BWrite-Error should be replaced with Write-Host to log properly
CTry block syntax is incorrect
DAdd-Content should include timestamp for better logs
Step-by-Step Solution
Solution:
  1. Step 1: Review logging commands

    Write-Error writes to error stream, which is okay for alerting, but Add-Content logs to file without timestamp.
  2. Step 2: Importance of timestamps in logs

    Logs without timestamps make tracking errors over time difficult; adding $(Get-Date) improves log usefulness.
  3. Final Answer:

    Add-Content should include timestamp for better logs -> Option D
  4. Quick Check:

    Logs need timestamps for clarity [OK]
Quick Trick: Always add timestamps when logging errors to files [OK]
Common Mistakes:
  • Thinking Write-Error cannot be used in catch
  • Ignoring importance of timestamps in logs
  • Assuming try block syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes