Bird
0
0

You have a script that writes logs using Out-File with -Append. Sometimes logs are missing lines after simultaneous runs. What is the best way to fix this?

hard📝 Application Q9 of 15
PowerShell - File and Directory Operations
You have a script that writes logs using Out-File with -Append. Sometimes logs are missing lines after simultaneous runs. What is the best way to fix this?
AUse <code>Set-Content</code> instead of <code>Out-File</code>
BRemove the -Append parameter
CWrite logs to different files each time
DAdd a lock mechanism to serialize file writes
Step-by-Step Solution
Solution:
  1. Step 1: Understand concurrency issue

    Simultaneous writes cause race conditions, losing lines.
  2. Step 2: Identify solution

    Adding a lock or synchronization ensures one write at a time, preserving all lines.
  3. Final Answer:

    Add a lock mechanism to serialize file writes -> Option D
  4. Quick Check:

    Prevent race conditions with locking [OK]
Quick Trick: Use locks to avoid concurrent file write conflicts [OK]
Common Mistakes:
  • Switching to Set-Content (still overwrites)
  • Removing -Append (loses data)
  • Writing to different files (not always practical)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes