Does PowerShell's Get-Content lock the target files?

Asked 2 years ago, Updated 2 years ago, 420 views

View the log files that the script or RPA outputs in real time with the Get-Content option in PowerShell.Sometimes, the script and RPA terminate abnormally due to the error of not opening the log file, and my colleagues say that it is because I open the file with Get-Content, but I don't think Get-Content will lock the file exclusively like UNIX tail.However, no supporting information has been found at this time.

PowerShell is open-source, so I think the source will eventually solve it, but it's much less powerful to get to the destination.Do you have any PowerShell documentation (or information that I misunderstood that Get-Content is locked exclusively)?

windows powershell

2022-09-30 22:04

1 Answers

By default, fopen() used by both UNIX and Windows in file opening is not exclusive, and no exclusivity is provided by standard (there seems to be a glibc extension).

Separately, however, the CreateFile() (historically open files with this API) is a default exclusion specification that allows shared access if necessary.Additionally, it is divided into read and write permissions.

Even if the Get-Content-Wait side does not exclude and allows shared access, the lock conflicts and fails to open the file if the write program excludes CreateFile() by calling it roughly with the default argument.

I'm not confident about the PowerShell source code, but around here FileShare.ReadWrite I feel like I'm specifying permission to read or write.


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.