Execute Powershell Script from Agent Job

Hi experts. I need to run a Powershell script from an agent Job. The script accepts an argument also.

This is how I configured the Job Step
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
arguments d:\sqltest.ps1 ServerName\InstanceName

It starts running but never completes.
Does anyone see anything wrong with the syntax above?
Thanks

What service account is the job running as? What if you ran the script yourself manually?

The job runs as an AD account that has the sysadmin role and is in Local Admin group.
When I run "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
arguments d:\sqltest.ps1 ServerName\InstanceName from Command Prompt it fails with
this error:
"The term arguments is not the name of a cmdlet, function or script file....."

Your issue is not sql, its your powershell.

What is the first line in powershell script?

Why are you trying to start Powershell - there is a Powershell subsystem in SQL Server Agent, and all you need to do is put in the script to be run.

Set the 'Run As' to your proxy account - put the script to be executed with parameters as if you are running from a PS command line and your script should run in the SQLPS environment.

1 Like