commands

^

New-DbaAgentJob

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.

Synopsis

New-DbaAgentJob creates a new job

Description

New-DbaAgentJob makes is possible to create a job in the SQL Server Agent. It returns an array of the job(s) created

Syntax

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>]

 

Examples

 

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

Required Parameters

-SqlInstance

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
-Job

The name of the job. The name must be unique and cannot contain the percent (%) character.

Alias
Required True
Pipeline false
Default Value

Optional Parameters

-SqlCredential

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

Schedule to attach to job. This can be more than one schedule.

Alias
Required False
Pipeline false
Default Value
-ScheduleId

Schedule ID to attach to job. This can be more than one schedule ID.

Alias
Required False
Pipeline false
Default Value
-Disabled

Sets the status of the job to disabled. By default a job is enabled.

Alias
Required False
Pipeline false
Default Value False
-Description

The description of the job.

Alias
Required False
Pipeline false
Default Value
-StartStepId

The identification number of the first step to execute for the job.

Alias
Required False
Pipeline false
Default Value 0
-Category

The category of the job.

Alias
Required False
Pipeline false
Default Value
-OwnerLogin

The name of the login that owns the job.

Alias
Required False
Pipeline false
Default Value
-EventLogLevel

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
-EmailLevel

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
-PageLevel

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
-EmailOperator

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
-NetsendOperator

The name of the operator to whom the network message is sent.

Alias
Required False
Pipeline false
Default Value
-PageOperator

The name of the operator to whom a page is sent.

Alias
Required False
Pipeline false
Default Value
-DeleteLevel

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
-Force

The force parameter will ignore some errors in the parameters and assume defaults.

Alias
Required False
Pipeline false
Default Value False
-EnableException

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
-WhatIf

Shows what would happen if the command were to run. No actions are actually performed.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

Prompts you for confirmation before executing any changing operations within the command.

Alias cf
Required False
Pipeline false
Default Value