Created
July 19, 2019 16:56
-
-
Save roe3p/d81e001b36152fadb88bc6b31620f99b to your computer and use it in GitHub Desktop.
DBA - Backup SQL Agent Jobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# Script out all SQL Agent jobs #> | |
$ServerInstance = "MY_SERVER" | |
$OutputPath ="G:\BACKUPS\MY_SERVER\MY_SERVERAgentJobs\" | |
$jobs = Get-DbaAgentJob -SqlInstance $serverInstance | |
foreach ($job in $jobs) | |
{ | |
$jobnameclean = $job.Name | |
#Write-Host "Job Name before clean: " $jobnameclean | |
$jobnameclean = $jobnameclean.Replace(":", "-") | |
$jobnameclean = $jobnameclean.Replace("\", "-") | |
$jobnameclean = $jobnameclean.Replace("/", "-") | |
$jobnameclean = $jobnameclean.Replace("[", "-") | |
$jobnameclean = $jobnameclean.Replace("]", "-") | |
#Write-Host "Job Name after clean: " $jobnameclean | |
$Filename = $OutputPath + $ServerInstance + "_" + $jobnameclean + ".sql" | |
$job | Export-DbaScript -Filepath $Filename | |
} | |
[Environment]::Exit(0) #Needed if called from within a SQL Agent Job |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment