forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Export-DbaInstance.ps1
449 lines (391 loc) · 27.4 KB
/
Export-DbaInstance.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
function Export-DbaInstance {
<#
.SYNOPSIS
Exports SQL Server *ALL* database restore scripts, logins, database mail profiles/accounts, credentials, SQL Agent objects, linked servers,
Central Management Server objects, server configuration settings (sp_configure), user objects in systems databases,
system triggers and backup devices from one SQL Server to another.
For more granular control, please use one of the -Exclude parameters and use the other functions available within the dbatools module.
.DESCRIPTION
Export-DbaInstance consolidates most of the export scripts in dbatools into one command.
This is useful when you're looking to Export entire instances. It less flexible than using the underlying functions.
Think of it as an easy button. Unless an -Exclude is specified, it exports:
All database restore scripts.
All logins.
All database mail objects.
All credentials.
All objects within the Job Server (SQL Agent).
All linked servers.
All groups and servers within Central Management Server.
All SQL Server configuration objects (everything in sp_configure).
All user objects in system databases.
All system triggers.
All system backup devices.
All Audits.
All Endpoints.
All Extended Events.
All Policy Management objects.
All Resource Governor objects.
All Server Audit Specifications.
All Custom Errors (User Defined Messages).
All Server Roles.
All Availability Groups.
.PARAMETER SqlInstance
The target SQL Server instances
.PARAMETER SqlCredential
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.
.PARAMETER Credential
Alternative Windows credentials for exporting Linked Servers and Credentials. Accepts credential objects (Get-Credential)
.PARAMETER Path
Specifies the directory where the file or files will be exported.
.PARAMETER WithReplace
If this switch is used, databases are restored from backup using WITH REPLACE. This is useful if you want to stage some complex file paths.
.PARAMETER NoRecovery
If this switch is used, databases will be left in the No Recovery state to enable further backups to be added.
.PARAMETER IncludeDbMasterKey
Exports the db master key then logs into the server to copy it to the $Path
.PARAMETER Exclude
Exclude one or more objects to export
Databases
Logins
AgentServer
Credentials
LinkedServers
SpConfigure
CentralManagementServer
DatabaseMail
SysDbUserObjects
SystemTriggers
BackupDevices
Audits
Endpoints
ExtendedEvents
PolicyManagement
ResourceGovernor
ServerAuditSpecifications
CustomErrors
ServerRoles
AvailabilityGroups
ReplicationSettings
.PARAMETER BatchSeparator
Batch separator for scripting output. "GO" by default.
.PARAMETER NoPrefix
If this switch is used, the scripts will not include prefix information containing creator and datetime.
.PARAMETER ExcludePassword
If this switch is used, the scripts will not include passwords for Credentials, LinkedServers or Logins.
.PARAMETER ScriptingOption
Add scripting options to scripting output for all objects except Registered Servers and Extended Events.
.PARAMETER Append
Append to the target file instead of overwriting.
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: Export
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Export-DbaInstance
.EXAMPLE
PS C:\> Export-DbaInstance -SqlInstance sqlserver\instance
All databases, logins, job objects and sp_configure options will be exported from
sqlserver\instance to an automatically generated folder name in Documents.
.EXAMPLE
PS C:\> Export-DbaInstance -SqlInstance sqlcluster -Exclude Databases, Logins -Path C:\dr\sqlcluster
Exports everything but logins and database restore scripts to C:\dr\sqlcluster
.EXAMPLE
PS C:\> Export-DbaInstance -SqlInstance sqlcluster -Path C:\servers\ -NoPrefix
Exports everything to C:\servers but scripts do not include prefix information.
#>
[CmdletBinding()]
param (
[parameter(Mandatory, ValueFromPipeline)]
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[PSCredential]$Credential,
[Alias("FilePath")]
[string]$Path = (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport'),
[switch]$NoRecovery,
[switch]$IncludeDbMasterKey,
[ValidateSet('Databases', 'Logins', 'AgentServer', 'Credentials', 'LinkedServers', 'SpConfigure', 'CentralManagementServer', 'DatabaseMail', 'SysDbUserObjects', 'SystemTriggers', 'BackupDevices', 'Audits', 'Endpoints', 'ExtendedEvents', 'PolicyManagement', 'ResourceGovernor', 'ServerAuditSpecifications', 'CustomErrors', 'ServerRoles', 'AvailabilityGroups', 'ReplicationSettings')]
[string[]]$Exclude,
[string]$BatchSeparator = 'GO',
[switch]$Append,
[Microsoft.SqlServer.Management.Smo.ScriptingOptions]$ScriptingOption,
[switch]$NoPrefix = $false,
[switch]$ExcludePassword,
[switch]$EnableException
)
begin {
$null = Test-ExportDirectory -Path $Path
if (-not $ScriptingOption) {
$ScriptingOption = New-DbaScriptingOption
}
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
$started = Get-Date
$ScriptingOptions = New-Object Microsoft.SqlServer.Management.Smo.ScriptingOptions
$ScriptingOptions.ScriptBatchTerminator = $true
}
process {
if (Test-FunctionInterrupt) { return }
foreach ($instance in $SqlInstance) {
$stepCounter = $fileCounter = 0
try {
$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 10
} catch {
Stop-Function -Message "Error occurred while establishing connection to $instance" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}
$timeNow = (Get-Date -uformat "%m%d%Y%H%M%S")
$exportPath = Join-DbaPath -Path $Path -Child "$($server.name.replace('\', '$'))-$timeNow"
if (-not (Test-Path $exportPath)) {
try {
$null = New-Item -ItemType Directory -Path $exportPath -ErrorAction Stop
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_
return
}
}
if ($Exclude -notcontains 'SpConfigure') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting SQL Server Configuration"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting SQL Server Configuration"
Export-DbaSpConfigure -SqlInstance $server -FilePath "$exportPath\$fileCounter-sp_configure.sql"
if (-not (Test-Path "$exportPath\$fileCounter-sp_configure.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'CustomErrors') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting custom errors (user defined messages)"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting custom errors (user defined messages)"
$null = Get-DbaCustomError -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-customererrors.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-customererrors.sql"
if (-not (Test-Path "$exportPath\$fileCounter-customererrors.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'ServerRoles') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting server roles"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting server roles"
$null = Get-DbaServerRole -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-serverroles.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-serverroles.sql"
if (-not (Test-Path "$exportPath\$fileCounter-serverroles.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'Credentials') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting SQL credentials"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting SQL credentials"
$null = Export-DbaCredential -SqlInstance $server -Credential $Credential -FilePath "$exportPath\$fileCounter-credentials.sql" -Append:$Append -ExcludePassword:$ExcludePassword
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-credentials.sql"
if (-not (Test-Path "$exportPath\$fileCounter-credentials.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'Logins') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting logins"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting logins"
Export-DbaLogin -SqlInstance $server -FilePath "$exportPath\$fileCounter-logins.sql" -Append:$Append -ExcludePassword:$ExcludePassword -NoPrefix:$NoPrefix -WarningAction SilentlyContinue
if (-not (Test-Path "$exportPath\$fileCounter-logins.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'DatabaseMail') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting database mail"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting database mail"
$null = Get-DbaDbMailConfig -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-dbmail.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaDbMailAccount -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-dbmail.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaDbMailProfile -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-dbmail.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaDbMailServer -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-dbmail.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaDbMail -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-dbmail.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-dbmail.sql"
if (-not (Test-Path "$exportPath\$fileCounter-dbmail.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'CentralManagementServer') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting Central Management Server"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting Central Management Server"
$null = Get-DbaRegServerGroup -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-regserver.sql" -Append:$Append -BatchSeparator 'GO' -NoPrefix:$NoPrefix
$null = Get-DbaRegServer -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-regserver.sql" -Append:$Append -BatchSeparator 'GO' -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-regserver.sql"
if (-not (Test-Path "$exportPath\$fileCounter-regserver.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'BackupDevices') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting Backup Devices"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting Backup Devices"
$null = Get-DbaBackupDevice -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-backupdevices.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-backupdevices.sql"
if (-not (Test-Path "$exportPath\$fileCounter-backupdevices.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'LinkedServers') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting linked servers"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting linked servers"
Export-DbaLinkedServer -SqlInstance $server -FilePath "$exportPath\$fileCounter-linkedservers.sql" -Credential $Credential -Append:$Append -ExcludePassword:$ExcludePassword
if (-not (Test-Path "$exportPath\$fileCounter-linkedservers.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'SystemTriggers') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting System Triggers"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting System Triggers"
$null = Get-DbaInstanceTrigger -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-servertriggers.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$triggers = Get-Content -Path "$exportPath\$fileCounter-servertriggers.sql" -Raw -ErrorAction Ignore
if ($triggers) {
$triggers = $triggers.ToString() -replace 'CREATE TRIGGER', "GO`r`nCREATE TRIGGER"
$triggers = $triggers.ToString() -replace 'ENABLE TRIGGER', "GO`r`nENABLE TRIGGER"
$null = $triggers | Set-Content -Path "$exportPath\$fileCounter-servertriggers.sql" -Force
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-servertriggers.sql"
}
if (-not (Test-Path "$exportPath\$fileCounter-servertriggers.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'Databases') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting database restores"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting database restores"
Get-DbaDbBackupHistory -SqlInstance $server -Last | Restore-DbaDatabase -SqlInstance $server -NoRecovery:$NoRecovery -WithReplace -OutputScriptOnly -WarningAction SilentlyContinue | Out-File -FilePath "$exportPath\$fileCounter-databases.sql" -Append:$Append
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-databases.sql"
if (-not (Test-Path "$exportPath\$fileCounter-databases.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'Audits') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting Audits"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting Audits"
$null = Get-DbaInstanceAudit -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-audits.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-audits.sql"
if (-not (Test-Path "$exportPath\$fileCounter-audits.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'ServerAuditSpecifications') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting Server Audit Specifications"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting Server Audit Specifications"
$null = Get-DbaInstanceAuditSpecification -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-auditspecs.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-auditspecs.sql"
if (-not (Test-Path "$exportPath\$fileCounter-auditspecs.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'Endpoints') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting Endpoints"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting Endpoints"
$null = Get-DbaEndpoint -SqlInstance $server | Where-Object IsSystemObject -eq $false | Export-DbaScript -FilePath "$exportPath\$fileCounter-endpoints.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-endpoints.sql"
if (-not (Test-Path "$exportPath\$fileCounter-endpoints.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'PolicyManagement') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting Policy Management"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting Policy Management"
$null = Get-DbaPbmCondition -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-policymanagement.sql" -Append:$Append -BatchSeparator $BatchSeparator -NoPrefix:$NoPrefix
$null = Get-DbaPbmObjectSet -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-policymanagement.sql" -Append:$Append -BatchSeparator $BatchSeparator -NoPrefix:$NoPrefix
$null = Get-DbaPbmPolicy -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-policymanagement.sql" -Append:$Append -BatchSeparator $BatchSeparator -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-policymanagement.sql"
if (-not (Test-Path "$exportPath\$fileCounter-policymanagement.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'ResourceGovernor') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting Resource Governor"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting Resource Governor"
$null = Get-DbaResourceGovernor -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-resourcegov.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaRgClassifierFunction -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-resourcegov.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaRgResourcePool -SqlInstance $server | Where-Object Name -notin 'default', 'internal' | Export-DbaScript -FilePath "$exportPath\$fileCounter-resourcegov.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaRgWorkloadGroup -SqlInstance $server | Where-Object Name -notin 'default', 'internal' | Export-DbaScript -FilePath "$exportPath\$fileCounter-resourcegov.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Add-Content -Value "ALTER RESOURCE GOVERNOR RECONFIGURE" -Path "$exportPath\$fileCounter-resourcegov.sql"
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-resourcegov.sql"
if (-not (Test-Path "$exportPath\$fileCounter-resourcegov.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'ExtendedEvents') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting Extended Events"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting Extended Events"
$null = Get-DbaXESession -SqlInstance $server | Export-DbaXeSession -FilePath "$exportPath\$fileCounter-extendedevents.sql" -Append:$Append -BatchSeparator 'GO' -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-extendedevents.sql"
if (-not (Test-Path "$exportPath\$fileCounter-extendedevents.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'AgentServer') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting job server"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting job server"
$null = Get-DbaAgentJobCategory -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-sqlagent.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaAgentOperator -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-sqlagent.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaAgentAlert -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-sqlagent.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaAgentProxy -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-sqlagent.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaAgentSchedule -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-sqlagent.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
$null = Get-DbaAgentJob -SqlInstance $server | Export-DbaScript -FilePath "$exportPath\$fileCounter-sqlagent.sql" -Append:$Append -BatchSeparator $BatchSeparator -ScriptingOptionsObject $ScriptingOption -NoPrefix:$NoPrefix
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-sqlagent.sql"
if (-not (Test-Path "$exportPath\$fileCounter-sqlagent.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'ReplicationSettings') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting replication settings"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting replication settings"
$null = Export-DbaRepServerSetting -SqlInstance $instance -SqlCredential $SqlCredential -FilePath "$exportPath\$fileCounter-replication.sql"
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-replication.sql"
if (-not (Test-Path "$exportPath\$fileCounter-replication.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'SysDbUserObjects') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting user objects in system databases (this can take a minute)."
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting user objects in system databases (this can take a minute)."
$null = Export-DbaSysDbUserObject -SqlInstance $server -FilePath "$exportPath\$fileCounter-userobjectsinsysdbs.sql" -BatchSeparator $BatchSeparator -NoPrefix:$NoPrefix -ScriptingOptionsObject $ScriptingOption
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-userobjectsinsysdbs.sql"
if (-not (Test-Path "$exportPath\$fileCounter-userobjectsinsysdbs.sql")) {
$fileCounter--
}
}
if ($Exclude -notcontains 'AvailabilityGroups') {
$fileCounter++
Write-Message -Level Verbose -Message "Exporting availability group"
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting availability groups"
$null = Get-DbaAvailabilityGroup -SqlInstance $server -WarningAction SilentlyContinue | Export-DbaScript -FilePath "$exportPath\$fileCounter-DbaAvailabilityGroups.sql" -Append:$Append -BatchSeparator $BatchSeparator -NoPrefix:$NoPrefix #-ScriptingOptionsObject $ScriptingOption
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\$fileCounter-DbaAvailabilityGroups.sql"
if (-not (Test-Path "$exportPath\$fileCounter-DbaAvailabilityGroups.sql")) {
$fileCounter--
}
}
Write-Progress -Activity "Performing Instance Export for $instance" -Completed
}
}
end {
$totalTime = ($elapsed.Elapsed.toString().Split(".")[0])
Write-Message -Level Verbose -Message "SQL Server export complete."
Write-Message -Level Verbose -Message "Export started: $started"
Write-Message -Level Verbose -Message "Export completed: $(Get-Date)"
Write-Message -Level Verbose -Message "Total Elapsed time: $totalTime"
}
}