functions/Get-DbaDbFileGroup.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
113
114
115
116
117
118
119
function Get-DbaDbFileGroup {
    <#
    .SYNOPSIS
        Returns a summary of information on filegroups
 
    .DESCRIPTION
        Shows information around filegroups.
 
    .PARAMETER SqlInstance
        The target SQL Server instance or instances. This can be a collection and receive pipeline input.
 
    .PARAMETER SqlCredential
        Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
 
    .PARAMETER Database
        The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will be processed.
 
   .PARAMETER InputObject
        Database object piped in from Get-DbaDatabase
 
    .PARAMETER FileGroup
        Define a specific FileGroup you would like to query.
 
    .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: Database, FileGroup
        Author: Patrick Flynn (@sqllensman)
 
        Website: https://dbatools.io
        Copyright: (c) 2018 by dbatools, licensed under MIT
        License: MIT https://opensource.org/licenses/MIT
 
    .LINK
        https://dbatools.io/Get-DbaDbFileGroup
 
    .EXAMPLE
        PS C:\> Get-DbaDbFileGroup -SqlInstance sql2016
 
        Return all FileGroups for all databases on instance sql2016
 
    .EXAMPLE
        PS C:\> Get-DbaDbFileGroup -SqlInstance sql2016 -Database MyDB
 
        Return all FileGroups for database MyDB on instance sql2016
 
    .EXAMPLE
        PS C:\> Get-DbaDbFileGroup -SqlInstance sql2016 -FileGroup Primary
 
        Returns information on filegroup called Primary if it exists in any database on the server sql2016
 
    .EXAMPLE
        PS C:\> 'localhost','localhost\namedinstance' | Get-DbaDbFileGroup
 
        Returns information on all FileGroups for all databases on instances 'localhost','localhost\namedinstance'
 
    .EXAMPLE
        PS C:\> 'localhost','localhost\namedinstance' | Get-DbaDbFileGroup
 
        Returns information on all FileGroups for all databases on instances 'localhost','localhost\namedinstance'
 
    .EXAMPLE
        PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress,SQL2 -ExcludeDatabase model,master | Get-DbaDbFileGroup
 
        Returns information on all FileGroups for all databases except model and master on instances SQL1\SQLExpress,SQL2
    #>

    [CmdletBinding()]
    param ([parameter(ValueFromPipeline)]
        [DbaInstanceParameter[]]$SqlInstance,
        [PSCredential]$SqlCredential,
        [object[]]$Database,
        [parameter(ValueFromPipeline)]
        [Microsoft.SqlServer.Management.Smo.Database[]]$InputObject,
        [string[]]$FileGroup,
        [switch]$EnableException
    )

    process {
        if (Test-Bound -not 'SqlInstance', 'InputObject') {
            Write-Message -Level Warning -Message "You must specify either a SQL instance or supply an InputObject"
            return
        }

        if (Test-Bound -Not -ParameterName InputObject) {
            $InputObject += Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database
        }

        foreach ($db in $InputObject) {
            if ($db.IsAccessible) {
                Write-Message -Level Verbose -Message "Processing database: $db"
                $server = $db.Parent
                if (Test-Bound -ParameterName Database) {
                    $db = $db | Where-Object { $Database -contains $_.Name }
                }
                $fileGroups = $db.Filegroups

                if (Test-Bound -ParameterName Filegroup) {
                    $fileGroups = $fileGroups | Where-Object { $Filegroup -contains $_.Name }
                }

                foreach ($fg in $fileGroups) {
                    Write-Message -Level Verbose -Message "Processing filegroup $($fg.Name)"
                    $fg | Add-Member -Force -MemberType NoteProperty -Name ComputerName -Value $server.ComputerName
                    $fg | Add-Member -Force -MemberType NoteProperty -Name InstanceName -Value $server.ServiceName
                    $fg | Add-Member -Force -MemberType NoteProperty -Name SqlInstance -Value $server.DomainInstanceName

                    $defaultprops = "ComputerName", "InstanceName", "SqlInstance", "Parent", "FileGroupType", "Name", "Size"

                    Select-DefaultView -InputObject $fg -Property $defaultprops
                }
            } else {
                Write-Message -Level Verbose -Message "Skipping processing of database: $db as database is not accessible"
            }
        }
    }
}