< Back
New-DbaAgentJob
Post
NAME New-DbaAgentJob
SYNOPSIS
New-DbaAgentJob creates a new job
SYNTAX
New-DbaAgentJob [-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [[-SqlCredential]
<Pscredential>] [-Job] <String> [[-Schedule] <System.Object[]>] [[-ScheduleId] <System.Int32[]>] [-Disabled
<Switch>] [[-Description] <String>] [[-StartStepId] <Int>] [[-Category] <String>] [[-OwnerLogin] <String>]
[[-EventLogLevel] <System.Object>] [[-EmailLevel] <System.Object>] [[-PageLevel] <System.Object>]
[[-EmailOperator] <String>] [[-NetsendOperator] <String>] [[-PageOperator] <String>] [[-DeleteLevel]
<System.Object>] [-Force <Switch>] [-EnableException <Switch>] [<CommonParameters>]
DESCRIPTION
New-DbaAgentJob makes is possible to create a job in the SQL Server Agent.
It returns an array of the job(s) created
PARAMETERS
-Category [<String>]
The category of the job.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DeleteLevel [<System.Object>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Description [<String>]
The description of the job.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Disabled [<Switch>]
Sets the status of the job to disabled. By default a job is enabled.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EmailLevel [<System.Object>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EmailOperator [<String>]
The e-mail name of the operator to whom the e-mail is sent when EmailLevel is reached.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EnableException [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EventLogLevel [<System.Object>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Force [<Switch>]
The force parameter will ignore some errors in the parameters and assume defaults.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Job [<String>]
The name of the job. The name must be unique and cannot contain the percent (%) character.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NetsendOperator [<String>]
The name of the operator to whom the network message is sent.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-OwnerLogin [<String>]
The name of the login that owns the job.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-PageLevel [<System.Object>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-PageOperator [<String>]
The name of the operator to whom a page is sent.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Schedule [<System.Object[]>]
Schedule to attach to job. This can be more than one schedule.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ScheduleId [<System.Int32[]>]
Schedule ID to attach to job. This can be more than one schedule ID.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlCredential [<Pscredential>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
The target SQL Server instance or instances. You must have sysadmin access and server version must be SQL
Server version 2000 or greater.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-StartStepId [<Int>]
The identification number of the first step to execute for the job.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
Tags: Agent, Job, JobStep
Author: Sander Stad (@sqlstad), sqlstad.nl
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Description 'Just another job'
Creates a job with the name "Job1" and a small description
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Disabled
Creates the job but sets it to disabled
-------------------------- EXAMPLE 3 --------------------------
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
-------------------------- EXAMPLE 4 --------------------------
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
-------------------------- EXAMPLE 5 --------------------------
PS C:\\>New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Description 'Just another job' -Whatif
Doesn't create the job but shows what would happen.
-------------------------- EXAMPLE 6 --------------------------
PS C:\\>New-DbaAgentJob -SqlInstance sql1, sql2, sql3 -Job 'Job One'
Creates a job with the name "Job One" on multiple servers
-------------------------- EXAMPLE 7 --------------------------
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
RELATED LINKS
https://dbatools.io/New-DbaAgentJob
SYNOPSIS
New-DbaAgentJob creates a new job
SYNTAX
New-DbaAgentJob [-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [[-SqlCredential]
<Pscredential>] [-Job] <String> [[-Schedule] <System.Object[]>] [[-ScheduleId] <System.Int32[]>] [-Disabled
<Switch>] [[-Description] <String>] [[-StartStepId] <Int>] [[-Category] <String>] [[-OwnerLogin] <String>]
[[-EventLogLevel] <System.Object>] [[-EmailLevel] <System.Object>] [[-PageLevel] <System.Object>]
[[-EmailOperator] <String>] [[-NetsendOperator] <String>] [[-PageOperator] <String>] [[-DeleteLevel]
<System.Object>] [-Force <Switch>] [-EnableException <Switch>] [<CommonParameters>]
DESCRIPTION
New-DbaAgentJob makes is possible to create a job in the SQL Server Agent.
It returns an array of the job(s) created
PARAMETERS
-Category [<String>]
The category of the job.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-DeleteLevel [<System.Object>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Description [<String>]
The description of the job.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Disabled [<Switch>]
Sets the status of the job to disabled. By default a job is enabled.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EmailLevel [<System.Object>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EmailOperator [<String>]
The e-mail name of the operator to whom the e-mail is sent when EmailLevel is reached.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EnableException [<Switch>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-EventLogLevel [<System.Object>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Force [<Switch>]
The force parameter will ignore some errors in the parameters and assume defaults.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Job [<String>]
The name of the job. The name must be unique and cannot contain the percent (%) character.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-NetsendOperator [<String>]
The name of the operator to whom the network message is sent.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-OwnerLogin [<String>]
The name of the login that owns the job.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-PageLevel [<System.Object>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-PageOperator [<String>]
The name of the operator to whom a page is sent.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-Schedule [<System.Object[]>]
Schedule to attach to job. This can be more than one schedule.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-ScheduleId [<System.Int32[]>]
Schedule ID to attach to job. This can be more than one schedule ID.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlCredential [<Pscredential>]
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.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]
The target SQL Server instance or instances. You must have sysadmin access and server version must be SQL
Server version 2000 or greater.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
-StartStepId [<Int>]
The identification number of the first step to execute for the job.
Required? false
Position? named
Default value
Accept pipeline input? False
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
Tags: Agent, Job, JobStep
Author: Sander Stad (@sqlstad), sqlstad.nl
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Description 'Just another job'
Creates a job with the name "Job1" and a small description
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Disabled
Creates the job but sets it to disabled
-------------------------- EXAMPLE 3 --------------------------
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
-------------------------- EXAMPLE 4 --------------------------
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
-------------------------- EXAMPLE 5 --------------------------
PS C:\\>New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Description 'Just another job' -Whatif
Doesn't create the job but shows what would happen.
-------------------------- EXAMPLE 6 --------------------------
PS C:\\>New-DbaAgentJob -SqlInstance sql1, sql2, sql3 -Job 'Job One'
Creates a job with the name "Job One" on multiple servers
-------------------------- EXAMPLE 7 --------------------------
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
RELATED LINKS
https://dbatools.io/New-DbaAgentJob