Quantcast
Channel: SQL Server Replication forum
Viewing all articles
Browse latest Browse all 4054

Powershell not scripting all options specified for replication

$
0
0

Hi all,

So I started writing a script, borrowing form two others people have posted, and it works pretty well. Meaning it almost completely matches the output I would get from scripting the replication out from SQL Server itself. However, there are a couple of things that are not showing up in the script, and I am curious if anyone knows why? The script is attached. The pieces I am curious about are:

Adding the option to IncludeUninstallDistributor or UninstallDistributor doesn't appear to add any code to actually do that. i.e. the below code is never added.

/****** Uninstalling the server as a Distributor. Script Date: 10/1/2015 4:34:13 PM ******/
use master
exec sp_dropdistributor @no_checks = 1, @ignore_distributor = 1
GO

Also, adding the reverse for the create script generation, IncludeInstallDistributor or InstallDistributor, does not add the below code.

use master
exec sp_adddistributor @distributor = N'xxx\xxx', @password = N''
GO

The purpose of the script is simply to script out all drop/create scripts for all publications and subscriptions, run the drop script, restore over the databases using backups of non-replicated databases, run the create replication script, and have it re-sync.

This is as far as I have gotten. Thoughts?

param ([string] $Publisher ,
       [string] $OutputFile = "C:\Temp\Repl-Backup.sql")

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Replication") | out-null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.RMO") | out-null

    [string] $Output = ""
    [int] $TranPubCnt = 0
    [int] $MergePubCnt = 0

    if ($Publisher -eq "" -or $Publisher -eq [string]::Empty)
    {
        $Publisher = read-host -prompt "Publisher?"
    }

    if (Test-Path $OutputFile)
    {
        Clear-Content $OutputFile
    }

    $ReplSvr = New-Object ("Microsoft.SqlServer.Replication.ReplicationServer") $Publisher"" > $OutputFile

    if ($ReplSvr.ReplicationDatabases.Count -eq 0)
    {
        $Output = "No replicated databases found for Publisher " + $Publisher
        $Output >> $OutputFile

        #return
    }

    foreach($Database in $ReplSvr.ReplicationDatabases)
    {
        $TranPubCnt = $TranPubCnt + $Database.TransPublications.Count
        $MergePubCnt = $MergePubCnt + $Database.MergePublications.Count
    }

    if (($TranPubCnt + $MergePubCnt) -eq 0)
    {
        $Output = "No Publications found for Publisher " + $Publisher
        $Output >> $OutputFile

        #return
    }

    # Drop

#    $ScriptOptions = [Microsoft.SqlServer.Replication.scriptoptions]::Deletion `
 #              -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeSubscriberSideSubscriptions `
  #             -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeArticles `
   #            -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludePublisherSideSubscriptions `
    #           -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeDisableReplicationDB `
     #          -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludePublicationAccesses `
      #         -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeGo `

    # Create
    $ScriptOptions = [Microsoft.SqlServer.Replication.ScriptOptions]::Creation `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeInstallDistributor `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::EnableReplicationDB `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludePublisherSideSubscriptions `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeCreateLogreaderAgent `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeCreateDistributionAgent `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeCreateMergeAgent `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeCreateSnapshotAgent `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludePublicationAccesses `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeArticles `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeSubscriberSideSubscriptions `
               -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeGo `


    foreach($Database in $ReplSvr.ReplicationDatabases)
    {
        if ($Database.TransPublications.Count -gt 0)
        {
            foreach($TranPub in $Daatbase.TransPublications)
            {
                [string] $PubScript = $TranPub.script($ScriptOptions)

                $PubScript >> $OutputFile"" >> $OutputFile
            }
        }
    }

    foreach($Database in $ReplSvr.ReplicationDatabases)
    {
        if ($Database.MergePublications.Count -gt 0)
        {
            foreach($MergePub in $Database.MergePublications)
            {
                [string] $PubScript = $MergePub.script($ScriptOptions)

                $PubScript >> $OutputFile"" >> $OutputFile
            }
        }
    }


John M. Couch


Viewing all articles
Browse latest Browse all 4054

Trending Articles