Agent Job Resource
Synopsis
Manages SQL Server Agent jobs, including job creation, notification settings, and monitoring of job execution status.
Type
Capabilities
- Get
- Set
- Delete
- Export
Properties
Connection properties
serverInstance
SQL Server instance name.
connectUsername
Username for SQL authentication.
connectPassword
Password for SQL authentication.
Job properties
name
Name of the Agent job.
description
Description of the job.
isEnabled
Whether the job is enabled.
category
Category of the job.
ownerLoginName
Login name of the job owner.
startStepId
Step ID at which execution starts. Minimum value is 1.
Notification properties
emailLevel
When to send email. Accepts Never, OnSuccess, OnFailure, or Always.
operatorToEmail
Operator to email.
pageLevel
When to page. Accepts Never, OnSuccess, OnFailure, or Always.
operatorToPage
Operator to page.
netSendLevel
When to net send. Accepts Never, OnSuccess, OnFailure, or Always.
operatorToNetSend
Operator for net send.
eventLogLevel
When to write to the event log. Accepts Never, OnSuccess, OnFailure, or
Always.
deleteLevel
When to delete the job. Accepts Never, OnSuccess, OnFailure, or Always.
Read-only properties
jobId
Unique identifier of the job.
dateCreated
Date the job was created.
dateLastModified
Date the job was last modified.
lastRunDate
Date of the last run.
lastRunOutcome
Outcome of the last run. Returns Failed, Succeeded, Retry, or Cancelled.
nextRunDate
Date of the next scheduled run.
currentRunStatus
Current status. Returns Idle, Executing, WaitingForWorkerThread, or
BetweenRetries.
currentRunStep
Current step being executed.
currentRunRetryAttempt
Current retry attempt number.
hasStep
Whether the job has steps defined.
hasSchedule
Whether the job has schedules.
versionNumber
Job version number.
DSC properties
_exist
Whether the job should exist.
Examples
Example 1 — Get a job
Example 2 — Create a job
resource_input=$(cat <<'EOF'
serverInstance: .
name: NightlyMaintenance
description: Nightly index rebuild and statistics update
isEnabled: true
category: Database Maintenance
emailLevel: OnFailure
operatorToEmail: DBA Team
EOF
)
dsc resource set -r OpenDsc.SqlServer/AgentJob --input "$resource_input"
Example 3 — Configuration document
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Nightly maintenance job
type: OpenDsc.SqlServer/AgentJob
properties:
serverInstance: "."
name: NightlyMaintenance
description: Nightly index rebuild and statistics update
isEnabled: true
category: Database Maintenance
emailLevel: OnFailure
operatorToEmail: DBA Team
Exit codes
| Code | Description |
|---|---|
| 0 | Success |
| 1 | Error |
| 2 | Invalid JSON |
| 3 | Invalid argument |
| 4 | Unauthorized access |
| 5 | Invalid operation |