forked from Jamal27/SQLServer_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackupScriptJobs.ps1
More file actions
21 lines (19 loc) · 926 Bytes
/
BackupScriptJobs.ps1
File metadata and controls
21 lines (19 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Utilizando a função do Dbatools para capturar a lista de todos os Jobs de um servidor específico
$JobList = Get-DbaAgentJob -SqlInstance "DESKTOP-A7S2JPV\SQLSERVER2016" -EnableException
#Caminho onde será gerado os scripts
$Pathbase = "C:\temp\BackupJobs\"
$Path = ""
ForEach ($Job in $JobList)
{
#Corrige nome do arquivo em jobs com caracter especial no nome
$Path = $Pathbase + $Job.Name.Replace("/"," ").Replace("\"," ").Replace(":"," ") + ".sql"
#Inclui algumas opções no script, como cabeçalho do Job
$options = New-DbaScriptingOption
$options.ScriptSchema = $true
$options.IncludeDatabaseContext = $true
$options.IncludeHeaders = $true
$options.ScriptBatchTerminator = $true
$options.AnsiFile = $true
#Exportar Job por Job para o caminho especificado utilizando Dbatools.
$Job | Export-DbaScript -Path $Path -ScriptingOptionsObject $options -EnableException
}