functionSet-DbaDbOwner{<#
.SYNOPSIS
Sets database owners with a desired login if databases do not match that owner.

.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).

.PARAMETER Database
Specifies the database(s) to process. Options for this list are auto-populated from the server. If unspecified, all databases will be processed.

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

.PARAMETER TargetLogin
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.

.PARAMETER WhatIf
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.

.PARAMETER Confirm
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.

.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.

# dynamic sa name for orgs who have changed their sa nameif(!$TargetLogin){$TargetLogin=($server.logins|Where-Object{$_.id-eq1}).Name}

#Validate loginif(($server.Logins.Name)-notcontains$TargetLogin){Stop-Function-Message"$TargetLogin is not a valid login on $instance. Moving on."-Continue-EnableException$EnableException}

#Owner cannot be a group$TargetLoginObject=$server.Logins|where-object{$PSItem.Name-eq$TargetLogin}|Select-Object-propertyName,LoginTypeif($TargetLoginObject.LoginType-eq'WindowsGroup'){Stop-Function-Message"$TargetLogin is a group, therefore can't be set as owner. Moving on."-Continue-EnableException$EnableException}

#Get database list. If value for -Database is passed, massage to make it a string array.#Otherwise, use all databases on the instance where owner not equal to -TargetLogin#use where owner and target login do not match#exclude system dbs$dbs=$server.Databases|Where-Object{$_.IsAccessible-and$_.Owner-ne$TargetLogin-and@('master','model','msdb','tempdb','distribution')-notcontains$_.Name}

#filter collection based on -Databases/-Exclude parametersif($Database){$dbs=$dbs|Where-Object{$Database-contains$_.Name}}

#Database is online and accessibleif($db.Status-notmatch'Normal'){Write-Message-LevelWarning-Message"$dbname on $instance is in a $($db.Status) state and can not be altered. It will be skipped."}#Database is updatable, not read-onlyelseif($db.IsUpdateable-eq$false){Write-Message-LevelWarning-Message"$dbname on $instance is not in an updateable state and can not be altered. It will be skipped."}#Is the login mapped as a user? Logins already mapped in the database can not be the ownerelseif($db.Users.name-contains$TargetLogin){Write-Message-LevelWarning-Message"$dbname on $instance has $TargetLogin as a mapped user. Mapped users can not be database owners."}else{$db.SetOwner($TargetLogin)[PSCustomObject]@{ComputerName=$server.ComputerNameInstanceName=$server.ServiceNameSqlInstance=$server.DomainInstanceNameDatabase=$dbOwner=$TargetLogin}}}catch{Stop-Function-Message"Failure updating owner."-ErrorRecord$_-Target$instance-Continue}}}}}end{Test-DbaDeprecation-DeprecatedOn"1.0.0"-EnableException:$false-AliasSet-DbaDatabaseOwner}}