I am trying to use a text file for a lock mechanism.
The idea is that once the powershell script is running and holding the file handle, other processes will not be able to to open it and will wait until it released.
$file = [System.io.File]::Open('D:\file.lock', 'Open', 'Read', 'None')
$reader = New-Object System.IO.StreamReader($file)
$text = $reader.ReadToEnd()
$text | Out-File $file
$reader.Close()
$file.Close()
The locking is working well, however I want the once the script releases the file, it should do a 'touch' so the file's Last Modified Date will be changed
The problematic code of mine is $text | Out-File $file
as it is not doing a thing
How can I save the file, or do a 'touch'