Home Forums Programming Powershell PS script works in ISE but fails in SQL JOB step with a syntax error RE: PS script works in ISE but fails in SQL JOB step with a syntax error

  • mstjean - Thursday, January 4, 2018 1:11 PM

    But when I paste it into a SQL JOB step on the SQL2016 box, it fails with a syntax error on this line:
        "'$($Row.$Prop)'"

    The reason this fails in a PowerShell step of SQL Agent is due to the tokens with Agent jobs. A token, when used in Agent job, is wrapped using "$()", so anything within that is picked up by SQL Agent as a token and it will try to resolve it as one. These are used differently in PowerShell and just don't mix well when you try to use them in a SQL Agent PowerShell job step.

    It is best to use a CmdExec step in SQL Agent as it will give you the most consistent experience to running scripts in normal PowerShell or PowerShell ISE.

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton