functions/Get-DbaRepPublication.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
function Get-DbaRepPublication {
    <#
    .SYNOPSIS
    Displays all publications for a server or database.
 
    .DESCRIPTION
    Quickly find all transactional, merge, and snapshot publications on a specific server or database.
 
    .PARAMETER SqlInstance
    Allows you to specify a comma separated list of servers to query.
 
    .PARAMETER Database
    The database(s) to process. If unspecified, all databases will be processed.
 
    .PARAMETER SqlCredential
    Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
 
    .PARAMETER PublicationType
    Limit by specific type of publication. Valid choices include: Transactional, Merge, Snapshot
 
    .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.
 
    .NOTES
    Author: Colin Douglas
    Tags: Replication
 
    Website: https://dbatools.io
    Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
    License: MIT https://opensource.org/licenses/MIT
 
    .LINK
    https://dbatools.io/Get-DbaRepPublication
 
    .EXAMPLE
    Get-DbaRepPublication -SqlInstance sql2008, sqlserver2012
    Return all publications for servers sql2008 and sqlserver2012.
 
    .EXAMPLE
    Get-DbaRepPublication -SqlInstance sql2008 -Database TestDB
    Return all publications on server sql2008 for only the TestDB database
 
    .EXAMPLE
    Get-DbaRepPublication -SqlInstance sql2008 -PublicationType Transactional
    Return all publications on server sql2008 for all databases that have Transactional publications
#>

    [CmdletBinding()]
    param (
        [parameter(Mandatory, ValueFromPipeline)]
        [DbaInstanceParameter[]]$SqlInstance,
        [object[]]$Database,
        [PSCredential]$SqlCredential,
        [ValidateSet("Transactional", "Merge", "Snapshot")]
        [object[]]$PublicationType,
        [switch]$EnableException
    )

    process {

        foreach ($instance in $SqlInstance) {

            # Connect to Publisher
            try {
                Write-Message -Level Verbose -Message "Connecting to $instance"
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9
            }
            catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }

            $dbList = $server.Databases

            if ($Database) {
                $dbList = $dbList | Where-Object name -in $Database
            }

            $dbList = $dbList | Where-Object { ($_.ID -gt 4) -and ($_.status -ne "Offline") }


            foreach ($db in $dbList) {

                if (($db.ReplicationOptions -ne "Published") -and ($db.ReplicationOptions -ne "MergePublished")) {
                    Write-Message -Level Verbose -Message "Skipping $($db.name). Database is not published."
                }

                $repDB = Connect-ReplicationDB -Server $server -Database $db

                $pubTypes = $repDB.TransPublications + $repDB.MergePublications

                if ($PublicationType) {
                    $pubTypes = $pubTypes | Where-Object Type -in $PublicationType
                }

                foreach ($pub in $pubTypes) {

                    [PSCustomObject]@{
                        ComputerName = $server.ComputerName
                        InstanceName = $server.InstanceName
                        SqlInstance  = $server.SqlInstance
                        Server = $server.name
                        Database = $db.name
                        PublicationName = $pub.Name
                        PublicationType = $pub.Type
                    }
                }
            }
        }
    }
}