November 22, 2013 at 4:57 pm
Hello I just started a new position and I have been asked to go through all our SQL Servers and pull a copy of all the SQL Server Agent Jobs off to a file. I started this by just using SSMS and "Right Clicking" each Job then selecting Script Job as > Create To > File.
I was wondering is anyone had a script or that can automatically do this for all jobs on the Server? We have over 100 Servers in my new environment and some have as many as 50 jobs on them.
Any help with this would be greatly Appreciated.
Thanks
November 24, 2013 at 10:51 am
1 level of improvement is to display the job list in the "Object Explorer" pane, highlight all the jobs at once, then "Create to File" and put them all in 1 file for that server.
I do that once in a while when I want to search all the jobs for something specific.
November 24, 2013 at 6:57 pm
jameslauf (11/22/2013)
I have been asked to go through all our SQL Servers and pull a copy of all the SQL Server Agent Jobs off to a file.
Unless they told you "Why", that's a fairly ridiculous request. What do they indend to achieve from such a thing? There may be much better methods than the simple listing of SQL Server Agent jobs but they must first define what they seek.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 25, 2013 at 12:48 pm
We are getting ready to Sunset several Servers here and we are under several regulations so I believe it is for documentation purposes.
I am new enough to this company and my DBA experience until now has been on small envinronments like 2 SQL servers and max 5 Databases.
November 25, 2013 at 12:53 pm
I was able to Highlight all and copy to single file. Thanks
I am still hoping to find a way to automate this more. All help is appreciated.
November 26, 2013 at 4:18 am
Here is the script that i found ..
http://www.nilkanth.com/2004/07/18/sql-jobs-auto-backup/
Then put this script in a job and schedule it to receive the output .
Regards,
Kumar
November 26, 2013 at 6:40 am
I have a couple of powershell scripts which I can use to do this. I use them when I take over a role if there is little source control and just as a precaution. They are useful for migrations too.
I forget where I got this one so I can't give credit to the initial author. I added a few modifications to it so...
# Date: 23/02/12
# Description: PS script to generate all SQL Server Agent jobs on the given instance.
# The script accepts an input file of server names.
# Version: 1.0
#
# Example Execution: .\Create_SQLAentJobSripts.ps1 .\ServerNameList.txt
#param([String]$ServerListPath)
#Load the input file into an Object array
$ServerNameList = "";
#get-content -path $ServerListPath
$login = Read-Host 'Enter your SQL login'
$password = Read-Host 'Enter your password' -AsSecureString;
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password);
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR);
#Load the SQL Server SMO Assemly
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo') | Out-Null
#Create a new SqlConnection object
$objSQLConnection = New-Object System.Data.SqlClient.SqlConnection
#For each server in the array do the following..
foreach($ServerName in $ServerNameList)
{
Try
{
#$objSQLConnection.ConnectionString = "Server=$ServerName;Integrated Security=SSPI;"
$objSQLConnection.ConnectionString = "Server=$ServerName;User Id=$login;Password=$PlainPassword"
Write-Host "Trying to connect to SQL Server instance on $ServerName..." -NoNewline
$objSQLConnection.Open() | Out-Null
Write-Host "Success."
$objSQLConnection.Close()
}
Catch
{
Write-Host -BackgroundColor Red -ForegroundColor White "Fail"
$errText = $Error[0].ToString()
if ($errText.Contains("network-related"))
{Write-Host "Connection Error. Check server name, port, firewall."}
Write-Host $errText
continue
}
#IF the output folder does not exist then create it
$OutputFolder = "C:\Scripts\Jobs\$ServerName"
$DoesFolderExist = Test-Path $OutputFolder
$null = if (!$DoesFolderExist){MKDIR "$OutputFolder"}
#Create a new SMO instance for this $ServerName
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $objSQLConnection
#Script out each SQL Server Agent Job for the server
$srv.JobServer.Jobs | foreach {$job = $_.name -replace "\*", ""; $_.Script() | out-file "$OutputFolder\$job.sql"}
}
I'll need to try and find the other one. Its much easier to follow and uses SQLPSX.
November 26, 2013 at 9:06 am
Thank You very much. Both of these should do the job. I will post once I have tried them.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply