Create a copy of a SQL Server Job Using T-SQL or Powershell, how can I create a stored procedure that performs the following:


Copy a SQL Server Agent Job to another backup Job on the same server (with a new name)
Disable the backup job so that it is not scheduled.

USE msdb;

IF object_id('[backupSQLServerJob]') IS NULL EXEC ('create procedure [backupSQLServerJob] as select 1')
GO

ALTER PROCEDURE backupSQLServerJob
    @JobName varchar(50)
AS
BEGIN

    DECLARE @BackupName VARCHAR(100) = CONCAT(@JobName, '_bak_', CONVERT(VARCHAR(10), GETDATE(), 112));

    IF EXISTS (SELECT * FROM dbo.sysjobs WHERE name = @JobName)
    BEGIN
        -- What goes here?
    END
END



The solution needs to work on SQL Server 2008 - 2014
It would be a large script as a Job consists of data entries in multiple tables/views:

msdb.dbo.sysjobs_view
msdb.dbo.sysjobsteps
msdb.dbo.sysjobschedules
-- you probably dont need these as you are copying to the same server
msdb.dbo.sysschedules
msdb.dbo.syscategories
sys.server_principals
msdb.dbo.sysOperators


Create Jobs using following SPs:

msdb.dbo.sp_add_job
msdb.dbo.sp_add_jobserver
msdb.dbo.sp_add_jobstep
msdb.dbo.sp_add_jobschedule


Look in MSDN to work out what arguments to pass