Solved

Get-Content File Locked test?

Posted on 2010-09-22
4
2,218 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

Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Set OWA language and time zone in Exchange for individuals, all users or per database.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

747 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