forked from mubix/PowerShell-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptReplPublications.ps1
62 lines (57 loc) · 2.47 KB
/
ScriptReplPublications.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
param ($sqlServer,$path,[switch]$scriptPerPublication)
Import-Module Repl
if ($sqlServer -eq "")
{
$sqlserver = Read-Host -Prompt "Please provide a value for -sqlServer"
}
if ($path -eq "")
{
$path = Read-Host -Prompt "Please provide a value for output directory path"
}
$scriptOptions = New-ReplScriptOptions
$scriptOptions.IncludeArticles = $true
$scriptOptions.IncludePublisherSideSubscriptions = $true
$scriptOptions.IncludeCreateSnapshotAgent = $true
$scriptOptions.IncludeGo = $true
$scriptOptions.EnableReplicationDB = $true
$scriptOptions.IncludePublicationAccesses = $true
$scriptOptions.IncludeCreateLogreaderAgent = $true
$scriptOptions.IncludeCreateQueuereaderAgent = $true
$scriptOptions.IncludeSubscriberSideSubscriptions = $true
$distributor = Get-ReplServer $sqlserver
if($distributor.DistributionServer -eq $distributor.SqlServerName)
{
$distributor.DistributionPublishers | ForEach-Object {
$distributionPublisher = $_
if($distributionPublisher.PublisherType -eq "MSSQLSERVER")
{
$outPath = "{0}\from_{1}\{2}\" -f $path,$distributionPublisher.Name.Replace("\","_"),$((Get-Date).toString('yyyy-MMM-dd_HHmmss'))
New-Item $outPath -ItemType Directory | Out-Null
Get-ReplPublication $distributionPublisher.Name | ForEach-Object {
$publication = $_
$fileName = "{0}{1}.sql" -f $outPath,$publication.DatabaseName.Replace(" ", "")
if($scriptPerPublication)
{
$fileName = "{0}{1}_{2}.sql" -f $outPath,$publication.DatabaseName.Replace(" ", ""),$publication.Name.Replace(" ", "")
}
Write-Debug $("Scripting {0} to {1}" -f $publication.Name.Replace(" ", ""),$fileName)
Get-ReplScript -rmo $publication -scriptOpts $($scriptOptions.ScriptOptions) | Out-File $fileName -Append
}
}
}
}
else
{
$outPath = "{0}\from_{1}\{2}\" -f $path,$distributor.SqlServerName.Replace("\","_"),$((Get-Date).toString('yyyy-MMM-dd_HHmmss'))
New-Item $outpath -ItemType Directory | Out-Null
Get-ReplPublication $distributor.SqlServerName | ForEach-Object {
$publication = $_
$fileName = "{0}{1}.sql" -f $outPath,$publication.DatabaseName.Replace(" ", "")
if($scriptPerPublication)
{
$fileName = "{0}{1}_{2}.sql" -f $outPath,$publication.DatabaseName.Replace(" ", ""),$publication.Name.Replace(" ", "")
}
Write-Debug $("Scripting {0} to {1}" -f $publication.Name.Replace(" ", ""),$fileName)
Get-ReplScript -rmo $publication -scriptOpts $($scriptOptions.ScriptOptions) | Out-File $fileName -Append
}
}