SQL SERVER Powershell fails with “set-executionpolicy RemoteSigned -scope process -Force”

This is the second or third time I have run into the message below when trying to run a Powershell script from a SQL SERVER agent job. Even after running set-executionpolicy to something other then RemoteSigned.
This issue has occurred while running Microsoft Windows 2012 and SQL SERVER 2012. And I was running the SQL AGENT step using a proxy account.

Executed as user: DOMAIN\testuser01. A job step received an error at line 1 in a PowerShell script. 
The corresponding line is 'set-executionpolicy RemoteSigned -scope process -Force'. 
Correct the script and reschedule the job. The error information returned by PowerShell is: 'Security error.  '.  Process Exit Code -1.  
The step failed.

Step 1 create credential
Step 2 create proxy from credential
Step 3 Assign proxy account to job step
Step 4. Paste the Powershell code into the job step and run the SQL AGENT job, you receive the message above.
Step 5. In an attempt to remediate the issue I performed the following steps.
Start SQL Server Management Studio as Administrator, or you cannot change the Powershell execution policy

Right click on SQL SERVER instance from from SQL Server Management Studio > Start Powershell

PS SQLSERVER:\SQL\SQLSERVER01\INSTANCE01> set-executionpolicy -scope Process unrestricted
PS SQLSERVER:\SQL\SQLSERVER01\INSTANCE01> get-executionpolicy -list

                                  Scope                         ExecutionPolicy
                                  -----                         ---------------
                          MachinePolicy                            Unrestricted
                             UserPolicy                               Undefined
                                Process                            Unrestricted
                            CurrentUser                               Undefined
                           LocalMachine                            Unrestricted

Even after setting the execution policy to Unrestricted the Powershell script would still not execute. Note I do not recommend setting the execution policy to Unrestricted. This blog post is geared towards figuring out why Powershell is not honoring the set-executionpolicy cmdlet. The fix was to modify registry key below and set the registry key to the appropriate execution policy.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps110\Unrestricted
REG_SZ Unrestricted
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s