@@ -131,6 +131,7 @@ Connect-Rubrik @ConnectRubrik
131131# Get information about the database we will add to an availaility group
132132Write-Host " - Getting information about $DatabaseName on $PrimarySQLServerInstance from $RubrikServer "
133133$RubrikDatabase = Get-RubrikDatabase - Name $DatabaseName - ServerInstance $PrimarySQLServerInstance - DetailedObject | Where-Object {$_.isRelic -eq $false }
134+ Write-Debug - Message ($RubrikDatabase | Out-String )
134135if ([bool ]($RubrikDatabase.PSobject.Properties.name -match " id" ) -eq $false ){
135136 Write-Error - Message " Database $DatabaseName on $PrimarySQLServerInstance not found on $RubrikServer "
136137 break
@@ -144,21 +145,23 @@ if ([bool]($RubrikDatabase.latestRecoveryPoint) -eq $false){
144145
145146# Go to the primary replica and get the other replica servers
146147$SourceSQLInstance = Get-RubrikSQLInstance - ServerInstance $PrimarySQLServerInstance
148+ Write-Debug - Message ($SourceSQLInstance | Out-String )
147149
148150# Is the database already in an availability group?
149151Write-Host " - Checking to see if database is not already in an Availability Group"
150152$Query = " SELECT top 1 database_id
151153FROM sys.dm_hadr_database_replica_states
152154WHERE database_id = DB_ID('$ ( $DatabaseName ) ')"
153155$Groups = Invoke-Sqlcmd - ServerInstance $PrimarySQLServerInstance - Query $Query
156+ Write-Debug - Message ($Groups | Out-String )
154157
155158if ([bool ]($Groups.PSobject.Properties.name -match " database_id" ) -eq $true ){
156159 Write-Error " Database is already a member of an Availability Group"
157160 break
158161}
159162
160163# What replicas are involved in the availbility group?
161- Write-Host " Getting replica servers involved in $AvailabilityGroupName from $PrimarySQLServerInstance "
164+ Write-Host " - Getting replica servers involved in $AvailabilityGroupName from $PrimarySQLServerInstance "
162165If ($ ($SourceSQLInstance.Version ).substring(0 , $SourceSQLInstance.Version.indexOf (" ." )) -ge 13 ){
163166 $Query = " SELECT replica_server_name
164167 FROM [sys].[availability_groups] groups
@@ -174,33 +177,38 @@ else{
174177 WHERE name = '$ ( $AvailabilityGroupName ) ' "
175178}
176179$Replicas = Invoke-Sqlcmd - ServerInstance $PrimarySQLServerInstance - Query $Query
177-
180+ Write-Debug - Message ( $Replicas | Out-String )
178181[System.Collections.ArrayList ] $ReplicasInAG = @ ()
179182foreach ($Replica in $Replicas ){
180183 $db = New-Object PSObject
181- $db | Add-Member - type NoteProperty - Name ReplicaServerName - Value $Replica.replica_server_name
182184 if ($Replica.replica_server_name.IndexOf (" \" ) -gt 0 ){
183- Write-Host " Getting information about $ ( $Replica.replica_server_name ) from $ ( $RubrikServer ) "
185+ # Write-Host "- Getting information about $($Replica.replica_server_name) from $($RubrikServer)"
184186 $HostName = $Replica.replica_server_name.Substring (0 , $Replica.replica_server_name.IndexOf (" \" ))
185187 $Instance = $Replica.replica_server_name.Substring ($Replica.replica_server_name.IndexOf (" \" )+1 , ($Replica.replica_server_name.Length - $Replica.replica_server_name.IndexOf (" \" )) -1 )
186188 $TargetInstance = Get-RubrikSQLInstance - ServerInstance $Replica.replica_server_name
187189 if ([bool ]($TargetInstance.PSobject.Properties.name -match " id" ) -eq $false ){
188190 $FQDN = ([System.Net.Dns ]::GetHostByName(($HostName ))).hostname
191+ Write-Debug - Message " - FQDN: [$ ( $FQDN ) ]"
189192 $TargetInstance = Get-RubrikSQLInstance - ServerInstance " $ ( $FQDN ) \$ ( $Instance ) "
190193 $Replica.replica_server_name = " $ ( $FQDN ) \$ ( $Instance ) "
191194 }
192195 }
193196 else {
194- Write-Host " Getting information about $ ( $Replica.replica_server_name ) from $ ( $RubrikServer ) "
197+ # Write-Host "- Getting information about $($Replica.replica_server_name) from $($RubrikServer)"
195198 $HostName = $Replica.replica_server_name
196199 $Instance = " DEFAULT"
197200 $TargetInstance = Get-RubrikSQLInstance - ServerInstance $Replica.replica_server_name
198201 if ([bool ]($TargetInstance.PSobject.Properties.name -match " id" ) -eq $false ){
199202 $FQDN = ([System.Net.Dns ]::GetHostByName(($HostName ))).hostname
203+ Write-Debug - Message " FQDN: [$ ( $FQDN ) ]"
200204 $TargetInstance = Get-RubrikSQLInstance - ServerInstance $FQDN
205+ $Replica.replica_server_name = $FQDN
201206 }
202207 }
203-
208+ Write-Debug - Message ($TargetInstance | Out-String )
209+
210+ $db | Add-Member - type NoteProperty - Name ReplicaServerName - Value $Replica.replica_server_name
211+ $db | Add-Member - Type NoteProperty - Name RubrikDatabaseId - Value $RubrikDatabase.id
204212
205213 if ([bool ]($TargetInstance.PSobject.Properties.name -match " id" ) -eq $false ){
206214 Write-Error - Message " $ ( $Replica.replica_server_name ) was not found on $RubrikServer "
@@ -211,13 +219,15 @@ foreach ($Replica in $Replicas){
211219 $db | Add-Member - type NoteProperty - name Instance - Value $Instance
212220 $db | Add-Member - type NoteProperty - name DatabaseName - Value $DatabaseName
213221
214-
222+
215223 if ($Replica.replica_server_name -ne $PrimarySQLServerInstance ){
216224 $Query = " SELECT state_desc FROM sys.databases WHERE name = '" + $DatabaseName + " '"
225+ Write-Debug - Message " replica_server_name: [$ ( $Replica.replica_server_name ) ]"
226+ Write-Debug - Message " Query: [$ ( $Query ) ]"
217227 $Results = Invoke-Sqlcmd - ServerInstance $Replica.replica_server_name - Query $Query
218228
219229 if ([bool ]($Results.PSobject.Properties.name -match " state_desc" ) -eq $true ){
220- Write-Host " $ ( $DatabaseName ) already exists on $ ( $Replica.replica_server_name ) . Unable to setup log shipping when database already exists"
230+ Write-Error - Message " [ $ ( $DatabaseName ) ] already exists on $ ( $Replica.replica_server_name ) . Unable to setup log shipping when database already exists. "
221231 $db | Add-Member - type NoteProperty - name RubrikRequest - Value " FAILED"
222232 $db | Add-Member - type NoteProperty - name Primary - Value $false
223233 break
@@ -226,65 +236,70 @@ foreach ($Replica in $Replicas){
226236 $TargetFilePaths = Get-RubrikDatabaseFiles - Id $RubrikDatabase.id `
227237 - RecoveryDateTime $RubrikDatabase.latestRecoveryPoint | Select-Object LogicalName, @ {n = ' exportPath' ;e = {$_.OriginalPath }}, @ {n = ' newFilename' ;e = {$_.OriginalName }}
228238
229- Write-Host " Setting up log shipping between $PrimarySQLServerInstance and $ ( $Replica.replica_server_name ) "
239+ Write-Host " - Setting up log shipping between $PrimarySQLServerInstance and $ ( $Replica.replica_server_name ) "
230240 $RubrikRequest = New-RubrikLogShipping - id $RubrikDatabase.id `
231241 - targetInstanceId $TargetInstance.id `
232242 - targetDatabaseName $DatabaseName `
233243 - state " RESTORING" `
234244 - TargetFilePaths $TargetFilePaths
235245 $db | Add-Member - type NoteProperty - name RubrikRequest - Value $RubrikRequest
236246 $db | Add-Member - type NoteProperty - name Primary - Value $false
247+ # $db | Add-Member -Type NoteProperty -Name RubrikDatabaseId -Value $RubrikDatabase.id
237248 }
238249 }
239250 else {
240251 $db | Add-Member - type NoteProperty - name Primary - Value $true
241252 }
242253 $ReplicasInAG += $db
243254}
244- $ReplicasInAG
245- break
246- # #Wait for log shipping requests to complete for all replicas
247- # foreach($Replica in $ReplicasinAG | Where-Object Primary -eq $false)
248- # {
249- # Get-RubrikRequest -id $Replica.RubrikRequest.id -WaitForCompletion -Type mssql
250- # # Get-RubrikRequestInfo -RubrikRequest $Replica.RubrikRequest
251- # }
252255
253- # Write-Host "Wait for all of the logs to be applied" -ForegroundColor Green
254- # foreach($Replica in $ReplicasinAG | Where-Object Primary -eq $false)
255- # {
256- # $GetRubrikLogShipping = @{
257- # PrimaryDatabaseId = $RubrikDatabase.id
258- # }
259- # $RubrikLogShipping = Get-RubrikLogShipping @GetRubrikLogShipping
256+ Write-Debug - Message ($ReplicasInAG | Out-String )
260257
258+ # Wait for log shipping requests to complete for all replicas
259+ Write-Host " - Wait for all of the logs to be applied" - ForegroundColor Green
260+ foreach ($Replica in $ReplicasinAG | Where-Object Primary -eq $false )
261+ {
262+ Write-Host " - Initializing Log Shipping of $ ( $PrimarySQLServerInstance ) .$ ( $Replica.DatabaseName ) to $ ( $Replica.ReplicaServerName ) "
263+ Get-RubrikRequest - id $Replica.RubrikRequest.id - WaitForCompletion - Type mssql
261264
262- # Foreach ($LogShippedDB in $RubrikLogShipping) {
263- # do{
264- # $CheckRubrikLogShipping = Get-RubrikLogShipping -id $LogShippedDB.id
265- # $lastAppliedPoint = ($CheckRubrikLogShipping.lastAppliedPoint)
266- # Start-Sleep -Seconds 1
267- # } until ($latestRecoveryPoint -eq $lastAppliedPoint)
268- # if ($RemoveLogShipping -eq $true){
269- # Write-Host "Removing Log Shipping from $($LogShippedDB.location)" -ForegroundColor Green
270- # Remove-RubrikLogShipping -id $LogShippedDB.id
271- # }
272- # }
265+ do {
266+ # region Last Applied Point
267+ $GetRubrikLogShipping = @ {
268+ PrimaryDatabaseId = $Replica .RubrikDatabaseId
269+ location = $Replica .ReplicaServerName
270+ }
271+ $CheckRubrikLogShipping = Get-RubrikLogShipping @GetRubrikLogShipping
272+ Write-Debug - Message ( $CheckRubrikLogShipping | Out-String )
273+ $lastAppliedPoint = ( $CheckRubrikLogShipping .lastAppliedPoint )
274+ Write-Debug - Message " LastAppliedPoint: [ $ ( $lastAppliedPoint ) ] "
275+ # endregion
273276
277+ # region Lastest Recovery Point
278+ $GetRubrikDatabase = @ {
279+ id = $Replica.RubrikDatabaseId
280+ DetailedObject = $true
281+ }
282+ $CheckRubrikLogShipping = Get-RubrikDatabase @GetRubrikDatabase
283+ $latestRecoveryPoint = $CheckRubrikLogShipping.latestRecoveryPoint
284+ Write-Debug - Message " latestRecoveryPoint: [$ ( $latestRecoveryPoint ) ]"
285+ # endregion
286+ Start-Sleep - Seconds 1
287+ } until ($latestRecoveryPoint -eq $lastAppliedPoint )
288+ }
274289
275290
276291# Add all replicas to the availability group and then remove log shipping.
277292foreach ($Replica in $ReplicasinAG | Sort-Object Primary - Descending )
278293{
279294 if ([bool ]($Replica.Primary ) -eq $true ){
280- Write-Host " Adding $ ( $DatabaseName ) to $ ( $AvailabilityGroupName ) on $ ( $Replica.ReplicaServerName ) "
295+ Write-Host " - Adding $ ( $DatabaseName ) to $ ( $AvailabilityGroupName ) on $ ( $Replica.ReplicaServerName ) "
281296 $Query = " ALTER AVAILABILITY GROUP [$ ( $AvailabilityGroupName ) ] ADD DATABASE [$ ( $DatabaseName ) ];"
282297 }
283298 else {
284- Write-Host " Joining $ ( $DatabaseName ) to $ ( $AvailabilityGroupName ) on $ ( $Replica.ReplicaServerName ) "
299+ Write-Host " - Joining $ ( $DatabaseName ) to $ ( $AvailabilityGroupName ) on $ ( $Replica.ReplicaServerName ) "
285300 $Query = " ALTER DATABASE [$ ( $DatabaseName ) ] SET HADR AVAILABILITY GROUP = [$ ( $AvailabilityGroupName ) ]; "
286301 }
287302 Invoke-Sqlcmd - ServerInstance $Replica.ReplicaServerName - Query $Query - Verbose
288303}
289- Write-Host " Removing Log Shipping for $DatabaseName "
304+ Write-Host " - Removing Log Shipping for $DatabaseName "
290305Get-RubrikLogShipping - PrimaryDatabaseName $DatabaseName - SecondaryDatabaseName $DatabaseName | Remove-RubrikLogShipping
0 commit comments