commands

^

Set-DbaAgentJobOwner

Author Michael Fal (@Mike_Fal), http://mikefal.net
Availability Windows, Linux, macOS

 

Synopsis

Sets SQL Agent job owners with a desired login if jobs do not match that owner.

Description

This function alters SQL Agent Job 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 the user may specify a different login for ownership.

This be applied to all local (non MultiServer) jobs or only to a select collection of jobs.

Best practice reference: https://www.itprotoday.com/sql-server-tip-assign-ownership-jobs-sysadmin-account

If the 'sa' account was renamed, the new name will be used.

Syntax

Set-DbaAgentJobOwner
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-Job] <Object[]>]
    [[-ExcludeJob] <Object[]>]
    [[-InputObject] <Job[]>]
    [[-Login] <String>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Set-DbaAgentJobOwner -SqlInstance localhost

Sets SQL Agent Job owner to sa on all jobs where the owner does not match sa.

Example: 2
PS C:\> Set-DbaAgentJobOwner -SqlInstance localhost -Login DOMAIN\account

Sets SQL Agent Job owner to 'DOMAIN\account' on all jobs where the owner does not match 'DOMAIN\account'. Note
that Login must be a valid security principal that exists on the target server.

Example: 3
PS C:\> Set-DbaAgentJobOwner -SqlInstance localhost -Job job1, job2

Sets SQL Agent Job owner to 'sa' on the job1 and job2 jobs if their current owner does not match 'sa'.

Example: 4
PS C:\> 'sqlserver','sql2016' | Set-DbaAgentJobOwner

Sets SQL Agent Job owner to sa on all jobs where the owner does not match sa on both sqlserver and sql2016.

Example: 5
PS C:\> Get-DbaAgentJob -SqlInstance vmsql | Where-Object OwnerLoginName -eq login1 | Set-DbaAgentJobOwner -TargetLogin login2 | Out-Gridview

Sets SQL Agent Job owner to login2 where their current owner is login1 on instance vmsql. Send result to gridview.

Optional Parameters

-SqlInstance

The target SQL Server instance or instances.

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

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

Alias Jobs
Required False
Pipeline false
Default Value
-ExcludeJob

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

Alias
Required False
Pipeline false
Default Value
-InputObject

Enables piped input from Get-DbaAgentJob

Alias
Required False
Pipeline true (ByValue)
Default Value
-Login

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.

Alias TargetLogin
Required False
Pipeline false
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

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.

Alias wi
Required False
Pipeline false
Default Value
-Confirm

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

Alias cf
Required False
Pipeline false
Default Value

 

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