SQL Server 2019 - Agent job PowerShell step issue

  • I have a couple of SQL Agent job steps which run PowerShell commands of the form:

    Invoke-Command -ComputerName REMOTE -ScriptBlock {& "E:\Scripts\PowerShellScript.ps1" -ServerName "REMOTE\INSTANCE"}

    In each case, these steps have started failing with errors of the form:

    The job script encountered the following errors. These errors did not stop the script:

    Unhandled Exception: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
    at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
    at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
    at Microsoft.SqlServer.Management.PowerShell.Res.GetString(String key, Object arg0)
    at Microsoft.SqlServer.Management.PowerShell.SqlPowerShell.GetErrorRecordMessage(ErrorRecord errorRecord)
    at Microsoft.SqlServer.Management.PowerShell.SqlPowerShell.HandleAgentJob(RunspaceConfiguration config, IList`1 listOfScriptConfigurationEntries)
    at Microsoft.SqlServer.Management.PowerShell.SqlPowerShell.Main(String[] args)

    I've added a "Start-Transcript" to the top of the script but the output file doesn't get created, so I don't think the script is being executed.

    If I run "powershell.exe" as the proxy account defined for this job step and run the same command, it runs without issue. So the script is fine and the permissions aren't an issue.

    I'm wondering if there's something related to the version of PowerShell used by SQL but can't see why this would be different to a similar setup on another SQL 2019 instance - which works fine.

  • the server where it works is it a Named instance or default instance?

    and my advise, for multiple reasons, is never to use the powershell from SQL Server and instead use a command prompt to execute the script.

  • All our instances are named. Never had problems with PS job steps previously (aside from the limited command set) and am unsure why this is not working on this particular instance.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply