locked
If script errors, ignore error and keep trying until successful RRS feed

  • Question

  • I've been working on a script (my very first) and figured out many of the challenges but the last issue is how to handle an error. The script first determines if the major version of Powershell is 3 or greater. If so, it runs a specific script... If not, it runs a different script that is compatible with v2 or even 1.... (Note: I'm not allowed to update Powershell using the script nor install any updates as per customer IT staff).... The only issue now is the error and I'm still trying to resolve it on my own, I'd be grateful for any guidance!

    The error that could occur: export-csv : The process cannot access the file because it is being used by another process


    new-psdrive -Name "S" -PSProvider FileSystem -Root "\\host\Share$"
    $PowershellVersion = $PSVersionTable.PSVersion.Major

    If ($PowershellVersion -ge 3)
    {
    Invoke-Expression "&'S:\Temp\APCv10a.ps1'"
    }
    Else 
    {
    Invoke-Expression "&'S:\Temp\APCv10b.ps1'"
    }


    Jason Ostrowski



    • Edited by HomeTheaterGuy Tuesday, May 8, 2018 2:19 PM
    • Moved by Bill_Stewart Friday, July 27, 2018 6:46 PM This is not "scripts on demand"
    Tuesday, May 8, 2018 12:29 PM

All replies

  • If you want to set the error action preference for your whole script you can do this by adding e.g. $ErrorActionPreference = "Silently Continue" at the beginning of your script.

    Have a look at the possible settings of that variable: https://social.technet.microsoft.com/wiki/contents/articles/20878.error-action-preference-in-powershell.aspx

    Maybe taking a look at try/catch would help you too.

    Tuesday, May 8, 2018 12:38 PM
  • I see no error.

    You need to learn to format code correctly and to post it correctly using the code posting tool.

    Don't add pointless comments.

    New-PsDrive -Name S -PSProvider FileSystem -Root '\\host\Share$'
    if($PSVersionTable.PSVersion.Major -ge 3){
        S:\Temp\APCv10a.ps1
    }else{
        S:\Temp\APCv10b.ps1
    }
    Remove-PSDrive -Name S

    "3 or greater" is "-ge"!  Say it as "greater than or equal to 3".

    Notice the code is self-explanatory.  It needs no comments.

    This is not VB6.  Use the above formatting.  Keywords like if/where/for etc do not get caps.

    See the following to get correct training in syntax and style.

    Learn PowerShell  
    PowerShell Documentation
    PowerShell Style Guidelines


    \_(ツ)_/


    • Edited by jrv Tuesday, May 8, 2018 12:42 PM
    Tuesday, May 8, 2018 12:40 PM
  • JRV, thanks for the input! I'm learning Powershell as I am going along and compared to my first iteration of the script, it has come leaps and bounds.... Yes, it should have been -ge 3 based on what I posted and originally it was -gt 2 because only PS 2 and under has issues with a few of the commands.... 

    The comments are there because the company has an SOP for programming/scripting that states you must have a brief but succinct description of what the script is targeting, what each section of code does and version number (even if it is final).... 


    Jason Ostrowski

    Tuesday, May 8, 2018 1:48 PM
  • Your comments were not in keeping with the company requirement and served no purpose.  What the company is stating is that you need a simple narrative explaining the purpose of the code and not just a single word or two repetitive of the name of the command being used.

    Read the link on "style" I posted to learn how to write correct comments and what comments to not use.

    You still have not addressed the error you claim you are getting In this script we would never ignore errors as that would produce bad results.

    You also have not fixed the code posted in your original post.  Use the code posting tool.

     


    \_(ツ)_/

    Tuesday, May 8, 2018 1:56 PM
  • Thanks for your response Toby... I did forget to add the common error and that is the .csv is locked. The process writes to a csv (would have preferred a DB but not an option) and the script runs from multiple stations at the same time targeting the common .csv... So the common error is "export-csv : The process cannot access the file '\\server\Share$\Temp\name.csv' because it is being used by another process". This runs everyday and the name of the .csv has the current date in it "050818variable.csv"; so it changes daily. This is a temporary process to deal with an entity that has been acquired, is now on the company MPLS but is still a separate entity; most likely for the next year to year and a half.

    Jason Ostrowski

    Tuesday, May 8, 2018 1:58 PM
  • You need to trap the error and take correct action to prevent an out-of-control script from creating repeated errors.

    help about_try_catch_finally

    All of these things would be learned in a few short hours with the tutorial.


    \_(ツ)_/

    Tuesday, May 8, 2018 2:04 PM
  • new-psdrive -Name "S" -PSProvider FileSystem -Root "\\host\Share$"
    $PowershellVersion = $PSVersionTable.PSVersion.Major
    
    If ($PowershellVersion -ge 3)
    {
    Invoke-Expression "&'S:\Temp\APCv10a.ps1'"
    }
    Else 
    {
    Invoke-Expression "&'S:\Temp\APCv10b.ps1'"
    }

    Where is the error code you are referring too?

    The process cannot access the file because it is being used by another process

    you can use something like

    Export-csv -ea Silentlycontinue -NotypeFormat
    
    

    • Edited by j0rt3g4 Wednesday, May 9, 2018 5:14 AM
    Wednesday, May 9, 2018 5:12 AM
  • There is no need to use "&" or "Invoke-Expression".  Using both makes no sense at all.

    A script is run by just entering the full path to the script there is no need for anything else.

    S:\Temp\APCv10a.ps1

    There is also no need to create a drive as this also works in Windows.

    \host\Share$\Temp\APCv10a.ps1


    \_(ツ)_/

    Wednesday, May 9, 2018 5:22 AM