dumping ground for code that might be useful sometime, somewhere. Beware the formatting mangle of the blog post...

Thursday, March 1, 2007

SQL-DMO: SQLServerAgent Jobs to files

Generate a separate script for each SQLServerAgent job. Filename the same as the job name.

Make sure SQLServerAgent job names contain no illegal filename characters and service account has write permissions to the destination folder. Edit for database servername and folder location for output files.


Const ForWriting = 2

Dim job
Dim oFSO
Dim oFSOWrite
Dim oSQLServer
Dim s

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oSQLServer = CreateObject("SQLDMO.SQLServer")

oSQLServer.LoginSecure = True
oSQLServer.Connect "SERVER\INSTANCE"

For Each job in oSQLServer.JobServer.Jobs
Set oFSOWrite = oFSO.OpenTextFile("c:\Datadump\" & job.name & ".sql", ForWriting, True)
oFSOWrite.WriteLine job.Script
oFSOWrite.Close
Next

Set oFSO = Nothingo
SQLServer.Disconnect
Set oSQLServer = Nothing

No comments: