Answered Scripting replication objects

  • Wednesday, October 10, 2007 10:33 AM
     
     

     

    Hi,

    I'm trying to script a publisher's publications and subscribers.

    I've got this far, but when I call the script method I just get a blank string back. What am I doing wrong?

     

    Code Block

    Dim srv As New Microsoft.SqlServer.Management.Smo.Server("MYSERVER")
    Dim script_output As String
    Console.Write("Connected to servr " + srv.Name)
    script_output = srv.ReplicationServer.DistributionPublishers(0).Script(Microsoft.SqlServer.Replication.ScriptOptions.Creation & Microsoft.SqlServer.Replication.ScriptOptions.IncludeAll)

     

     

    The server I'm connecting to is the distributor.

    TIA,

    moff.

All Replies

  • Thursday, October 11, 2007 9:41 AM
     
     Answered

    Got it sussed - I should have been scripting against the ReplicationServer object of the Publisher .

     

    For interest, here's my working code:

     

    Code Block (Powershell)

    # RNM |

    #-----+

     

    [reflection.assembly]::LoadwithPartialName("Microsoft.SQLServer.SMO") | out-Null
    [reflection.assembly]::LoadwithPartialName("Microsoft.SQLServer.RMO") | out-Null

     

    $publisher = 'MYSERVER'
    $pub_svr = New-Object 'Microsoft.sqlserver.management.smo.server' $publisher

     

    $pub_svr.ReplicationServer.Script(( `
             [Microsoft.SqlServer.Replication.scriptoptions]::Creation `
       -bor  [Microsoft.SqlServer.Replication.scriptoptions]::IncludeAll `
       -bxor [Microsoft.SqlServer.Replication.scriptoptions]::IncludeReplicationJobs `
    )) > "c:\work\replication_script.txt"

     

    Note that you have to exclude ReplicationJobs if your Publisher is not your Distributor (otherwise the scripting falls over in a heap when SMO can't find the Replication Job on the Publisher )