Does anyone have an idea of how to generate the Create statement for a SQL Agent Job? I've looked everywhere, and I can't seem to find a search result that doesn't involve using SSMS and going through the Object Explorer. I've gone off and found the [msdb].[dbo].[sysjobsteps] table, where it has the UID of the job, and the steps. My idea was to group by job_id, order by step_id and then flounder around for a while before figuring out how to concatenate. But I thought I would ask here before proceeding down that path.
Powershell to the rescue:
Here's a start, without the error handling that SSMS adds when scripting the job. It pulls info from the relevant tables in MSDB that are used to run
sp_add_job
andsp_add_jobstep
You could also create a stored procedure out of this with a @JobName parameter and pass it in the name of the job you want to script out.