Bird
0
0

How can you modify this script to log errors only if the error message contains the word 'Access'?

hard📝 Workflow Q9 of 15
PowerShell - Error Handling
How can you modify this script to log errors only if the error message contains the word 'Access'?
try { Remove-Item 'C:\protected.txt' } catch { Add-Content -Path 'error.log' -Value "Error: $_" }
AUse Set-Content instead of Add-Content
BUse Write-Error instead of Add-Content
CMove Add-Content outside the catch block
DAdd an if statement inside catch: if ($_ -match 'Access') { Add-Content ... }
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering errors in catch

    Use an if condition to check error message content before logging.
  2. Step 2: Apply condition inside catch block

    Add an if statement inside catch: if ($_ -match 'Access') { Add-Content ... } correctly adds if ($_ -match 'Access') to filter errors.
  3. Final Answer:

    Add an if statement inside catch: if ($_ -match 'Access') { Add-Content ... } -> Option D
  4. Quick Check:

    Filter errors with if inside catch [OK]
Quick Trick: Use if ($_ -match 'text') inside catch to filter errors [OK]
Common Mistakes:
  • Using Write-Error which does not filter
  • Logging errors outside catch block
  • Using Set-Content which overwrites logs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes