Run Windows PowerShell Steps in SQL Server Agent
Use SQL Server Agent to run SQL Server PowerShell scripts at schedule times.
Before you begin: Limitations and Restrictions
To run PowerShell from SQL Server Agent, using: PowerShell Job Step, Command Prompt Job Step
Before You Begin
There are several types of SQL Server Agent job steps. Each type is associated with a subsystem that implements a specific environment, such as a replication agent or command prompt environment. You can code Windows PowerShell scripts, and then use SQL Server Agent to include the scripts in jobs that run at scheduled times or in response to SQL Server events. Windows PowerShell scripts can be run using either a command prompt job step or a PowerShell job step.
Use a PowerShell job step to have the SQL Server Agent subsystem run the
sqlps
utility, which launches PowerShell 2.0 and imports thesqlps
module.Use a command prompt job step to run PowerShell.exe, and specify a script that imports the
sqlps
module.
Limitations and Restrictions
Caution
Each SQL Server Agent job step that runs PowerShell with the sqlps
module launches a process which consumes approximately 20 MB of memory. Running large numbers of concurrent Windows PowerShell job steps can adversely impact performance.
Create a PowerShell Job Step
To create a PowerShell job step
Expand SQL Server Agent, create a new job or right-click an existing job, and then click Properties. For more information about creating a job, see Creating Jobs.
In the Job Properties dialog, click the Steps page, and then click New.
In the New Job Step dialog, type a job Step name.
In the Type list, click PowerShell.
In the Run as list, select the proxy account with the credentials that the job will use.
In the Command box, enter the PowerShell script syntax that will be executed for the job step. Alternately, click Open and select a file containing the script syntax.
Click the Advanced page to set the following job step options: what action to take if the job step succeeds or fails, how many times SQL Server Agent should try to execute the job step, and how often retry attempts should be made.
Create a Command Prompt Job Step
To create a CmdExec job step
Expand SQL Server Agent, create a new job or right-click an existing job, and then click Properties. For more information about creating a job, see Creating Jobs.
In the Job Properties dialog, click the Steps page, and then click New.
In the New Job Step dialog, type a job Step name.
In the Type list, choose Operating system (CmdExec).
In Run as list, select the proxy account with the credentials that the job will use. By default, CmdExec job steps run under the context of the SQL Server Agent service account.
In the Process exit code of a successful command box, enter a value from 0 to 999999.
In the Command box, enter powershell.exe with parameters specifying the PowerShell script to be run.
Click the Advanced page to set job step options, such as: what action to take if the job step succeeds or fails, how many times SQL Server Agent should try to execute the job step, and the file where SQL Server Agent can write the job step output. Only members of the sysadmin fixed server role can write job step output to an operating system file.