Home Forums Programming Powershell SQL Agent job not failing when Powershell script errors RE: SQL Agent job not failing when Powershell script errors

  • I finally got this working and thought I'd post the solution.

    In the SQL Agent job Step I changed the type to:

    Type: Operating sytem (CmdExec)

    And the Powershell script looks like this:

    Param(

    $source,

    $destination

    )

    try {

    Copy-Item $source -Destination $destination -ea stop

    } catch {

    Write-Error $_

    [System.Environment]::Exit(1)

    }

    The Write-Error $_ is not necessary but it gives a more descriptive error message for someone not familiar with powershell (like me). The Exit(1) passes back to the SQL Agent job which fails if not 0.

    Thanks everyone for the help it pushed me in the correct direction.

    Thanks,

    Sqlraider