VMware Cloud Community
glezama
Contributor
Contributor

Multiple Powercli scripts writing to the same CSV file simultaneously

We have multiple powercli scripts running simultaneously.  We would like them to write the status of their operations to the same csv file.
Is it possible for multiple scripts to write to the same CSV file without creating a locking issue for powershell?

0 Kudos
1 Reply
LucD
Leadership
Leadership

No, the Export-Csv cmdlet places a lock on the target file.

But you could use a Try-Catch construction to determine if there is a lock, and wait in a loop until the lock is gone.

See for example this Test-FileLockhttps://stackoverflow.com/questions/24992681/powershell-check-if-a-file-is-locked function.

Another, more advanced, way of working would be to use a mutex, but you wouldn't be able to use Export-Csv in that case.

See Boe's post Using Mutexes to Write Data to the Same Logfile Across Processes With PowerShell


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos