Author | Sander Stad (@sqlstad), sqlstad.nl |
Availability | Windows, Linux, macOS |
Want to see the source code for this command? Check out New-DbaAgentJobStep on GitHub.
Want to see the Bill Of Health for this command? Check out New-DbaAgentJobStep.
New-DbaAgentJobStep creates a new job step for a job
New-DbaAgentJobStep creates a new job in the SQL Server Agent for a specific job
New-DbaAgentJobStep
[-SqlInstance] <DbaInstanceParameter[]>
[[-SqlCredential] <PSCredential>]
[-Job] <Object[]>
[[-StepId] <Int32>]
[-StepName] <String>
[[-Subsystem] <String>]
[[-SubsystemServer] <String>]
[[-Command] <String>]
[[-CmdExecSuccessCode] <Int32>]
[[-OnSuccessAction] <String>]
[[-OnSuccessStepId] <Int32>]
[[-OnFailAction] <String>]
[[-OnFailStepId] <Int32>]
[[-Database] <String>]
[[-DatabaseUser] <String>]
[[-RetryAttempts] <Int32>]
[[-RetryInterval] <Int32>]
[[-OutputFileName] <String>]
[-Insert]
[[-Flag] <String[]>]
[[-ProxyName] <String>]
[-Force]
[-EnableException]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
PS C:\> New-DbaAgentJobStep -SqlInstance sql1 -Job Job1 -StepName Step1
Create a step in "Job1" with the name Step1 with the default subsystem TransactSql.
PS C:\> New-DbaAgentJobStep -SqlInstance sql1 -Job Job1 -StepName Step1 -Database msdb
Create a step in "Job1" with the name Step1 where the database will the msdb
PS C:\> New-DbaAgentJobStep -SqlInstance sql1, sql2, sql3 -Job Job1 -StepName Step1 -Database msdb
Create a step in "Job1" with the name Step1 where the database will the "msdb" for multiple servers
PS C:\> New-DbaAgentJobStep -SqlInstance sql1, sql2, sql3 -Job Job1, Job2, 'Job Three' -StepName Step1 -Database msdb
Create a step in "Job1" with the name Step1 where the database will the "msdb" for multiple servers for multiple jobs
PS C:\> sql1, sql2, sql3 | New-DbaAgentJobStep -Job Job1 -StepName Step1 -Database msdb
Create a step in "Job1" with the name Step1 where the database will the "msdb" for multiple servers using pipeline
PS C:\> New-DbaAgentJobStep -SqlInstance sq1 -Job Job1 -StepName StepA -Database msdb -StepId 2 -Insert
Assuming Job1 already has steps Step1 and Step2, will create a new step Step A and set the step order as Step1, StepA, Step2
Internal StepIds will be updated, and any specific OnSuccess/OnFailure step references will also be updated
The target SQL Server instance or instances. You must have sysadmin access and server version must be SQL Server version 2000 or greater.
Alias | |
Required | True |
Pipeline | true (ByValue) |
Default Value |
The name of the job to which to add the step.
Alias | |
Required | True |
Pipeline | false |
Default Value |
The name of the step.
Alias | |
Required | True |
Pipeline | false |
Default Value |
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The sequence identification number for the job step. Step identification numbers start at 1 and increment without gaps.
Alias | |
Required | False |
Pipeline | false |
Default Value | 0 |
The subsystem used by the SQL Server Agent service to execute command.
Allowed values 'ActiveScripting','AnalysisCommand','AnalysisQuery','CmdExec','Distribution','LogReader','Merge','PowerShell','QueueReader','Snapshot','Ssis','TransactSql'
The default is 'TransactSql'
Alias | |
Required | False |
Pipeline | false |
Default Value | TransactSql |
Accepted Values | ActiveScripting,AnalysisCommand,AnalysisQuery,CmdExec,Distribution,LogReader,Merge,PowerShell,QueueReader,Snapshot,Ssis,TransactSql |
The subsystems AnalysisScripting, AnalysisCommand, AnalysisQuery ned the server property to be able to apply
Alias | |
Required | False |
Pipeline | false |
Default Value |
The commands to be executed by SQLServerAgent service through subsystem.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The value returned by a CmdExec subsystem command to indicate that command executed successfully.
Alias | |
Required | False |
Pipeline | false |
Default Value | 0 |
The action to perform if the step succeeds.
Allowed values "QuitWithSuccess" (default), "QuitWithFailure", "GoToNextStep", "GoToStep".
The text value van either be lowercase, uppercase or something in between as long as the text is correct.
Alias | |
Required | False |
Pipeline | false |
Default Value | QuitWithSuccess |
Accepted Values | QuitWithSuccess,QuitWithFailure,GoToNextStep,GoToStep |
The ID of the step in this job to execute if the step succeeds and OnSuccessAction is "GoToStep".
Alias | |
Required | False |
Pipeline | false |
Default Value | 0 |
The action to perform if the step fails.
Allowed values "QuitWithSuccess" (default), "QuitWithFailure", "GoToNextStep", "GoToStep".
The text value van either be lowercase, uppercase or something in between as long as the text is correct.
Alias | |
Required | False |
Pipeline | false |
Default Value | QuitWithFailure |
Accepted Values | QuitWithSuccess,QuitWithFailure,GoToNextStep,GoToStep |
The ID of the step in this job to execute if the step fails and OnFailAction is "GoToStep".
Alias | |
Required | False |
Pipeline | false |
Default Value | 0 |
The name of the database in which to execute a Transact-SQL step. The default is 'master'.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The name of the user account to use when executing a Transact-SQL step.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The number of retry attempts to use if this step fails. The default is 0.
Alias | |
Required | False |
Pipeline | false |
Default Value | 0 |
The amount of time in minutes between retry attempts. The default is 0.
Alias | |
Required | False |
Pipeline | false |
Default Value | 0 |
The name of the file in which the output of this step is saved.
Alias | |
Required | False |
Pipeline | false |
Default Value |
This switch indicates the new step is inserted at the specified stepid.
All following steps will have their IDs incremented by, and success/failure next steps incremented accordingly
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
AppendAllCmdExecOutputToJobHistory Job history, including command output, is appended to the job history file.
AppendToJobHistory Job history is appended to the job history file.
AppendToLogFile Job history is appended to the SQL Server log file.
AppendToTableLog Job history is appended to a log table.
LogToTableWithOverwrite Job history is written to a log table, overwriting previous contents.
None Job history is not appended to a file.
ProvideStopProcessEvent Job processing is stopped.
Alias | |
Required | False |
Pipeline | false |
Default Value | |
Accepted Values | AppendAllCmdExecOutputToJobHistory,AppendToJobHistory,AppendToLogFile,AppendToTableLog,LogToTableWithOverwrite,None,ProvideStopProcessEvent |
The name of the proxy that the job step runs as.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The force parameter will ignore some errors in the parameters and assume defaults.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
Shows what would happen if the command were to run. No actions are actually performed.
Alias | wi |
Required | False |
Pipeline | false |
Default Value |
Prompts you for confirmation before executing any changing operations within the command.
Alias | cf |
Required | False |
Pipeline | false |
Default Value |