functions/Copy-DbaAgentCategory.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 Copy-DbaAgentCategory { <# .SYNOPSIS Copy-DbaAgentCategory migrates SQL Agent categories from one SQL Server to another. This is similar to sp_add_category. https://msdn.microsoft.com/en-us/library/ms181597.aspx .DESCRIPTION By default, all SQL Agent categories for Jobs, Operators and Alerts are copied. The -OperatorCategories parameter is auto-populated for command-line completion and can be used to copy only specific operator categories. The -AgentCategories parameter is auto-populated for command-line completion and can be used to copy only specific agent categories. The -JobCategories parameter is auto-populated for command-line completion and can be used to copy only specific job categories. If the category already exists on the destination, it will be skipped unless -Force is used. .PARAMETER Source Source SQL Server. You must have sysadmin access and server version must be SQL Server version 2000 or higher. .PARAMETER SourceSqlCredential Allows you to login to servers using SQL Logins instead of Windows Authentication (AKA Integrated or Trusted). To use: $scred = Get-Credential, then pass $scred object to the -SourceSqlCredential parameter. Windows Authentication will be used if SourceSqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials. To connect as a different Windows user, run PowerShell as that user. .PARAMETER Destination Destination SQL Server. You must have sysadmin access and the server must be SQL Server 2000 or higher. .PARAMETER DestinationSqlCredential Allows you to login to servers using SQL Logins instead of Windows Authentication (AKA Integrated or Trusted). To use: $dcred = Get-Credential, then pass this $dcred to the -DestinationSqlCredential parameter. Windows Authentication will be used if DestinationSqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials. To connect as a different Windows user, run PowerShell as that user. .PARAMETER CategoryType Specifies the Category Type to migrate. Valid options are "Job", "Alert" and "Operator". When CategoryType is specified, all categories from the selected type will be migrated. For granular migrations, use the three parameters below. .PARAMETER OperatorCategory This parameter is auto-populated for command-line completion and can be used to copy only specific operator categories. .PARAMETER AgentCategory This parameter is auto-populated for command-line completion and can be used to copy only specific agent categories. .PARAMETER JobCategory This parameter is auto-populated for command-line completion and can be used to copy only specific job categories. .PARAMETER WhatIf If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run. .PARAMETER Confirm If this switch is enabled, you will be prompted for confirmation before executing any operations that change state. .PARAMETER Force If this switch is enabled, the Category will be dropped and recreated on Destination. .PARAMETER Silent If this switch is enabled, the internal messaging functions will be silenced. .NOTES Tags: Migration, Agent Author: Chrissy LeMaire (@cl), netnerds.net Requires: sysadmin access on SQL Servers 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/Copy-DbaAgentCategory .EXAMPLE Copy-DbaAgentCategory -Source sqlserver2014a -Destination sqlcluster Copies all operator categories from sqlserver2014a to sqlcluster using Windows authentication. If operator categories with the same name exist on sqlcluster, they will be skipped. .EXAMPLE Copy-DbaAgentCategory -Source sqlserver2014a -Destination sqlcluster -OperatorCategory PSOperator -SourceSqlCredential $cred -Force Copies a single operator category, the PSOperator operator category from sqlserver2014a to sqlcluster using SQL credentials to authenticate to sqlserver2014a and Windows credentials for sqlcluster. If a operator category with the same name exists on sqlcluster, it will be dropped and recreated because -Force was used. .EXAMPLE Copy-DbaAgentCategory -Source sqlserver2014a -Destination sqlcluster -WhatIf -Force Shows what would happen if the command were executed using force. #> [CmdletBinding(DefaultParameterSetName = "Default", SupportsShouldprocess = $true)] param ( [parameter(Mandatory = $true)] [DbaInstanceParameter]$Source, [PSCredential] $SourceSqlCredential, [parameter(Mandatory = $true)] [DbaInstanceParameter]$Destination, [PSCredential] $DestinationSqlCredential, [Parameter(ParameterSetName = 'SpecificAlerts')] [ValidateSet('Job', 'Alert', 'Operator')] [string[]]$CategoryType, [switch]$Force, [switch]$Silent ) begin { Function Copy-JobCategory { <# .SYNOPSIS Copy-JobCategory migrates job categories from one SQL Server to another. .DESCRIPTION By default, all job categories are copied. The -JobCategories parameter is auto-populated for command-line completion and can be used to copy only specific job categories. If the associated credential for the category does not exist on the destination, it will be skipped. If the job category already exists on the destination, it will be skipped unless -Force is used. #> param ( [string[]]$JobCategories ) process { $serverJobCategories = $sourceServer.JobServer.JobCategories | Where-Object ID -ge 100 $destJobCategories = $destServer.JobServer.JobCategories | Where-Object ID -ge 100 foreach ($jobCategory in $serverJobCategories) { $categoryName = $jobCategory.Name $copyJobCategoryStatus = [pscustomobject]@{ SourceServer = $sourceServer.Name DestinationServer = $destServer.Name Name = $categoryName Status = $null DateTime = [Sqlcollaborative.Dbatools.Utility.DbaDateTime](Get-Date) } if ($JobCategories.Count -gt 0 -and $JobCategories -notcontains $categoryName) { continue } if ($destJobCategories.Name -contains $jobCategory.name) { if ($force -eq $false) { $copyJobCategoryStatus.Status = "Skipped" $copyJobCategoryStatus Write-Message -Level Warning -Message "Job category $categoryName exists at destination. Use -Force to drop and migrate." continue } else { if ($Pscmdlet.ShouldProcess($destination, "Dropping job category $categoryName and recreating")) { try { Write-Message -Level Verbose -Message "Dropping Job category $categoryName" $destServer.JobServer.JobCategories[$categoryName].Drop() } catch { $copyJobCategoryStatus.Status = "Failed" $copyJobCategoryStatus Stop-Function -Message "Issue dropping job category" -Target $categoryName -InnerErrorRecord $_ -Continue } } } } if ($Pscmdlet.ShouldProcess($destination, "Creating Job category $categoryName")) { try { Write-Message -Level Verbose -Message "Copying Job category $categoryName" $sql = $jobCategory.Script() | Out-String Write-Message -Level Debug -Message $sql $destServer.Query($sql) $copyJobCategoryStatus.Status = "Successful" $copyJobCategoryStatus } catch { $copyJobCategoryStatus.Status = "Failed" $copyJobCategoryStatus Stop-Function -Message "Issue copying job category" -Target $categoryName -InnerErrorRecord $_ } } } } } function Copy-OperatorCategory { <# .SYNOPSIS Copy-OperatorCategory migrates operator categories from one SQL Server to another. .DESCRIPTION By default, all operator categories are copied. The -OperatorCategories parameter is auto-populated for command-line completion and can be used to copy only specific operator categories. If the associated credential for the category does not exist on the destination, it will be skipped. If the operator category already exists on the destination, it will be skipped unless -Force is used. #> [CmdletBinding(DefaultParameterSetName = "Default", SupportsShouldprocess = $true)] param ( [string[]]$OperatorCategories ) process { $serverOperatorCategories = $sourceServer.JobServer.OperatorCategories | Where-Object ID -ge 100 $destOperatorCategories = $destServer.JobServer.OperatorCategories | Where-Object ID -ge 100 foreach ($operatorCategory in $serverOperatorCategories) { $categoryName = $operatorCategory.Name $copyOperatorCategoryStatus = [pscustomobject]@{ SourceServer = $sourceServer.Name DestinationServer = $destServer.Name Name = $categoryName Status = $null DateTime = [Sqlcollaborative.Dbatools.Utility.DbaDateTime](Get-Date) } if ($operatorCategories.Count -gt 0 -and $operatorCategories -notcontains $categoryName) { continue } if ($destOperatorCategories.Name -contains $operatorCategory.Name) { if ($force -eq $false) { $copyOperatorCategoryStatus.Status = "Skipped" $copyOperatorCategoryStatus Write-Message -Level Warning -Message "Operator category $categoryName exists at destination. Use -Force to drop and migrate." continue } else { if ($Pscmdlet.ShouldProcess($destination, "Dropping operator category $categoryName and recreating")) { try { Write-Message -Level Verbose -Message "Dropping Operator category $categoryName" $destServer.JobServer.OperatorCategories[$categoryName].Drop() Write-Message -Level Verbose -Message "Copying Operator category $categoryName" $sql = $operatorCategory.Script() | Out-String Write-Message -Level Debug -Message $sql $destServer.Query($sql) } catch { $copyOperatorCategoryStatus.Status = "Failed" $copyOperatorCategoryStatus Stop-Function -Message "Issue dropping operator category" -Target $categoryName -InnerErrorRecord $_ } } } } else { if ($Pscmdlet.ShouldProcess($destination, "Creating Operator category $categoryName")) { try { Write-Message -Level Verbose -Message "Copying Operator category $categoryName" $sql = $operatorCategory.Script() | Out-String Write-Message -Level Debug -Message $sql $destServer.Query($sql) $copyOperatorCategoryStatus.Status = "Successful" $copyOperatorCategoryStatus } catch { $copyOperatorCategoryStatus.Status = "Failed" $copyOperatorCategoryStatus Stop-Function -Message "Issue copying operator category" -Target $categoryName -InnerErrorRecord $_ } } } } } } function Copy-AlertCategory { <# .SYNOPSIS Copy-AlertCategory migrates alert categories from one SQL Server to another. .DESCRIPTION By default, all alert categories are copied. The -AlertCategories parameter is auto-populated for command-line completion and can be used to copy only specific alert categories. If the associated credential for the category does not exist on the destination, it will be skipped. If the alert category already exists on the destination, it will be skipped unless -Force is used. #> [CmdletBinding(DefaultParameterSetName = "Default", SupportsShouldprocess = $true)] param ( [string[]]$AlertCategories ) process { if ($sourceServer.VersionMajor -lt 9 -or $destServer.VersionMajor -lt 9) { throw "Server AlertCategories are only supported in SQL Server 2005 and above. Quitting." } $serverAlertCategories = $sourceServer.JobServer.AlertCategories | Where-Object ID -ge 100 $destAlertCategories = $destServer.JobServer.AlertCategories | Where-Object ID -ge 100 foreach ($alertCategory in $serverAlertCategories) { $categoryName = $alertCategory.Name $copyAlertCategoryStatus = [pscustomobject]@{ SourceServer = $sourceServer.Name DestinationServer = $destServer.Name Name = $categoryName Status = $null DateTime = [Sqlcollaborative.Dbatools.Utility.DbaDateTime](Get-Date) } if ($alertCategories.Length -gt 0 -and $alertCategories -notcontains $categoryName) { continue } if ($destAlertCategories.Name -contains $alertCategory.name) { if ($force -eq $false) { $copyAlertCategoryStatus.Status = "Skipped" $copyAlertCategoryStatus Write-Message -Level Warning -Message "Alert category $categoryName exists at destination. Use -Force to drop and migrate." continue } else { if ($Pscmdlet.ShouldProcess($destination, "Dropping alert category $categoryName and recreating")) { try { Write-Message -Level Verbose -Message "Dropping Alert category $categoryName" $destServer.JobServer.AlertCategories[$categoryName].Drop() Write-Message -Level Verbose -Message "Copying Alert category $categoryName" $sql = $alertcategory.Script() | Out-String Write-Verbose $sql $destServer.Query($sql) } catch { $copyAlertCategoryStatus.Status = "Failed" $copyAlertCategoryStatus Stop-Function -Message "Issue dropping alert category" -Target $categoryName -InnerErrorRecord $_ } } } } else { if ($Pscmdlet.ShouldProcess($destination, "Creating Alert category $categoryName")) { try { Write-Message -Level Verbose -Message "Copying Alert category $categoryName" $sql = $alertCategory.Script() | Out-String Write-Message -Level Debug -Message $sql $destServer.Query($sql) $copyAlertCategoryStatus.Status = "Successful" $copyAlertCategoryStatus } catch { $copyAlertCategoryStatus.Status = "Failed" $copyAlertCategoryStatus Stop-Function -Message "Issue creating alert category" -Target $categoryName -InnerErrorRecord $_ } } } } } } $sourceServer = Connect-SqlInstance -SqlInstance $Source -SqlCredential $SourceSqlCredential $destServer = Connect-SqlInstance -SqlInstance $Destination -SqlCredential $DestinationSqlCredential $source = $sourceServer.DomainInstanceName $destination = $destServer.DomainInstanceName } process { if ($CategoryType.count -gt 0) { switch ($CategoryType) { "Job" { Copy-JobCategory } "Alert" { Copy-AlertCategory } "Operator" { Copy-OperatorCategory } } return } if (($OperatorCategory.Count + $AlertCategory.Count + $jobCategory.Count) -gt 0) { if ($OperatorCategory.Count -gt 0) { Copy-OperatorCategory -OperatorCategories $OperatorCategory } if ($AlertCategory.Count -gt 0) { Copy-AlertCategory -AlertCategories $AlertCategory } if ($jobCategory.Count -gt 0) { Copy-JobCategory -JobCategories $jobCategory } return } Copy-OperatorCategory Copy-AlertCategory Copy-JobCategory } end { Test-DbaDeprecation -DeprecatedOn "1.0.0" -Silent:$false -Alias Copy-SqlAgentCategory } } |