|
| 1 | +function Get-DbaDbDataClassification { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Retrieves data classification information for columns in SQL Server databases |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Retrieves data classification labels stored as extended properties on table columns. Data classification |
| 8 | + is used to tag sensitive data columns with information type and sensitivity labels, which helps with |
| 9 | + compliance, data governance, and security auditing. |
| 10 | +
|
| 11 | + Classification metadata is stored as four extended properties on each classified column: |
| 12 | + - sys_information_type_id: GUID identifying the information type |
| 13 | + - sys_information_type_name: Human-readable information type name (e.g., "Financial", "Health", "Credentials") |
| 14 | + - sys_sensitivity_label_id: GUID identifying the sensitivity label |
| 15 | + - sys_sensitivity_label_name: Human-readable sensitivity label (e.g., "Public", "General", "Confidential") |
| 16 | +
|
| 17 | + These properties are compatible with Microsoft Information Protection (MIP) labels used by SQL Server |
| 18 | + Data Discovery & Classification in SSMS and Azure SQL Database. |
| 19 | +
|
| 20 | + Requires SQL Server 2005 or later due to use of sys.extended_properties. |
| 21 | +
|
| 22 | + .PARAMETER SqlInstance |
| 23 | + The target SQL Server instance or instances. |
| 24 | +
|
| 25 | + .PARAMETER SqlCredential |
| 26 | + Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). |
| 27 | +
|
| 28 | + Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - |
| 29 | + Integrated are all supported. |
| 30 | +
|
| 31 | + For MFA support, please use Connect-DbaInstance. |
| 32 | +
|
| 33 | + .PARAMETER Database |
| 34 | + Specifies which databases to search for data classifications. Only applies when connecting directly via SqlInstance. |
| 35 | +
|
| 36 | + .PARAMETER Schema |
| 37 | + Filters results to columns in the specified schema(s). |
| 38 | +
|
| 39 | + .PARAMETER Table |
| 40 | + Filters results to columns in the specified table(s). |
| 41 | +
|
| 42 | + .PARAMETER Column |
| 43 | + Filters results to the specified column name(s). |
| 44 | +
|
| 45 | + .PARAMETER InputObject |
| 46 | + Accepts database objects piped from Get-DbaDatabase. |
| 47 | +
|
| 48 | + .PARAMETER EnableException |
| 49 | + By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. |
| 50 | + This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. |
| 51 | + Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch. |
| 52 | +
|
| 53 | + .NOTES |
| 54 | + Tags: DataClassification, Classification, Compliance, Security |
| 55 | + Author: the dbatools team + Claude |
| 56 | +
|
| 57 | + Website: https://dbatools.io |
| 58 | + Copyright: (c) 2024 by dbatools, licensed under MIT |
| 59 | + License: MIT https://opensource.org/licenses/MIT |
| 60 | +
|
| 61 | + .LINK |
| 62 | + https://dbatools.io/Get-DbaDbDataClassification |
| 63 | +
|
| 64 | + .OUTPUTS |
| 65 | + PSCustomObject |
| 66 | +
|
| 67 | + Returns one object per classified column with the following properties: |
| 68 | + - ComputerName: The computer name of the SQL Server instance |
| 69 | + - InstanceName: The SQL Server instance name |
| 70 | + - SqlInstance: The full SQL Server instance name |
| 71 | + - Database: The database name |
| 72 | + - Schema: The schema name of the table |
| 73 | + - Table: The table name |
| 74 | + - Column: The column name |
| 75 | + - InformationTypeId: GUID identifying the information type |
| 76 | + - InformationType: Human-readable information type name |
| 77 | + - SensitivityLabelId: GUID identifying the sensitivity label |
| 78 | + - SensitivityLabel: Human-readable sensitivity label name |
| 79 | +
|
| 80 | + .EXAMPLE |
| 81 | + PS C:\> Get-DbaDbDataClassification -SqlInstance sql2019 |
| 82 | +
|
| 83 | + Returns all data classifications across all databases on sql2019. |
| 84 | +
|
| 85 | + .EXAMPLE |
| 86 | + PS C:\> Get-DbaDbDataClassification -SqlInstance sql2019 -Database AdventureWorks |
| 87 | +
|
| 88 | + Returns all data classifications in the AdventureWorks database. |
| 89 | +
|
| 90 | + .EXAMPLE |
| 91 | + PS C:\> Get-DbaDbDataClassification -SqlInstance sql2019 -Database AdventureWorks -Table Customer |
| 92 | +
|
| 93 | + Returns data classifications for columns in the Customer table. |
| 94 | +
|
| 95 | + .EXAMPLE |
| 96 | + PS C:\> Get-DbaDatabase -SqlInstance sql2019 -Database AdventureWorks | Get-DbaDbDataClassification |
| 97 | +
|
| 98 | + Returns all data classifications in AdventureWorks by piping the database object. |
| 99 | +
|
| 100 | + .EXAMPLE |
| 101 | + PS C:\> Get-DbaDbDataClassification -SqlInstance sql2019 -Database AdventureWorks | Where-Object SensitivityLabel -eq "Highly Confidential" |
| 102 | +
|
| 103 | + Returns only columns classified as Highly Confidential in AdventureWorks. |
| 104 | + #> |
| 105 | + [CmdletBinding()] |
| 106 | + param ( |
| 107 | + [DbaInstanceParameter[]]$SqlInstance, |
| 108 | + [PSCredential]$SqlCredential, |
| 109 | + [string[]]$Database, |
| 110 | + [string[]]$Schema, |
| 111 | + [string[]]$Table, |
| 112 | + [string[]]$Column, |
| 113 | + [parameter(ValueFromPipeline)] |
| 114 | + [Microsoft.SqlServer.Management.Smo.Database[]]$InputObject, |
| 115 | + [switch]$EnableException |
| 116 | + ) |
| 117 | + process { |
| 118 | + if ($SqlInstance) { |
| 119 | + $InputObject = Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database | Where-Object IsAccessible |
| 120 | + } |
| 121 | + |
| 122 | + foreach ($db in $InputObject) { |
| 123 | + $server = $db.Parent |
| 124 | + |
| 125 | + if ($server.VersionMajor -lt 9) { |
| 126 | + Stop-Function -Message "Data classification requires SQL Server 2005 or later. Skipping $server" -Target $server -Continue |
| 127 | + } |
| 128 | + |
| 129 | + $sql = " |
| 130 | +SELECT |
| 131 | + SCHEMA_NAME(o.schema_id) AS SchemaName, |
| 132 | + o.name AS TableName, |
| 133 | + c.name AS ColumnName, |
| 134 | + CAST(ep1.value AS NVARCHAR(MAX)) AS InformationTypeId, |
| 135 | + CAST(ep2.value AS NVARCHAR(MAX)) AS InformationType, |
| 136 | + CAST(ep3.value AS NVARCHAR(MAX)) AS SensitivityLabelId, |
| 137 | + CAST(ep4.value AS NVARCHAR(MAX)) AS SensitivityLabel |
| 138 | +FROM sys.objects o |
| 139 | +INNER JOIN sys.columns c ON o.object_id = c.object_id |
| 140 | +LEFT JOIN sys.extended_properties ep1 |
| 141 | + ON ep1.major_id = o.object_id AND ep1.minor_id = c.column_id |
| 142 | + AND ep1.name = 'sys_information_type_id' AND ep1.class = 1 |
| 143 | +LEFT JOIN sys.extended_properties ep2 |
| 144 | + ON ep2.major_id = o.object_id AND ep2.minor_id = c.column_id |
| 145 | + AND ep2.name = 'sys_information_type_name' AND ep2.class = 1 |
| 146 | +LEFT JOIN sys.extended_properties ep3 |
| 147 | + ON ep3.major_id = o.object_id AND ep3.minor_id = c.column_id |
| 148 | + AND ep3.name = 'sys_sensitivity_label_id' AND ep3.class = 1 |
| 149 | +LEFT JOIN sys.extended_properties ep4 |
| 150 | + ON ep4.major_id = o.object_id AND ep4.minor_id = c.column_id |
| 151 | + AND ep4.name = 'sys_sensitivity_label_name' AND ep4.class = 1 |
| 152 | +WHERE o.type = 'U' |
| 153 | + AND (ep1.value IS NOT NULL OR ep2.value IS NOT NULL OR ep3.value IS NOT NULL OR ep4.value IS NOT NULL) |
| 154 | +ORDER BY SCHEMA_NAME(o.schema_id), o.name, c.name" |
| 155 | + |
| 156 | + try { |
| 157 | + $results = $db.Query($sql) |
| 158 | + } catch { |
| 159 | + Stop-Function -Message "Error querying data classifications in $($db.Name) on $server" -ErrorRecord $_ -Target $db -Continue |
| 160 | + } |
| 161 | + |
| 162 | + foreach ($row in $results) { |
| 163 | + if ($Schema -and $row.SchemaName -notin $Schema) { continue } |
| 164 | + if ($Table -and $row.TableName -notin $Table) { continue } |
| 165 | + if ($Column -and $row.ColumnName -notin $Column) { continue } |
| 166 | + |
| 167 | + [PSCustomObject]@{ |
| 168 | + ComputerName = $db.ComputerName |
| 169 | + InstanceName = $db.InstanceName |
| 170 | + SqlInstance = $db.SqlInstance |
| 171 | + Database = $db.Name |
| 172 | + Schema = $row.SchemaName |
| 173 | + Table = $row.TableName |
| 174 | + Column = $row.ColumnName |
| 175 | + InformationTypeId = $row.InformationTypeId |
| 176 | + InformationType = $row.InformationType |
| 177 | + SensitivityLabelId = $row.SensitivityLabelId |
| 178 | + SensitivityLabel = $row.SensitivityLabel |
| 179 | + DatabaseObject = $db |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments