< Back

Set-DbaAgentJobOwner

Mon Jan 13, 2020 5:26 pm

NAME Set-DbaAgentJobOwner



SYNOPSIS

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





SYNTAX

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

[[-SqlCredential] <Pscredential>] [[-Job] <System.Object[]>] [[-ExcludeJob] <System.Object[]>] [[-InputObject]

<Microsoft.SqlServer.Management.Smo.Agent.Job[]>] [[-Login] <String>] [-EnableException <Switch>]

[<CommonParameters>]





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 jobs or only to a select

collection of jobs.



Best practice reference: http://sqlmag.com/blog/sql-server-tip-a ... in-account



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





PARAMETERS

-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



-ExcludeJob [<System.Object[]>]

Specifies the job(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.Agent.Job[]>]

Enables piped input from Get-DbaAgentJob



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Job [<System.Object[]>]

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

jobs will be processed.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Login [<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



-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



<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: Agent, Job

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



RELATED LINKS

https://dbatools.io/Set-DbaAgentJobOwner