Link to home
Start Free TrialLog in
Avatar of rwskas
rwskasFlag for United States of America

asked on

Get-Content File Locked test?

I have a powershell script which every 30 seconds tries to read teh content of an output file. The output file is the result of running a script like this:
C:\scripts\myscript.ps1 > C:\temp\outputfile.txt

The problem I am running into is that occasionally (randomly) the Get-Content command files due to the file being locked.

How can I test that the file is not locked prior to my Get-Content command?
A function would be easiest, so that I could:
If (Test $filename) { get-content ... }

Thoughts?
ASKER CERTIFIED SOLUTION
Avatar of soostibi
soostibi
Flag of Hungary image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwskas

ASKER

Yeah, Thats how I have it setup now.. I would prefer to check it prior to trying to get the content though, because I dont really want to turn on EA silentlycontinue as I would still like to review other error messages.
In the failure part you can check end emit the error by reading the $error[0] variable.
Avatar of rwskas

ASKER

Your solution works, its just not what I was asking..