Options

Powershell help!

wsousa1wsousa1 Member Posts: 8 ■□□□□□□□□□
Hello All,

i have written a script that is used to automate application web releases. this script will copy new releases to a temp folder in our prod environment, qc it , if it passes then proceed with a backup of production, qc again then finally deploy the code to production. everything works fine, but every now and then some random web file is locked by another process.

Sample error:
The process cannot access the file 'filenamehere.aspx' because it is being used by another process.
At C:\***** ******.ps1:8 char:1
+ Invoke-Command -computername computername -Creden ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (Login.aspx:FileInfo) [Copy-Item], IOException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemIOError,Microsoft.PowerShell.Commands.CopyItemCommand
+ PSComputerName : computername

Is there a way to have powershell catch the error and retry to copy the file? Not too long ago when we manually copied and pasted the release code, an error would come up stating that a file was in use, so we would hit retry until it worked. Not sure if the same can be done.

Any help would be greatly appreciated.

Thanks!

Comments

  • Options
    jelevatedjelevated Member Posts: 139
    You can but before providing try/catch code, is it possible to stop whatever is using the file to release the hook instead?
  • Options
    wsousa1wsousa1 Member Posts: 8 ■□□□□□□□□□
    Its a possibility. Lets try that as an option and i can test. I always try to give it some time and "try again" as it usually works the second or third time.
  • Options
    QordQord Member Posts: 632 ■■■■□□□□□□
    Yes, there are a few different ways to do this. You could try/catch, do/while, do/until....they'd all do what you want.

    There's a good (and simple!) chunk of code here that tries to do something 3 times (waiting 30 second between tries):
    Powershell: Simple retry logic | Thomas Maurer

    I bet that would do what you need. If not, it's a good starting place.
  • Options
    wsousa1wsousa1 Member Posts: 8 ■□□□□□□□□□
    i will try that right now. i'll keep you posted.
  • Options
    wsousa1wsousa1 Member Posts: 8 ■□□□□□□□□□
    OMG i couldn't be more excited. it worked like a charm. i was testing with an excel document, perfect way to test a locked file btw. i opened the excel doc on the destination dir, ran the script, while it was retrying i closed out of the excel doc and it completed the transfer. Thank you guys for the help, keep up the good work!
Sign In or Register to comment.