commands

^

Get-DbaDbSchema

Author Adam Lancaster https://github.com/lancasteradam
Availability Windows, Linux, macOS

 

Synopsis

Finds the database schema SMO object(s) based on the given filter params.

Description

Finds the database schema SMO object(s) based on the given filter params.

Syntax

Get-DbaDbSchema
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-Database] <String[]>]
    [[-Schema] <String[]>]
    [[-SchemaOwner] <String[]>]
    [-IncludeSystemDatabases]
    [-IncludeSystemSchemas]
    [[-InputObject] <Database[]>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Get-DbaDbSchema -SqlInstance localhost

Gets all non-system database schemas from all user databases on the localhost instance. Note: the dbo schema is a system schema and won't be included in the output from this example. To include the
dbo schema specify -IncludeSystemSchemas

Example: 2
PS C:\> Get-DbaDbSchema -SqlInstance localhost -Schema dbo -IncludeSystemSchemas

Returns the dbo schema from the databases on the localhost instance.

Example: 3
PS C:\> Get-DbaDbSchema -SqlInstance localhost -IncludeSystemDatabases -IncludeSystemSchemas

Gets all database schemas from all databases on the localhost instance.

Example: 4
PS C:\> Get-DbaDbSchema -SqlInstance localhost -Schema TestSchema

Finds and returns the TestSchema schema from the localhost instance.

Example: 5
PS C:\> Get-DbaDbSchema -SqlInstance localhost -SchemaOwner DBUser1

Finds and returns the schemas owned by DBUser1 from the localhost instance.

Example: 6
PS C:\> Get-DbaDbSchema -SqlInstance localhost -Database TestDB -SchemaOwner DBUser1

Finds and returns the schemas owned by DBUser1 in the TestDB database from the localhost instance.

Example: 7
PS C:\> $schema = Get-DbaDbSchema -SqlInstance localhost -Database TestDB -Schema TestSchema
PS C:\> $schema.Owner = DBUser2
PS C:\> $schema.Alter()

Finds the TestSchema in the TestDB on the localhost instance and then changes the schema owner to DBUser2

Example: 8
PS C:\> $schema = Get-DbaDbSchema -SqlInstance localhost -Database TestDB -Schema TestSchema
PS C:\> $schema.Drop()

Finds the TestSchema in the TestDB on the localhost instance and then drops it. Note: to drop a schema all objects must be transferred to another schema or dropped.

Example: 9
PS C:\> $db = Get-DbaDatabase -SqlInstance localhost -Database TestDB
PS C:\> $schema = $db | Get-DbaDbSchema -Schema TestSchema

Finds the TestSchema in the TestDB which is passed via pipeline into the Get-DbaDbSchema command.

Optional Parameters

-SqlInstance

The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.

Alias
Required False
Pipeline false
Default Value
-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported. For MFA support, please use Connect-DbaInstance.

Alias
Required False
Pipeline false
Default Value
-Database

The target database(s).

Alias
Required False
Pipeline false
Default Value
-Schema

The name(s) of the schema(s)

Alias
Required False
Pipeline false
Default Value
-SchemaOwner

The name(s) of the database user(s) that own(s) the schema(s).

Alias
Required False
Pipeline false
Default Value
-IncludeSystemDatabases

Include the system databases.

Alias
Required False
Pipeline false
Default Value False
-IncludeSystemSchemas

Include the system schemas.

Alias
Required False
Pipeline false
Default Value False
-InputObject

Allows piping from Get-DbaDatabase.

Alias
Required False
Pipeline true (ByValue)
Default Value
-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.

Alias
Required False
Pipeline false
Default Value False
-WhatIf

Shows what would happen if the command were to run. No actions are actually performed.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

Prompts you for confirmation before executing any changing operations within the command.

Alias cf
Required False
Pipeline false
Default Value

 

Want to see the source code for this command? Check out Get-DbaDbSchema on GitHub.
Want to see the Bill Of Health for this command? Check out Get-DbaDbSchema.