Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Quiting a ps1 script when an error occurs

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-08-2006   #1 (permalink)
Marco Shaw
Guest


 

Quiting a ps1 script when an error occurs

I've got a PSH prompt open, and have called a .ps1 script. It contains an
error.

It seemingly loops forever, and if I Ctrl-C it stops.

That's fine and all, but the script seems to hold on to files it
reads/writes:
PS C:\> gc 1.csv|select-object -first 1
Get-Content : The process cannot access the file 'C:\1.csv' because it is
being used by another process.
At line:1 char:3
+ gc <<<< 1.csv|select-object -first 1

Only way I know out is to kill the PSH prompt and open a new one.

There must be a better way?

Marco



My System SpecsSystem Spec
Old 11-08-2006   #2 (permalink)
Adam Milazzo
Guest


 

Re: Quiting a ps1 script when an error occurs

Marco Shaw wrote:
> I've got a PSH prompt open, and have called a .ps1 script. It contains an
> error.
>
> It seemingly loops forever, and if I Ctrl-C it stops.
>
> That's fine and all, but the script seems to hold on to files it
> reads/writes:
> PS C:\> gc 1.csv|select-object -first 1
> Get-Content : The process cannot access the file 'C:\1.csv' because it is
> being used by another process.
> At line:1 char:3
> + gc <<<< 1.csv|select-object -first 1
>
> Only way I know out is to kill the PSH prompt and open a new one.
>
> There must be a better way?
>
> Marco


Mmmm... that seems like a pretty bad bug. Terminated pipelines should
be terminated, not still holding open file handles. I guess it's a bit
trickier given that there's no automatic cleanup for threads, only
processes, and PowerShell runs pipelines in threads.
My System SpecsSystem Spec
Old 11-09-2006   #3 (permalink)
Maximilian Hänel
Guest


 

Re: Quiting a ps1 script when an error occurs

Hi Marco,

> That's fine and all, but the script seems to hold on to files it
> reads/writes:
> PS C:\> gc 1.csv|select-object -first 1
> Get-Content : The process cannot access the file 'C:\1.csv' because it is
> being used by another process.
> At line:1 char:3
> + gc <<<< 1.csv|select-object -first 1
>
> Only way I know out is to kill the PSH prompt and open a new one.


What happens when you enter

PS C:\> [GC]::Collect(); [GC]::WaitForPendingFinalizers()

a few times? Are the files still locked?

cu

Max
My System SpecsSystem Spec
Old 11-09-2006   #4 (permalink)
Jason Mobarak
Guest


 

Re: Quiting a ps1 script when an error occurs

On Nov 9, 5:39 am, Maximilian Hänel <ngS...@smjh.de> wrote:
> Hi Marco,
>
> > That's fine and all, but the script seems to hold on to files it
> > reads/writes:
> > PS C:\> gc 1.csv|select-object -first 1
> > Get-Content : The process cannot access the file 'C:\1.csv' because it is
> > being used by another process.
> > At line:1 char:3
> > + gc <<<< 1.csv|select-object -first 1

>
> > Only way I know out is to kill the PSH prompt and open a new one.What happens when you enter

>
> PS C:\> [GC]::Collect(); [GC]::WaitForPendingFinalizers()
>
> a few times? Are the files still locked?


This works for me if I do something like:

> (gi "foo.txt").Open([System.IO.FileMode]"Open")

CanRead : True
[... goodness ...]

> (gi "foo.txt").Open([System.IO.FileMode]"Open")

Exception calling "Open" with "1" argument(s): "The process cannot
access the file 'C:\users\jam\repos\foo.txt' because it is being
used by another process."
[... badness ...]

> [GC]::Collect(); [GC]::WaitForPendingFinalizers()


> $f.open([System.IO.FileMode]"Open")

CanRead : True
[... goodness ...]

My System SpecsSystem Spec
Old 11-09-2006   #5 (permalink)
Marco Shaw
Guest


 

Re: Quiting a ps1 script when an error occurs

> What happens when you enter
>
> PS C:\> [GC]::Collect(); [GC]::WaitForPendingFinalizers()
>
> a few times? Are the files still locked?


I fixed my problem, so there's no lock for me to test anymore. I'll
research what you're telling me above. Looks like something useful in case
I get in trouble again.


My System SpecsSystem Spec
Old 11-11-2006   #6 (permalink)
Alex K. Angelopoulos [MVP]
Guest


 

Re: Quiting a ps1 script when an error occurs


"Jason Mobarak" <jason.mobarak@gmail.com> wrote in message
news:1163118105.449805.314680@m7g2000cwm.googlegroups.com...
On Nov 9, 5:39 am, Maximilian Hänel <ngS...@smjh.de> wrote:

> PS C:\> [GC]::Collect(); [GC]::WaitForPendingFinalizers()


That's a good line to be remembered for, Jason.


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Delete Items When Unknown Error Occurs Kayla Vista mail 0 06-14-2008 11:48 AM
Cannot Upgrade to Vista - An Error Occurs (Xp SP3) DsurioN Vista installation & setup 6 04-27-2008 04:30 PM
Script file has 'OS Handle' error when run from script Jay PowerShell 1 09-18-2007 07:06 PM
When trying to un-junk mails Windows Mail has decied is Junk an error occurs Rasmus Bertelsen Vista mail 2 07-08-2006 04:41 AM
Password does not work when idle-time lockout occurs wineward Vista account administration 0 06-22-2006 09:07 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51