< Back

Set-DbaDbOwner

Mon Jan 13, 2020 5:35 pm

NAME Set-DbaDbOwner



SYNOPSIS

Sets database owners with a desired login if databases do not match that owner.





SYNTAX

Set-DbaDbOwner [[-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>] [[-SqlCredential]

<Pscredential>] [[-Database] <System.Object[]>] [[-ExcludeDatabase] <System.Object[]>] [[-InputObject]

<Microsoft.SqlServer.Management.Smo.Database[]>] [[-TargetLogin] <String>] [-EnableException <Switch>]

[<CommonParameters>]





DESCRIPTION

This function will alter database ownership to match a specified login if their current owner does not match the

target login. By default, the target login will be 'sa', but the function will allow the user to specify a

different login for ownership. The user can also apply this to all databases or only to a select list of

databases (passed as either a comma separated list or a string array).



Best Practice reference: http://weblogs.sqlteam.com/dang/archive ... ubles.aspx





PARAMETERS

-Database [<System.Object[]>]

Specifies the database(s) to process. Options for this list are auto-populated from the server. If

unspecified, all databases will be processed.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-EnableException [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ExcludeDatabase [<System.Object[]>]

Specifies the database(s) to exclude from processing. Options for this list are auto-populated from the server.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-InputObject [<Microsoft.SqlServer.Management.Smo.Database[]>]

Enables piping from Get-DbaDatabase



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlCredential [<Pscredential>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]

The target SQL Server instance or instances.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-TargetLogin [<String>]

Specifies the login that you wish check for ownership. This defaults to 'sa' or the sysadmin name if sa was

renamed. This must be a valid security principal which exists on the target server.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Tags: Database, Owner, DbOwner

Author: Michael Fal (@Mike_Fal), http://mikefal.net



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Set-DbaDbOwner -SqlInstance localhost



Sets database owner to 'sa' on all databases where the owner does not match 'sa'.

-------------------------- EXAMPLE 2 --------------------------



PS C:\\>Set-DbaDbOwner -SqlInstance localhost -TargetLogin DOMAIN\\account



Sets the database owner to DOMAIN\\account on all databases where the owner does not match DOMAIN\\account.

-------------------------- EXAMPLE 3 --------------------------



PS C:\\>Set-DbaDbOwner -SqlInstance sqlserver -Database db1, db2



Sets database owner to 'sa' on the db1 and db2 databases if their current owner does not match 'sa'.

-------------------------- EXAMPLE 4 --------------------------



PS C:\\>$db = Get-DbaDatabase -SqlInstance localhost -Database db1, db2



PS C:\\> $db | Set-DbaDbOwner -TargetLogin DOMAIN\\account



Sets database owner to 'sa' on the db1 and db2 databases if their current owner does not match 'sa'.



RELATED LINKS

https://dbatools.io/Set-DbaDbOwner