none
invoke-ascmd error does not trigger catch RRS feed

  • Question

  • I run this command to backup a database to a network path.
    Invoke-ASCmd -Server $ASServer -Query ($ASBackupQry.Replace('$(Database)', $Database))

    I know the path does not exist and will trigger the an error
    <return xmlns="urn:schemas-microsoft-com:xml-analysis"><root xmlns="urn:schemas-microsoft-com:xml-analysis:empty"><Exception xmlns="urn:schemas-microsoft-com:xml-analysis:exception" /><Messages xmlns="urn:schemas-microsoft-com:xml-analysis:exception"><Error ErrorCode="-1056833536" Description="File system error: The following error occurred during a file operation:  An unexpected network error occurred. . (networkpath.abf)." Source="Microsoft SQL Server 2016 Analysis Services" HelpFile="" /></Messages></root></return>

    I run this in a try/catch, but it does not trigger the catch.  $ErrorActionPreference is set to stop.  My work around is to send the output to a string object and then search for exception and trigger the catch manually. 

    Am I doing something wrong?

    Friday, November 8, 2019 11:33 PM

All replies

  • You must add "ErrorAction Stop" to the command or the exception handler will not be called.


    \_(ツ)_/

    Friday, November 8, 2019 11:37 PM
    Moderator
  • Try/Catch only works with command terminating or script terminating errors.

    Saturday, November 9, 2019 12:56 AM
  • Try/Catch only works with command terminating or script terminating errors.

    And only if the action is "Stop".


    \_(ツ)_/

    Saturday, November 9, 2019 1:06 AM
    Moderator