SQLDMO Script SQL Server Jobs

The following VBScript code snippet can be used to script all SQL Server Jobs on a SQL Server instance:


Const ForWriting = 2

Dim job
Dim oFSO
Dim oFSOWrite
Dim oSQLServer

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

oSQLServer.LoginSecure = True
oSQLServer.Connect “(local)”

Set oFSOWrite = oFSO.OpenTextFile(“c:\sqljobs.sql”, ForWriting, True)

For Each job in oSQLServer.JobServer.Jobs
oFSOWrite.WriteLine job.script


Set oFSO = Nothing