Quantcast
Channel: SQL Server Setup & Upgrade forum
Viewing all articles
Browse latest Browse all 7696

SQL Server Job Owner Change

$
0
0

Hi All,

I have a requirement from my client wherein i have to change the job owner from 'SA' to 'DBA_ADMIN' for all the jobs which have currently 'SA' as the owner. Also, I have to make the changes on nealry 100 Servers.

Could some one please share the powershell or the batch file method which can be used to change the owner of all jobs across 100 servers?

Below is the query which i have currently to change the job owner from 'SA' to 'DBA_ADMIN', but i don't want to connect to each and every server and run the qiuery, instead want to perform the task in an automated way.

DECLARE @name_holder VARCHAR(1000)

DECLARE My_Cursor CURSOR

FOR

SELECT [name] FROM msdb..sysjobs

WHERE suser_sname(owner_sid) = 'domain\user' –only change jobs that had a specific owner

OPEN My_Cursor

FETCH NEXT FROM My_Cursor INTO @name_holder

WHILE (@@FETCH_STATUS <> -1)

BEGIN

EXEC msdb..sp_update_job

@job_name = @name_holder,

@owner_login_name = 'sa'

FETCH NEXT FROM My_Cursor INTO @name_holder

END

CLOSE My_Cursor

DEALLOCATE My_Cursor

Thanks in Advance.

Regards, Kranthi


Viewing all articles
Browse latest Browse all 7696

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>