forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-DbaAgDatabase.ps1
92 lines (71 loc) · 4.24 KB
/
Get-DbaAgDatabase.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
#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle#
function Get-DbaAgDatabase {
<#
.SYNOPSIS
Gets availability group databases from a SQL Server instance.
.DESCRIPTION
Gets availability group databases from a SQL Server instance.
Default view provides most common set of properties for information on the database in an availability group.
Information returned on the database will be specific to that replica, whether it is primary or a secondary.
.PARAMETER SqlInstance
The target SQL Server instance or instances. Server version must be SQL Server version 2012 or higher.
.PARAMETER SqlCredential
Login to the SqlInstance instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
.PARAMETER AvailabilityGroup
Specify the availability groups to query.
.PARAMETER Database
Specify the database or databases to return. This list is auto-populated from the server for tab completion. Multiple databases can be specified. If none are specified all databases will be processed.
.PARAMETER InputObject
Enables piped input from Get-DbaAvailabilityGroup.
.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: Hadr, HA, AG, AvailabilityGroup, Replica
Author: Shawn Melton (@wsmelton), https://wsmelton.github.io
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Get-DbaAgDatabase
.EXAMPLE
PS C:\> Get-DbaAgDatabase -SqlInstance sql2017a
Returns all the databases in each availability group found on sql2017a
.EXAMPLE
PS C:\> Get-DbaAgDatabase -SqlInstance sql2017a -AvailabilityGroup AG101
Returns all the databases in the availability group AG101 on sql2017a
.EXAMPLE
PS C:\> Get-DbaAvailabilityGroup -SqlInstance sqlcluster -AvailabilityGroup SharePoint -Database Sharepoint_Config | Get-DbaAgDatabase
Returns the database Sharepoint_Config found in the availability group SharePoint on server sqlcluster
#>
[CmdletBinding()]
param (
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[string[]]$AvailabilityGroup,
[string[]]$Database,
[parameter(ValueFromPipeline)]
[Microsoft.SqlServer.Management.Smo.AvailabilityGroup[]]$InputObject,
[switch]$EnableException
)
process {
if ($SqlInstance) {
$InputObject += Get-DbaAvailabilityGroup -SqlInstance $SqlInstance -SqlCredential $SqlCredential -AvailabilityGroup $AvailabilityGroup
}
foreach ($db in $InputObject.AvailabilityDatabases) {
if ($Database) {
if ($db.Name -notin $Database) { continue }
}
$server = $db.Parent.Parent
Add-Member -Force -InputObject $db -MemberType NoteProperty -Name ComputerName -value $server.ComputerName
Add-Member -Force -InputObject $db -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
Add-Member -Force -InputObject $db -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
Add-Member -Force -InputObject $db -MemberType NoteProperty -Name Replica -value $server.ComputerName
Add-Member -Force -InputObject $db -MemberType NoteProperty -Name DatabaseName -value $db.Name # for backwards compat
Add-Member -Force -InputObject $db -MemberType NoteProperty -Name AvailabilityGroup -value $db.Parent.Name
$defaults = 'ComputerName', 'InstanceName', 'SqlInstance', 'AvailabilityGroup', 'Replica', 'Name', 'SynchronizationState', 'IsFailoverReady', 'IsJoined', 'IsSuspended'
Select-DefaultView -InputObject $db -Property $defaults
}
}
}