forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnable-DbaReplPublishing.ps1
119 lines (91 loc) · 5.54 KB
/
Enable-DbaReplPublishing.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
function Enable-DbaReplPublishing {
<#
.SYNOPSIS
Enables replication publishing for the target SQL instances.
.DESCRIPTION
Enables replication publishing for the target SQL instances.
.PARAMETER SqlInstance
The target SQL Server instance or 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 SnapshotShare
The share used to access snapshot files.
The default is the ReplData folder within the InstallDataDirectory for the instance.
.PARAMETER PublisherSqlLogin
If this is used the PublisherSecurity will be set to use this.
If not specified WindowsAuthentication will be used - this is the default, and recommended method.
.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.
.PARAMETER WhatIf
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
.PARAMETER Confirm
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
.NOTES
Tags: repl, Replication
Author: Jess Pomfret (@jpomfret), jesspomfret.com
Website: https://dbatools.io
Copyright: (c) 2023 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Enable-DbaReplPublishing
.EXAMPLE
PS C:\> Enable-DbaReplPublishing -SqlInstance SqlBox1\Instance2 -StartupProcedure '[dbo].[StartUpProc1]'
Attempts to set the procedure '[dbo].[StartUpProc1]' in the master database of SqlBox1\Instance2 for automatic execution when the instance is started.
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[string]$SnapshotShare,
[PSCredential]$PublisherSqlLogin,
[switch]$EnableException
)
process {
foreach ($instance in $SqlInstance) {
$replServer = Get-DbaReplServer -SqlInstance $instance -SqlCredential $SqlCredential -EnableException:$EnableException
Write-Message -Level Verbose -Message "Enabling replication publishing for $instance"
if ($replServer.IsDistributor) {
try {
if ($PSCmdlet.ShouldProcess($instance, "Getting distribution information on $instance")) {
$distPublisher = New-Object Microsoft.SqlServer.Replication.DistributionPublisher
$distPublisher.ConnectionContext = $replServer.ConnectionContext
$distPublisher.Name = $instance
$distPublisher.DistributionDatabase = $replServer.DistributionDatabases.Name
if (Test-Bound SnapshotShare -Not) {
$SnapshotShare = Join-Path (Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential).InstallDataDirectory 'ReplData'
Write-Message -Level Verbose -Message ('No snapshot share specified, using default of {0}' -f $SnapshotShare)
}
$distPublisher.WorkingDirectory = $SnapshotShare
}
if ($PSCmdlet.ShouldProcess($instance, "Configuring PublisherSecurity on $instance")) {
if ($PublisherSqlLogin) {
Write-Message -Level Verbose -Message "Configuring with a SQLLogin for PublisherSecurity"
$distPublisher.PublisherSecurity.WindowsAuthentication = $false
$distPublisher.PublisherSecurity.SqlStandardLogin = $PublisherSqlLogin.UserName
$distPublisher.PublisherSecurity.SecureSqlStandardPassword = $PublisherSqlLogin.Password
} else {
Write-Message -Level Verbose -Message "Configuring with WindowsAuth for PublisherSecurity"
$distPublisher.PublisherSecurity.WindowsAuthentication = $true
}
}
if ($PSCmdlet.ShouldProcess($instance, "Enable publishing on $instance")) {
Write-Message -Level Debug -Message $distPublisher
# lots more properties to add as params
$distPublisher.Create()
$replServer.Refresh()
$replServer
}
} catch {
Stop-Function -Message "Unable to enable replication publishing" -ErrorRecord $_ -Target $instance -Continue
}
} else {
Stop-Function -Message "$instance isn't currently enabled for distributing. Please enable that first." -Target $instance -Continue
}
}
}
}