Skip to content

Commit 7780b4f

Browse files
authored
Update 04_Agent_Jobs.ps1
Script out all Jobs to Single File
1 parent 8f6a5e3 commit 7780b4f

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

Scripts/04_Agent_Jobs.ps1

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<#
1+
<#
22
.SYNOPSIS
3-
Gets the SQL Agent Jobs
3+
Scripts out all SQL Agent Jobs
44
55
.DESCRIPTION
66
Writes the SQL Agent Jobs out to the "04 - Agent Jobs" folder
77
One file per job
8+
and a SingleFile with All Jobs
89
910
.EXAMPLE
1011
04_Agent_Jobs.ps1 localhost
@@ -28,7 +29,7 @@
2829

2930
[CmdletBinding()]
3031
Param(
31-
[string]$SQLInstance='localhost',
32+
[string]$SQLInstance='c0sqlmon',
3233
[string]$myuser,
3334
[string]$mypass
3435
)
@@ -43,10 +44,19 @@ catch
4344
Throw('SQLTranscriptase.psm1 not found')
4445
}
4546

47+
try
48+
{
49+
Import-Module ".\LoadSQLSmo.psm1"
50+
}
51+
catch
52+
{
53+
Throw('LoadSQLSmo.psm1 not found')
54+
}
55+
4656
LoadSQLSMO
4757

4858
# Init
49-
Set-StrictMode -Version latest
59+
Set-StrictMode -Version latest;
5060
[string]$BaseFolder = (Get-Item -Path ".\" -Verbose).FullName
5161
Write-Host -f Yellow -b Black "04 - Agent Jobs"
5262
Write-Output("Server: [{0}]" -f $SQLInstance)
@@ -162,6 +172,13 @@ if(!(test-path -path $fullfolderPathDis))
162172
mkdir $fullfolderPathDis | Out-Null
163173
}
164174

175+
$SingleFilename = "$BaseFolder\$sqlinstance\04 - Agent Jobs\Alljobs.sql"
176+
$SinglejobContents = ""
177+
178+
# Clear out putput folders
179+
Get-ChildItem -Path $fullfolderPathEn -Include * -File -Recurse | remove-item -Confirm:$false
180+
Get-ChildItem -Path $fullfolderPathDis -Include * -File -Recurse | remove-item -Confirm:$false
181+
165182
$jobcount = $server.JObserver.jobs.count
166183

167184
# Export with filename fixups
@@ -179,7 +196,7 @@ if ($jobs -ne $null)
179196
$myjobname = $myjobname.Replace(':', '-')
180197
$myjobname = $myjobname.replace('[','(')
181198
$myjobname = $myjobname.replace(']',')')
182-
$myjobname = $myjobname.replace('*','_')
199+
$myjobname = $myjobname.replace('*','_')
183200
$myjobname = $myjobname.replace('**','__')
184201

185202
if ($job.Isenabled)
@@ -190,11 +207,30 @@ if ($jobs -ne $null)
190207
{
191208
$FileName = "$fullfolderPathDis\$myjobname.sql"
192209
}
210+
211+
Write-Output('{0}' -f $myjobname)
212+
try
213+
{
214+
$jobContents = $job.Script()
215+
216+
# Append this job to the AllJobs string
217+
$SinglejobContents = $SinglejobContents + $jobContents+ "`r`nGO`r`n`r`n"
218+
219+
# Export individual job contents
220+
$jobContents| Out-File -FilePath $FileName
221+
}
222+
catch
223+
{
224+
Write-Output('Error: [{0}]' -f $Error[0])
225+
Write-Output('FileName: [{0}]' -f $FileName)
226+
}
227+
193228

194-
$job.Script() | Out-File -filepath $FileName
195-
$myjobname
196229
}
197230

231+
# Export Alljobs contents
232+
$SinglejobContents | Out-File -FilePath $SingleFilename
233+
198234
Write-Output ("{0} Jobs Exported" -f $jobCount)
199235
}
200236
else

0 commit comments

Comments
 (0)