functions/Get-DbaClusterNode.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
function Get-DbaClusterNode {
    <#
        .SYNOPSIS
            Returns the node(s) of a SQL Cluster.
 
        .DESCRIPTION
            Returns the name of the current node(s) in the SQL Server cluster.
 
            If the -ActiveNode Parameter is passed it only returns the name of the Server currently hosting the clustered instance.
 
        .PARAMETER SqlInstance
            Specifies the SQL Server clustered instance to check.
 
        .PARAMETER SqlCredential
            Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
 
        .PARAMETER ActiveNode
            If this parameter is selected the cmdlet will only return the Active Node in the cluster.
 
        .PARAMETER Detailed
            Output all properties, will be deprecated in 1.0.0 release.
 
        .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
            Tags: Cluster, WSFC, FCI
            Website: https://dbatools.io
            Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
            License: MIT https://opensource.org/licenses/MIT
 
        .LINK
            https://dbatools.io/Get-DbaClusterNode
 
        .EXAMPLE
            Get-DbaClusterNode -SqlInstance sqlcluster
 
            Returns all nodes in the cluster and details about each node.
 
        .EXAMPLE
            Get-DbaClusterNode -SqlInstance sqlcluster -ActiveNode
 
            Returns the name of the active node in the cluster
 
    #>

    param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter]$SqlInstance,
        [PSCredential]$SqlCredential,
        [switch]$ActiveNode,
        [switch]$Detailed,
        [alias('Silent')]
        [switch]$EnableException
    )

    begin {
        Test-DbaDeprecation -DeprecatedOn 1.0.0 -Parameter Detailed
        Test-DbaDeprecation -DeprecatedOn 1.0.0 -Alias Get-DbaClusterActiveNode
        $server = Connect-SqlInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential -MinimumVersion 10
    }

    process {
        if ($server.IsClustered -eq $false) {
            Stop-Function -Message "Not a clusterd instance." -Category ConnectionError -ErrorRecord $_ -Target $SqlInstance -Continue
        }

        # If the -ActiveNode switch is selected only the primary node is returned.
        if ($ActiveNode) {
            try {
                $sql = "SELECT * FROM sys.dm_os_cluster_nodes where is_current_owner = 1"
                $datatable = $server.query($sql)

                [PSCustomObject]@{
                    ComputerName      = $datatable.nodename
                    InstanceName      = $server.ServiceName
                    SqlInstance       = $server.DomainInstanceName
                    Status            = $datatable.Status
                    StatusDescription = $datatable.StatusDescription
                    CurrentOwner      = $datatable.is_current_owner
                } | Select-DefaultView -Property ComputerName
            }
            catch {
                Stop-Function -Message "Unable to query sys.dm_os_cluster_nodes on $server." -ErrorRecord $_ -Target $SqlInstance -Continue
            }
        }
        #Default Execution of this function
        else {
            try {
                $sql = "SELECT * FROM sys.dm_os_cluster_nodes"
                $datatable = $server.query($sql)

                foreach ($data in $datatable) {
                    [PSCustomObject]@{
                        ComputerName      = $data.nodename
                        InstanceName      = $server.ServiceName
                        SqlInstance       = $server.DomainInstanceName
                        Status            = $data.Status
                        StatusDescription = $data.StatusDescription
                        CurrentOwner      = $data.is_current_owner
                    } | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, StatusDescription, CurrentOwner
                }
            }
            catch {
                Stop-Function -Message "Unable to query sys.dm_os_cluster_nodes on $server." -ErrorRecord $_ -Target $SqlInstance -Continue
            }
        }
    }
}