Search code examples
sql-serverpowershellexecutionpolicy

SQL Server job can't run PowerShell because "scripts execution disabled"


I upgraded a Windows Server 2012 to 2019, but kept the SQL Server (110)

My sql agent jobs stopped working, the ones using PowerShell commands.

I get the error message

A job step received an error at line 1 in a PowerShell script. 
The corresponding line is 'import-module SQLPS  -DisableNameChecking'. 
Correct the script and reschedule the job. 
The error information returned by PowerShell is: 
'File C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1 
cannot be loaded because the execution of scripts is disabled on this system. 
Please see "get-help about_signing" for more details.

The Sqlps.ps1 IS signed with a valid signature, and I even temporarily tried to Set-ExecutionPolicy Unrestricted , but that didn't help either. (All suggested by chatgpt ;-) )

My script should download a file from an url and place it in c:\tmp

EDIT: Solved, I set the script execution policy to "Allow local scripts and remote signed scripts" in group policy for computer; it was pereviously "Not Configured"


Solution

    • If the effective execution policy is Restricted, even signed scripts won't help you.

    • What matters is what the effective execution policy is for the user identity that your services / scheduled tasks run as, which you can control as follows:

      • Ad hoc, in a given PowerShell CLI call (powershell.exe [-Command] ... / powershell.exe -File ...), precede the (possibly implied) -Command / -File parameter with an -ExecutionPolicy argument, e.g. - if you fully trust the script (eventually) getting invoked - -ExecutionPolicy Bypass

      • Persistently, via a machine-wide execution policy (requires elevation (administrative privileges)):

        • Via Set-ExecutionPolicy -Scope AllUsers
        • Via GPO policies (which override persistent Set-ExecutionPolicy configurations).

    See this answer for additional information.