-
Notifications
You must be signed in to change notification settings - Fork 0
/
RMO.ps1
executable file
·191 lines (149 loc) · 7.14 KB
/
RMO.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
#https://blogs.msdn.microsoft.com/johan/2008/10/01/powershell-editing-permissions-on-a-file-or-folder/
##SETUP
$workingDir = 'C:\Dev\Replication\Dans2017\repldata'
$passwrd = 'replP4ss!!123'
$secPwd = ConvertTo-SecureString $pwd -AsPlainText -Force
$smbShare = "repldata"
$distDbName = "distribution"
$logReaderUser = "repl_logreader"
$snapshotUser = "repl_snapshot"
$distributionUser = "repl_distribution"
if(!(Test-Path -Path $workingDir)) {
New-Item -Path $workingDir -ItemType Directory
}
if (!(Get-SmbShare -Name $smbShare -ErrorAction SilentlyContinue)) {
New-SmbShare -Path $workingDir -Name $smbShare -NoAccess "Everyone"
}
[hashtable]$replUsers = @{$logReaderUser = 'Read';$snapshotUser = 'Full';$distributionUser = 'Read'}
foreach ( $user in $replUsers.GetEnumerator() ) {
if(!(Get-LocalUser -Name $user.Key)) {
New-LocalUser -Name $user.Key -AccountNeverExpires -PasswordNeverExpires -Password $secPwd -Description "PWD: $passwrd"
}
switch ($user.Value) {
Read {
$permission = "Read"
}
Full {
$permission = "FullControl"
}
}
$acl = Get-Acl $workingDir
$accessRule = [System.Security.AccessControl.FileSystemAccessRule]::new("$env:COMPUTERNAME\$($user.key)",$permission,"Allow")
$acl.ResetAccessRule($accessRule)
Set-Acl -Path $workingDir -AclObject $acl
Grant-SmbShareAccess -Name $smbShare -AccountName $user.key -AccessRight $user.Value -Force | Out-Null
}
########
Import-Module sqlserver
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Rmo')
$publisherInstance = "DANSLAB\DANS2017"
$distributorInstance = $publisherInstance
$publicationDbName = "WideWorldImporters-Full"
$subscriberInstance = "DANSLAB\DANS2017S"
$subscriberDBName = "WWI_subscriber"
$pubSqlSvr = [Microsoft.SqlServer.Management.Smo.Server]::new($publisherInstance)
$subSqlSvr = [Microsoft.SqlServer.Management.Smo.Server]::new($subscriberInstance)
$distSvr = [Microsoft.SqlServer.Replication.ReplicationServer]::new($distributorInstance)
$pubSvr = [Microsoft.SqlServer.Replication.ReplicationServer]::new($publisherInstance)
$distDb = [Microsoft.SqlServer.Replication.DistributionDatabase]::new($distDbName,$distributorInstance)
$publisher = [Microsoft.SqlServer.Replication.DistributionPublisher]::new($publisherInstance,$publisherInstance)
$publicationDb = [Microsoft.SqlServer.Replication.ReplicationDatabase]::new($publicationDbName,$publisherInstance)
$publisher.DistributionDatabase = $distDb.name
$publisher.WorkingDirectory = $workingDir
if (($distSvr.IsDistributor) -and !($distSvr.DistributorInstalled)) {
$distDb.Create()
}
elseif (!($distSvr.DistributorInstalled)) {
$distSvr.InstallDistributor("sdfgis!5DFGa",$distDb) #Make pwd securestring
}
else {
Write-Output "Distributor and distributor database '$distDbName' already exist."
}
if (!$publisher.IsExistingObject) {
$publisher.Create()
}
if($publicationDb.LoadProperties())
{
#create publication, logreader agent and snapshot agent
if(!$pubSqlSvr.Logins.Item("DANSLAB\$logReaderUser")) {
$logReaderLogin = [Microsoft.SqlServer.Management.Smo.Login]::new($publisherInstance,"DANSLAB\$logReaderUser")
$logReaderLogin.LoginType = [Microsoft.SqlServer.Management.Smo.LoginType]("WindowsUser")
$logReaderLogin.Create()
}
$pubSqlDb = $pubSqlSvr.Databases.Item($publicationDbName)
if(!$pubSqlDb.Users.Item("DANSLAB\$logReaderUser")) {
$dbUser = [Microsoft.SqlServer.Management.Smo.User]::new($pubSqlDb,"DANSLAB\$logReaderUser")
$dbUser.Login = "DANSLAB\$logReaderUser"
$dbUser.Create()
}
$dbOwnerRole = $pubSqlDb.Roles.Item("db_owner")
if(!($dbOwnerRole.EnumMembers() | Where-Object {$_ -eq "DANSLAB\$logReaderUser"})) {
$dbOwnerRole.AddMember("DANSLAB\$logReaderUser")
}
$publicationDb.EnabledTransPublishing = $true
if(!($publicationDb.LogReaderAgentExists)) {
$publicationDb.LogReaderAgentProcessSecurity.Login = "DANSLAB\$logReaderUser"
$publicationDb.LogReaderAgentProcessSecurity.Password = $passwrd
$publicationDb.LogReaderAgentPublisherSecurity.WindowsAuthentication = $true
$publicationDb.CreateLogReaderAgent()
}
$publication = [Microsoft.SqlServer.Replication.TransPublication]::new("PUBN-$($publicationDb.Name)",$publicationDb.Name,$publisherInstance)
if(!$publication.IsExistingObject)
{
$publication.Create()
}
else
{
if(!($publication.SnapshotAgentExists))
{
$publication.CreateSnapshotAgent()
}
}
}
#We need an article
#https://docs.microsoft.com/en-us/sql/relational-databases/replication/publish/define-an-article?view=sql-server-2017
$articleName = 'Orders'
$articleTable = '[WideWorldImporters-Full].[Sales].[Orders]'
$schemaOwner = 'sales'
$article = [Microsoft.SqlServer.Replication.TransArticle]::new()
$article.ConnectionContext = $publisher.ConnectionContext
$article.Name = $articleName
$article.DatabaseName = $publicationDbName
$article.SourceObjectName = $articleName
$article.SourceObjectOwner = $schemaOwner
$article.PublicationName = $publication.Name
$article.Type = [Microsoft.SqlServer.Replication.ArticleOptions]::LogBased
if (!$article.IsExistingObject)
{
$article.Create()
}
#Create subscriber (Push for transactional)
#https://docs.microsoft.com/en-us/sql/relational-databases/replication/create-a-push-subscription?view=sql-server-2017
if ($publication.LoadProperties())
{
#Add AllowPush if not set
if (($Publication.Attributes -band [Microsoft.SqlServer.Replication.PublicationAttributes]::AllowPush) -eq 0)
{
$Publication.Attributes += [Microsoft.SqlServer.Replication.PublicationAttributes]::AllowPush
$publication.CommitPropertyChanges()
}
$subscription = [Microsoft.SqlServer.Replication.TransSubscription]::new()
$subscription.ConnectionContext = $publisher.ConnectionContext
$subscription.SubscriberName = $subscriberInstance
$subscription.SubscriptionDBName = $subscriberDBName
$subscription.PublicationName = $publication.Name
$subscription.DatabaseName = $publicationDbName
#credentials for distribution agent
$subscription.SynchronizationAgentProcessSecurity.Login = $distributionUser
$subscription.SynchronizationAgentProcessSecurity.Password = $passwrd
#Should encrypt connection when sending commands to distributor:
#https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/enable-encrypted-connections-to-the-database-engine?view=sql-server-2017
$subscription.CreateSyncAgentByDefault = $true
$subscription.AgentSchedule.FrequencyType = [Microsoft.SqlServer.Replication.ScheduleFrequencyType]::OnDemand
$subscription.Create()
}
##################
#$subscriberConn = [Microsoft.SqlServer.Management.Common.ServerConnection]::new($subscriberInstance)
#$subscriberConn.connect()#
#$subscriberDB = New-Object Microsoft.SqlServer.Management.Smo.Database($subscriberInstance, $subscriberDBName)
#$subscriberDB