Set‑SqlDscDatabaseOwner - dsccommunity/SqlServerDsc GitHub Wiki

SYNOPSIS

Sets the owner of a database in a SQL Server Database Engine instance.

SYNTAX

ServerObjectSet (Default)

Set-SqlDscDatabaseOwner -ServerObject <Server> -Name <String> [-Refresh] -OwnerName <String>
 [-DropExistingUser] [-Force] [-PassThru] [-WhatIf] [-Confirm]
 [<CommonParameters>]

DatabaseObjectSet

Set-SqlDscDatabaseOwner -DatabaseObject <Database> -OwnerName <String> [-DropExistingUser] [-Force] [-PassThru]
 [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION

This command sets the owner of a database in a SQL Server Database Engine instance.

The owner must be a valid login on the SQL Server instance. The command uses the SetOwner() method on the SMO Database object to change the ownership.

EXAMPLES

EXAMPLE 1

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
Set-SqlDscDatabaseOwner -ServerObject $serverObject -Name 'MyDatabase' -OwnerName 'sa'

Sets the owner of the database named MyDatabase to sa.

EXAMPLE 2

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
Set-SqlDscDatabaseOwner -ServerObject $serverObject -Name 'MyDatabase' -OwnerName 'sa' -DropExistingUser

Sets the owner of the database named MyDatabase to sa, dropping any existing user account mapped to the sa login before changing the owner.

EXAMPLE 3

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$databaseObject = $serverObject | Get-SqlDscDatabase -Name 'MyDatabase'
Set-SqlDscDatabaseOwner -DatabaseObject $databaseObject -OwnerName 'DOMAIN\SqlAdmin' -Force

Sets the owner of the database using a database object without prompting for confirmation.

EXAMPLE 4

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
Set-SqlDscDatabaseOwner -ServerObject $serverObject -Name 'MyDatabase' -OwnerName 'sa' -PassThru

Sets the owner and returns the updated database object.

PARAMETERS

-DatabaseObject

Specifies the database object to modify (from Get-SqlDscDatabase).

Type: Database
Parameter Sets: DatabaseObjectSet
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False

-DropExistingUser

Specifies whether to drop any existing database users mapped to the specified login before changing the owner. This is required if a non-dbo user account already exists for the login being set as the new owner.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Force

Specifies that the database owner should be modified without any confirmation.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Name

Specifies the name of the database to modify.

Type: String
Parameter Sets: ServerObjectSet
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-OwnerName

Specifies the name of the login that should be the owner of the database.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-PassThru

Specifies that the database object should be returned after modification.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Refresh

Specifies that the ServerObject's databases should be refreshed before trying to get the database object. This is helpful when databases could have been modified outside of the ServerObject, for example through T-SQL. But on instances with a large amount of databases it might be better to make sure the ServerObject is recent enough.

This parameter is only used when setting owner using ServerObject and Name parameters.

Type: SwitchParameter
Parameter Sets: ServerObjectSet
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-ServerObject

Specifies current server connection object.

Type: Server
Parameter Sets: ServerObjectSet
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Confirm

Prompts you for confirmation before running the cmdlet.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

Microsoft.SqlServer.Management.Smo.Database

The database object to modify (from Get-SqlDscDatabase).

OUTPUTS

None.

When PassThru is specified the output is [Microsoft.SqlServer.Management.Smo.Database].

NOTES

RELATED LINKS

⚠️ **GitHub.com Fallback** ⚠️