0

I have a situation where I am connecting to a server and performing an operation using powershell, in that if the error comes then it should goto catch block where I am doing some other operation.

 try{
 $Result = Invoke-Command -ComputerName "cmpname" -scriptblock {DTExec.exe /File "Z:\aaaaaaaSSIS\zzzzProjects\Z_Test\Integration test\test.dtsx"}  -ErrorAction Stop -credential $creds 
}

catch {
    "****An error occurred reading $file
}

Now in the above case if error is coming in -scriptblock it is not going in catch block. Where as if there is issue in Invoke-Command -ComputerName then it is going in catch block.

So I need the script to go in catch block when error occurs any where while executing the line and specially of -script block.

Anirudh Agarwal
  • 655
  • 2
  • 7
  • 29

1 Answers1

1

Maybe your are not treating your error as terminating. Try This:

Treating All Errors as Terminating: It is also possible to treat all errors as terminating using the ErrorActionPreference variable. You can do this either for the script your are working with or for the whole PowerShell session. To set it in a script, make the first line $ErrorActionPreference = Stop. To set it for the session, type $ErrorActionPreference = Stop at the PowerShell console.

Source

Felix Quehl
  • 744
  • 1
  • 9
  • 24