0

I have several powershell scripts that I am running from one powershell scripts.

I am using a try-catch to stop on errors. But that doesn't work with external scripts I'm calling. I cant use dot-source because some of the scripts require the 32 bit version of PowerShell (has to do with QuickBooks API call requiring 32bit)

So I am currently calling it using the full path name so i have something like this:

try {
# QB API requires powershell 32 bit: Open Sales Order by Item Report Call
& C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -file  $ScriptDir\QB_API_Open_Sales_Orders_by_Item.ps1

# QB API requires powershell 32 bit: Inventory List Call
& C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -file  $ScriptDir\QB_API_Inventory_List.ps1

# x64bit powershell: Convert QB_API Sales and Inventory Fiels from XML to CSV using XSLT
& C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -file  $ScriptDir\transform_xml.ps1

# x64bit powershell: run vendor vs sales file to get final output
& C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -file  $ScriptDir\Create_Sales_order_MPN_using_join.ps1
}
catch
{
Write-Warning $Error[0]
}

If I am dot-sourcing the scripts it works fine, but external calling it, it does not. Suggestions on how to catch the errors and stop the script?

moore1emu
  • 476
  • 8
  • 27
  • @AdminOfThings why don't I want to call write-error automatically when the error occurs? does doing the 2>$null allow for the catch part to display the error? – moore1emu Nov 11 '21 at 19:37
  • You could evaluate the return codes of your external calls. – stackprotector Nov 11 '21 at 21:04
  • if the script sets `$ErrorActionPreference = 'Stop'`, then `$lastexitcode` would be a non-zero value on error in the calling shell. – AdminOfThings Nov 11 '21 at 21:07
  • 2
    If you want `try-catch` to work with executables in PowerShell session, then you must do the following: 1) Set `$errorActionPreference = 'stop'` so that all errors are terminating. 2) Redirect error stream elsewhere for the executable call -> `2>$null` for example. Then you can choose to call `Write-Error` if you want, but regardless, `$error[0]` will contain your exception. – AdminOfThings Nov 11 '21 at 21:25
  • if you put your comments in an answer ill mark it as correct. thank you for your help. setting the erroractionpreference to stop appears to have done the trick – moore1emu Nov 12 '21 at 02:54

1 Answers1

1

If you want try-catch to work with executables in PowerShell session, then you must do the following:

  1. Set $errorActionPreference = 'stop' so that all errors are terminating
  2. Redirect error stream elsewhere for the executable call -> 2>$null for example.

$EAPBackup = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
try {
    C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -file  $ScriptDir\QB_API_Open_Sales_Orders_by_Item.ps1 2>$null
} catch {
    Write-Warning $error[0]
}
$ErrorActionPreference = $EAPBackup
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27