Author | Sander Stad (@sqlstad), sqlstad.nl |
Availability | Windows, Linux, macOS |
Want to see the source code for this command? Check out New-DbaAgentJob on GitHub.
Want to see the Bill Of Health for this command? Check out New-DbaAgentJob.
New-DbaAgentJob creates a new job
New-DbaAgentJob makes is possible to create a job in the SQL Server Agent.
It returns an array of the job(s) created
New-DbaAgentJob
[-SqlInstance] <DbaInstanceParameter[]>
[[-SqlCredential] <PSCredential>]
[-Job] <String>
[[-Schedule] <Object[]>]
[[-ScheduleId] <Int32[]>]
[-Disabled]
[[-Description] <String>]
[[-StartStepId] <Int32>]
[[-Category] <String>]
[[-OwnerLogin] <String>]
[[-EventLogLevel] <Object>]
[[-EmailLevel] <Object>]
[[-PageLevel] <Object>]
[[-EmailOperator] <String>]
[[-NetsendOperator] <String>]
[[-PageOperator] <String>]
[[-DeleteLevel] <Object>]
[-Force]
[-EnableException]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
PS C:\> New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Description 'Just another job'
Creates a job with the name "Job1" and a small description
PS C:\> New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Disabled
Creates the job but sets it to disabled
PS C:\> New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -EventLogLevel OnSuccess
Creates the job and sets the notification to write to the Windows Application event log on success
PS C:\> New-DbaAgentJob -SqlInstance SSTAD-PC -Job 'Job One' -EmailLevel OnFailure -EmailOperator dba
Creates the job and sets the notification to send an e-mail to the e-mail operator
PS C:\> New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Description 'Just another job' -Whatif
Doesn't create the job but shows what would happen.
PS C:\> New-DbaAgentJob -SqlInstance sql1, sql2, sql3 -Job 'Job One'
Creates a job with the name "Job One" on multiple servers
PS C:\> "sql1", "sql2", "sql3" | New-DbaAgentJob -Job 'Job One'
Creates a job with the name "Job One" on multiple servers using the pipe line
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. The name must be unique and cannot contain the percent (%) character.
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 |
Schedule to attach to job. This can be more than one schedule.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Schedule ID to attach to job. This can be more than one schedule ID.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Sets the status of the job to disabled. By default a job is enabled.
Alias | |
Required | False |
Pipeline | false |
Default Value | False |
The description of the job.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The identification number of the first step to execute for the job.
Alias | |
Required | False |
Pipeline | false |
Default Value | 0 |
The category of the job.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The name of the login that owns the job.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Specifies when to place an entry in the Microsoft Windows application log for this job.
Allowed values 0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always"
The text value can either be lowercase, uppercase or something in between as long as the text is correct.
Alias | |
Required | False |
Pipeline | false |
Default Value | |
Accepted Values | 0,Never,1,OnSuccess,2,OnFailure,3,Always |
Specifies when to send an e-mail upon the completion of this job.
Allowed values 0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always"
The text value can either be lowercase, uppercase or something in between as long as the text is correct.
Alias | |
Required | False |
Pipeline | false |
Default Value | |
Accepted Values | 0,Never,1,OnSuccess,2,OnFailure,3,Always |
Specifies when to send a page upon the completion of this job.
Allowed values 0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always"
The text value can either be lowercase, uppercase or something in between as long as the text is correct.
Alias | |
Required | False |
Pipeline | false |
Default Value | |
Accepted Values | 0,Never,1,OnSuccess,2,OnFailure,3,Always,0,Never,1,OnSuccess,2,OnFailure,3,Always |
The e-mail name of the operator to whom the e-mail is sent when EmailLevel is reached.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The name of the operator to whom the network message is sent.
Alias | |
Required | False |
Pipeline | false |
Default Value |
The name of the operator to whom a page is sent.
Alias | |
Required | False |
Pipeline | false |
Default Value |
Specifies when to delete the job.
Allowed values 0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always"
The text value can either be lowercase, uppercase or something in between as long as the text is correct.
Alias | |
Required | False |
Pipeline | false |
Default Value | |
Accepted Values | 0,Never,1,OnSuccess,2,OnFailure,3,Always |
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 |