Moving Tasks with Copy-SqlJob

More dbatools experimentation for me. This is a great set of Powershell cmdlets that solve simple, handy problems. In this post, I want to look at Copy-SqlJob, which will allow me to easily move jobs from one instance to another.

The Job Subsystem

Jobs in SQL Server are part of the SQL Agent subsystem, which means they’re a part of SQL Server, but not really. Maybe the one part of the jobs that is really complex is getting the code for the job. You either need to use an SMO interface and script out the job, or you can decode the tables in msdb. Neither of which is handy.

We do need to move jobs, though. Perhaps the most common places I need to copy jobs around is for a DR (disaster recovery) situation. Clustering will move jobs from node to node as it covers the instance, but all the other technologies (AGs, Log Shipping, Mirroring), all of these only work with databases. So items like jobs need to be manually moved.

That’s not good.

Over time, we’ll edit, add, change jobs, but I’ve never seen anyone do a great job of actually ensuring the DR systems are up to date with jobs, logins, etc. all the time. There’s too much of a manual process.

That can change with Copy-SqlJob. I can now not only copy particular jobs between an instance, I can use a –Force command to ensure the copy takes place. This means that I can easily ensure the most recent version of my job is copied over. Or I can just skip this, and if the job exists, it’s not copied.

Running the Cmdlet

It’s easy to use this cmdlet. It can use Windows Auth or SQL Auth. It’s up to you. The documentation leaves something to be desired, so you’ll need to use the PowerShell Get-Help to learn what parameters exist.

For a simple copy, I’ll move all jobs from one instance to another. Here are the jobs on my primary SQL Server 2016 instance.

2016-11-22 09_11_09-SQLQuery1.sql - localhost_SQL2016.DBAAdmin (PLATO_Steve (66))_ - Microsoft SQL S

These are administrative, backup jobs and index jobs from Ola Hallengren. I don’t have these on my QA instance, which isn’t good.

2016-11-22 09_11_25-SQLQuery1.sql - localhost_SQL2016.DBAAdmin (PLATO_Steve (66))_ - Microsoft SQL S

Suppose this is a new instance and I want to add them? I can use Copy-SqlJob, but I want to know what will happen. I’ll use the –Source and –Destination parameters and then include a –Whatif. This gives me a list of jobs that will be copied, and those that won’t be.

2016-11-22 09_21_13-powershell

If I use the –Force parameter, you’ll see that all jobs are copied.

2016-11-22 09_22_25-powershell

Well, not copied, but they will be.

I could limit this to specific jobs. For example, let me just copy over these jobs

  • DatabaseBackup – SYSTEM_DATABASES – FULL
  • DatabaseBackup – USER_DATABASES – FULL

When I run the command with the –Jobs parameter, I see just these jobs moved:

2016-11-22 09_45_14-powershell

Let’s actually do that. Once I remove the “”-Whatif” and run the command, I see this:

2016-11-22 10_09_21-SQLQuery1.sql - localhost_SQL2016.DBAAdmin (PLATO_Steve (66))_ - Microsoft SQL S

Note, this doesn’t actually move the dependent objects. Meaning the first job uses this procedure:  [dbo].[DatabaseBackup]

I’d have to make sure this is copied separately for the job to actually run and succeed. Right now it will fail with an error that an object can’t be found. However, if you have jobs that you are using in a DR situation, this ensures the job itself is moved over.

Rather than copy the syspolicy_purge_history job, I’ll exclude it. I can do that with the –Exclude parameter. Note that the two jobs I’ve already copied are shown as warnings.

2016-11-22 10_11_39-powershell

All the parts of the job itself are copied. The description, the owner, the comments, the schedule, the steps. Everything is copied, which makes this perfect for ensuring that when you deploy a job, it can be deployed to all of the places you need it deployed by copying it from the source.

Of course, you’ll also want to ensure you use some method to disable the jobs on the DR instances (along with re-enabling them on failover).

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , , . Bookmark the permalink.

2 Responses to Moving Tasks with Copy-SqlJob

  1. Kyle says:

    This is great! Does it do anything with the connections (like remapping the Local Server Connection) or do we still need to do that after copying the jobs?

  2. way0utwest says:

    I don’t think this does anything beyond copying the job as is. I suspect it doesn’t, but haven’t had a chance to test this.

Comments are closed.