functions/New-DbaAgentJob.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
function 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 .PARAMETER SqlInstance SQL Server instance. You must have sysadmin access and server version must be SQL Server version 2000 or greater. .PARAMETER SqlCredential Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use: $scred = Get-Credential, then pass $scred object to the -SqlCredential parameter. To connect as a different Windows user, run PowerShell as that user. .PARAMETER Job The name of the job. The name must be unique and cannot contain the percent (%) character. .PARAMETER Schedule Schedule to attach to job. This can be more than one schedule. .PARAMETER ScheduleId Schedule ID to attach to job. This can be more than one schedule ID. .PARAMETER Disabled Sets the status of the job to disabled. By default a job is enabled. .PARAMETER Description The description of the job. .PARAMETER StartStepId The identification number of the first step to execute for the job. .PARAMETER Category The category of the job. .PARAMETER CategoryId A language-independent mechanism for specifying a job category. .PARAMETER OwnerLogin The name of the login that owns the job. .PARAMETER 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 van either be lowercase, uppercase or something in between as long as the text is correct. .PARAMETER 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 van either be lowercase, uppercase or something in between as long as the text is correct. .PARAMETER NetsendLevel Specifies when to send a network message upon the completion of this job. Allowed values 0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always" The text value van either be lowercase, uppercase or something in between as long as the text is correct. .PARAMETER 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 van either be lowercase, uppercase or something in between as long as the text is correct. .PARAMETER EmailOperator The e-mail name of the operator to whom the e-mail is sent when EmailLevel is reached. .PARAMETER NetsendOperator The name of the operator to whom the network message is sent. .PARAMETER PageOperator The name of the operator to whom a page is sent. .PARAMETER DeleteLevel Specifies when to delete the job. Allowed values 0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always" The text value van either be lowercase, uppercase or something in between as long as the text is correct. .PARAMETER Force The force parameter will ignore some errors in the parameters and assume defaults. .PARAMETER WhatIf Shows what would happen if the command were to run. No actions are actually performed. .PARAMETER Confirm Prompts you for confirmation before executing any changing operations within the command. .PARAMETER Silent Use this switch to disable any kind of verbose messages .NOTES Original Author: Sander Stad (@sqlstad, sqlstad.nl) Tags: Agent, Job, Job Step Website: https://dbatools.io Copyright: (C) Chrissy LeMaire, clemaire@gmail.com License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0 .LINK https://dbatools.io/New-DbaAgentJob .EXAMPLE New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Description 'Just another job' Creates a job with the name "Job1" and a small description .EXAMPLE New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Disabled Creates the job but sets it to disabled .EXAMPLE 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 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 New-DbaAgentJob -SqlInstance sql1 -Job 'Job One' -Description 'Just another job' -Whatif Doesn't create the job but shows what would happen. .EXAMPLE New-DbaAgentJob -SqlInstance sql1, sql2, sql3 -Job 'Job One' Creates a job with the name "Job One" on multiple servers .EXAMPLE "sql1", "sql2", "sql3" | New-DbaAgentJob -Job 'Job One' Creates a job with the name "Job One" on multiple servers using the pipe line #> [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "Low")] param ( [parameter(Mandatory = $true, ValueFromPipeline = $true)] [Alias("ServerInstance", "SqlServer")] [DbaInstanceParameter[]]$SqlInstance, [PSCredential]$SqlCredential, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$Job, [object[]]$Schedule, [int[]]$ScheduleId, [switch]$Disabled, [string]$Description, [int]$StartStepId, [string]$Category, [int]$CategoryId, [string]$OwnerLogin, [ValidateSet(0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always")] [object]$EventLogLevel, [ValidateSet(0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always")] [object]$EmailLevel, [ValidateSet(0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always")] [Parameter()] [ValidateSet(0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always")] [object]$PageLevel, [string]$EmailOperator, [string]$NetsendOperator, [string]$PageOperator, [ValidateSet(0, "Never", 1, "OnSuccess", 2, "OnFailure", 3, "Always")] [object]$DeleteLevel, [switch]$Force, [switch]$Silent ) begin { # Check of the event log level is of type string and set the integer value if ($EventLogLevel -notin 1, 2, 3) { $EventLogLevel = switch ($EventLogLevel) { "Never" { 0 } "OnSuccess" { 1 } "OnFailure" { 2 } "Always" { 3 } default { 0 } } } # Check of the email level is of type string and set the integer value if ($EmailLevel -notin 1, 2, 3) { $EmailLevel = switch ($EmailLevel) { "Never" { 0 } "OnSuccess" { 1 } "OnFailure" { 2 } "Always" { 3 } default { 0 } } } # Check of the net send level is of type string and set the integer value if ($NetsendLevel -notin 1, 2, 3) { $NetsendLevel = switch ($NetsendLevel) { "Never" { 0 } "OnSuccess" { 1 } "OnFailure" { 2 } "Always" { 3 } default { 0 } } } # Check of the page level is of type string and set the integer value if ($PageLevel -notin 1, 2, 3) { $PageLevel = switch ($PageLevel) { "Never" { 0 } "OnSuccess" { 1 } "OnFailure" { 2 } "Always" { 3 } default { 0 } } } # Check of the delete level is of type string and set the integer value if ($DeleteLevel -notin 1, 2, 3) { $DeleteLevel = switch ($DeleteLevel) { "Never" { 0 } "OnSuccess" { 1 } "OnFailure" { 2 } "Always" { 3 } default { 0 } } } # Check the e-mail operator name if (($EmailLevel -ge 1) -and (-not $EmailOperator)) { Stop-Function -Message "Please set the e-mail operator when the e-mail level parameter is set." -Target $sqlinstance return } # Check the e-mail operator name if (($NetsendLevel -ge 1) -and (-not $NetsendOperator)) { Stop-Function -Message "Please set the netsend operator when the netsend level parameter is set." -Target $sqlinstance return } # Check the e-mail operator name if (($PageLevel -ge 1) -and (-not $PageOperator)) { Stop-Function -Message "Please set the page operator when the page level parameter is set." -Target $sqlinstance return } } process { if (Test-FunctionInterrupt) { return } foreach ($instance in $sqlinstance) { # Try connecting to the instance Write-Message -Message "Attempting to connect to $instance" -Level Verbose try { $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential } catch { Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue } # Check if the job already exists if (-not $Force -and ($server.JobServer.Jobs.Name -contains $Job)) { Stop-Function -Message "Job $Job already exists on $instance" -Target $instance -Continue } elseif ($Force -and ($server.JobServer.Jobs.Name -contains $Job)) { Write-Message -Message "Job $Job already exists on $instance. Removing.." -Level Verbose if ($PSCmdlet.ShouldProcess($instance, "Removing the job the job $instance")) { try { Remove-DbaAgentJob -SqlInstance $instance -Job $Job -Silent } catch { Stop-Function -Message "Couldn't remove job $Job from $instance" -Target $instance -Continue -InnerErrorRecord $_ } } } # Create the job object try { $smoJob = New-Object Microsoft.SqlServer.Management.Smo.Agent.Job($server.JobServer, $Job) } catch { Stop-Function -Message "Something went wrong creating the job. `n$($_.Exception.Message)" -Target $Job -Continue -InnerErrorRecord $_ } #region job options # Settings the options for the job if ($Disabled) { Write-Message -Message "Setting job to disabled" -Level Verbose $smoJob.IsEnabled = $false } else { Write-Message -Message "Setting job to enabled" -Level Verbose $smoJob.IsEnabled = $true } if ($Description.Length -ge 1) { Write-Message -Message "Setting job description" -Level Verbose $smoJob.Description = $Description } if ($StartStepId -ge 1) { Write-Message -Message "Setting job start step id" -Level Verbose $smoJob.StartStepID = $StartStepId } if ($Category.Length -ge 1) { Write-Message -Message "Setting job category" -Level Verbose $smoJob.Category = $Category } if ($CategoryId -ge 1) { Write-Message -Message "Setting job category id" -Level Verbose $smoJob.CategoryID = $CategoryId } if ($OwnerLogin.Length -ge 1) { # Check if the login name is present on the instance if ($server.Logins.Name -contains $OwnerLogin) { Write-Message -Message "Setting job owner login name to $OwnerLogin" -Level Verbose $smoJob.OwnerLoginName = $OwnerLogin } else { Stop-Function -Message "The owner $OwnerLogin does not exist on instance $instance" -Target $Job -Continue } } if ($EventLogLevel -ge 0) { Write-Message -Message "Setting job event log level" -Level Verbose $smoJob.EventLogLevel = $EventLogLevel } if ($EmailOperator) { if ($EmailLevel -ge 1) { # Check if the operator name is present if ($server.JobServer.Operators.Name -contains $EmailOperator) { Write-Message -Message "Setting job e-mail level" -Level Verbose $smoJob.EmailLevel = $EmailLevel Write-Message -Message "Setting job e-mail operator" -Level Verbose $smoJob.OperatorToEmail = $EmailOperator } else { Stop-Function -Message "The e-mail operator name $EmailOperator does not exist on instance $instance. Exiting.." -Target $Job -Continue } } else { Stop-Function -Message "Invalid combination of e-mail operator name $EmailOperator and email level $EmailLevel. Not setting the notification." -Target $Job -Continue } } if ($NetsendOperator) { if ($NetsendLevel -ge 1) { # Check if the operator name is present if ($server.JobServer.Operators.Name -contains $NetsendOperator) { Write-Message -Message "Setting job netsend level" -Level Verbose $smoJob.NetSendLevel = $NetsendLevel Write-Message -Message "Setting job netsend operator" -Level Verbose $smoJob.OperatorToNetSend = $NetsendOperator } else { Stop-Function -Message "The netsend operator name $NetsendOperator does not exist on instance $instance. Exiting.." -Target $Job -Continue } } else { Write-Message -Message "Invalid combination of netsend operator name $NetsendOperator and netsend level $NetsendLevel. Not setting the notification." } } if ($PageOperator) { if ($PageLevel -ge 1) { # Check if the operator name is present if ($server.JobServer.Operators.Name -contains $PageOperator) { Write-Message -Message "Setting job pager level" -Level Verbose $smoJob.PageLevel = $PageLevel Write-Message -Message "Setting job pager operator" -Level Verbose $smoJob.OperatorToPage = $PageOperator } else { Stop-Function -Message "The page operator name $PageOperator does not exist on instance $instance. Exiting.." -Target $Job -Continue } } else { Write-Message -Message "Invalid combination of page operator name $PageOperator and page level $PageLevel. Not setting the notification." -Level Warning } } if ($DeleteLevel -ge 0) { Write-Message -Message "Setting job delete level" -Level Verbose $smoJob.DeleteLevel = $DeleteLevel } #endregion job options # Execute if ($PSCmdlet.ShouldProcess($instance, "Creating the job on $instance")) { try { Write-Message -Message "Creating the job" -Level Verbose # Create the job $smoJob.Create() Write-Message -Message "Job created with UID $($smoJob.JobID)" -Level Verbose # Make sure the target is set for the job Write-Message -Message "Applying the target (local) to job $Job" -Level Verbose $smoJob.ApplyToTargetServer("(local)") # If a schedule needs to be attached if ($Schedule) { Set-DbaAgentJob -SqlInstance $instance -Job $smoJob -Schedule $Schedule -SqlCredential $SqlCredential } if ($ScheduleId) { Set-DbaAgentJob -SqlInstance $instance -Job $smoJob -ScheduleId $ScheduleId -SqlCredential $SqlCredential } } catch { Stop-Function -Message "Something went wrong creating the job. `n$($_.Exception.Message)" -Continue -InnerErrorRecord $_ } } # Return the job $smoJob } } end { Write-Message -Message "Finished creating job(s)." -Level Verbose } } |