Forum Discussion
AkitoTheGambler
Nov 24, 2023Copper Contributor
Powershell keeps file open Out-file -append / Add-content
I have the following module When running it it fails to write into the files as it claims to be in use by a process when running add-content or out-file -append it keeps it open but idk why. the mo...
AkitoTheGambler
Nov 24, 2023Copper Contributor
unfortunately we are using ConstrainedLanguage mode so we cannot use it
but running out-file manually doesnt usually lock the file after running
but somehow in some cases in scriptblocks or functions it does.
i dont understand why tho. (but not always, idk what the conditions are that cause it to behave like this)
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_modes?view=powershell-5.1#constrainedlanguage-mode
AkitoTheGambler
Nov 27, 2023Copper Contributor
i was trying to figure out why this happens as it only breaks inside the Scriptblock but doesnt hangup and keep it open if its everything outside the scriptblock
so
Write-Output $certMessage | Out-File -FilePath $logFile -Append
Write-Output $timestampMessage | Out-File -FilePath $logFile -Append
always works fine
but everything inside the foreach scriptblock fails
- guinoronhaApr 15, 2024Copper ContributorHi AkitoTheGambler,
Did you find a solution for this? I have the same issue. I use a function to write data to a log file and I get "random" access denied errors when writing to the file.