Sql-server – Execute Remote Powershell script from SQL Agent

powershellsql serversql-server-agent

A test SQL Server job hangs when it executes a Powershell script. It does work using my login and a Powershell window, but not using the Agent account and a job. The SQL Agent user has read/write/execute permissions to the directory with the powershell script.

Example:

Run as: SQL Server Agent Service Account

powershell.exe -file \\Server02\test2\Test.ps1

The agent account has been given access also.

This test file currently does something very basic: displays a string.

When it runs, it does not show an error message, because it just does not respond. Do you have any idea about what is happening, or which security settings are missing?

Best Answer

JNK was correct in the comment above. If I ran it in the command shell, there was a prompt saying

Security Warning Run only scripts that you trust

In order to avoid this message, I added -ExecutionPolicy Bypass to the command

Example: powershell.exe -ExecutionPolicy Bypass -file \Server02\test2\Test.ps1

The job is now able to complete.