Solved

Get-Content File Locked test?

Posted on 2010-09-22
4
2,240 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:rwskas
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 33742608
I would do this:

$content = Get-Content C:\yourlockedfile.txt -ErrorAction silentlycontinue
if($?){"Success"}else{"faliure"}
0
 
LVL 6

Author Comment

by:rwskas
ID: 33744708
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.
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33744838
In the failure part you can check end emit the error by reading the $error[0] variable.
0
 
LVL 6

Author Closing Comment

by:rwskas
ID: 33788020
Your solution works, its just not what I was asking..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A safe way to clean winsxs folder from your windows server 2008 R2 editions
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now