PowerShell Beginner – Trying to Terminate on OpenError in Script

I’m extremely new to PowerShell and this is my first script. I am attempting to unpivot CSV and Tab Delimited files for import into a data warehouse using a script to automate the process. While I’m sure the script is terrible, is has been getting the job done data-transformation-wise. If you have advice in that regard it would be welcome but it is not the focus of my question.

I would like to catch exceptions and perhaps log them. The first step, from my googling, seems to be using “-ErrorAction Stop” to terminate the script when a non-terminating error occurs. The most obvious error would be trouble accessing the two files I am working with ($inFile, $outFile). Either $inFile could be missing or $outFile could be write-locked. When these errors occur:

“Import-Csv : Could not find file ****.”

or

“Export-Csv : The process cannot access the file **** because it is being used by another process.”

the script does NOT terminate as I am expecting it to despite adding the “-ErrorAction Stop” bit. The last “Write-Output” command always executes. I looked at three “tutorials” for catching exceptions in PowerShell and they all just casually say to add that bit to cause the error to terminate the script. I’m looking for help understanding what it is I am doing wrong.

Thank you.

Link to script: https://pastebin.com/a1eXQf44

submitted by /u/theparadox1083
[link] [comments]

Leave a Reply