SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Server Jobs, status, duration running, step


SQL Server Jobs, status, duration running, step

Author
Message
- Win
- Win
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 707
Hello,

I need a help to pull information about SQL Server Agent jobs running on multiple instaces. (I am using Powershell)

I require: ServerName, JobName, Status (running, stopped, cancelled), Duration (if running from how long, was that hanged etc), step it is executing etc.

Since in my environment I am facing the Differential backups hanged for long time (days). Is there any way to monitor than Iam trying to.
Same thing with ReOrg jobs as well in the weekends.

Thanks in advance..

Cheers,
- Win
"Dont Judge a Book by its Cover"
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38336 Visits: 14411
This might help get you started:

$Instances = (Get-Content 'C:\Instances.txt') # Make a text file containing your instances, one on each line

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null

foreach ($instance in $Instances)
{
$sqlServer = New-Object "Microsoft.SqlServer.Management.Smo.Server" $Instance
$sqlAgent = $sqlServer.JobServer

if ($sqlAgent -eq $null)
{
Write-Error "Could not get SMO SQL Agent object for $Instance, permissions issue?"
}

foreach($agentJob in $sqlAgent.Jobs)
{
'Name: ' + $agentJob.Name
'CurrentRunStatus: ' + $agentJob.CurrentRunStatus
'CurrentRunStep: ' + $agentJob.CurrentRunStep
'LastRunDate: ' + $agentJob.LastRunDate
}
}


SMO Job Class

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
- Win
- Win
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 707
Thanks for your fast response and help.

I used the PS script and ran today. I received only a single column results with numbers. Might be I missed something.
Is there any TSQL way to get that requested report. Like backup job running from 26:34:45 hrs etc like..
If I found a job running more than 24 hrs, I will kill the process and rerun to success in weekends.


Thanks in advance.

Cheers,
- Win
"Dont Judge a Book by its Cover"
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38336 Visits: 14411
Did you create the text file with a list of your instances?

This is what you should see:

Name: Job Name 1
CurrentRunStatus: Idle
CurrentRunStep: 0 (unknown)
LastRunDate: 03/30/2013 18:53:11
Name: Job Name n
CurrentRunStatus: Idle
CurrentRunStep: 0 (unknown)
LastRunDate: 03/30/2013 18:53:11
.
.
.
Name: syspolicy_purge_history
CurrentRunStatus: Idle
CurrentRunStep: 0 (unknown)
LastRunDate: 04/02/2013 19:25:50


You could pull the answer back, likely with a single query, using T-SQL and Invoke-SqlCmd by referencing the job tables in msdb directly.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)SSC Guru (209K reputation)

Group: General Forum Members
Points: 209583 Visits: 41973
opc.three (4/10/2013)
This might help get you started:

$Instances = (Get-Content 'C:\Instances.txt') # Make a text file containing your instances, one on each line

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null

foreach ($instance in $Instances)
{
$sqlServer = New-Object "Microsoft.SqlServer.Management.Smo.Server" $Instance
$sqlAgent = $sqlServer.JobServer

if ($sqlAgent -eq $null)
{
Write-Error "Could not get SMO SQL Agent object for $Instance, permissions issue?"
}

foreach($agentJob in $sqlAgent.Jobs)
{
'Name: ' + $agentJob.Name
'CurrentRunStatus: ' + $agentJob.CurrentRunStatus
'CurrentRunStep: ' + $agentJob.CurrentRunStep
'LastRunDate: ' + $agentJob.LastRunDate
}
}


SMO Job Class


Very cool, Orlando. Good link, too! Book Marked.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
- Win
- Win
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 707
Yes, please..
I have created a text file with list of servers (I am using powershell for the same list).
I could see only single column in result set, converted to CSV file.
Length
23
24
25
26

I have been using a query in my environment, is this the best one, any changes or better options than my below query, but its not giving results for latest, SQL Server 2012:
Please suggest.

------ for how long the JOB is running --------------
CREATE TABLE #enum_job
(
Job_ID uniqueidentifier,
Last_Run_Date INT,
Last_Run_Time INT,
Next_Run_Date INT,
Next_Run_Time INT,
Next_Run_Schedule_ID INT,
Requested_To_Run INT,
Request_Source INT,
Request_Source_ID VARCHAR(100),
Running INT,
Current_Step INT,
Current_Retry_Attempt INT,
State INT
)


INSERT INTO
#enum_job EXEC master.dbo.xp_sqlagent_enum_jobs 1, garbage


SELECT
R.name ,
R.last_run_date,
R.RunningForTime,
GETDATE()AS now
FROM
#enum_job a
INNER JOIN
(
SELECT
j.name,
J.JOB_ID,
ja.run_requested_date AS last_run_date,
(DATEDIFF(mi,ja.run_requested_date,GETDATE())) AS RunningFor,
CASE LEN(CONVERT(VARCHAR(5),DATEDIFF(MI,JA.RUN_REQUESTED_DATE,GETDATE())/60))
WHEN 1 THEN '0' + CONVERT(VARCHAR(5),DATEDIFF(mi,ja.run_requested_date,GETDATE())/60)
ELSE CONVERT(VARCHAR(5),DATEDIFF(mi,ja.run_requested_date,GETDATE())/60)
END
+ ':' +
CASE LEN(CONVERT(VARCHAR(5),(DATEDIFF(MI,JA.RUN_REQUESTED_DATE,GETDATE())%60)))
WHEN 1 THEN '0'+CONVERT(VARCHAR(5),(DATEDIFF(mi,ja.run_requested_date,GETDATE())%60))
ELSE CONVERT(VARCHAR(5),(DATEDIFF(mi,ja.run_requested_date,GETDATE())%60))
END
+ ':' +
CASE LEN(CONVERT(VARCHAR(5),(DATEDIFF(SS,JA.RUN_REQUESTED_DATE,GETDATE())%60)))
WHEN 1 THEN '0'+CONVERT(VARCHAR(5),(DATEDIFF(ss,ja.run_requested_date,GETDATE())%60))
ELSE CONVERT(VARCHAR(5),(DATEDIFF(ss,ja.run_requested_date,GETDATE())%60))
END AS RunningForTime
FROM
msdb.dbo.sysjobactivity AS ja
LEFT OUTER JOIN msdb.dbo.sysjobhistory AS jh
ON
ja.job_history_id = jh.instance_id
INNER JOIN msdb.dbo.sysjobs_view AS j
ON
ja.job_id = j.job_id
WHERE
(
ja.session_id =
(
SELECT
MAX(session_id) AS EXPR1
FROM
msdb.dbo.sysjobactivity
)
)
)
R ON R.job_id = a.Job_Id
AND a.Running = 1
DROP TABLE #enum_job

Cheers,
- Win
"Dont Judge a Book by its Cover"
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38336 Visits: 14411
sp_help_job might be a better option for you.

I looked into this once before when writing a synchronous version of sp_start_job and I think the trouble you're going to face, whether you go with T-SQL or PowerShell, is going to be figuring out when an executing job, and the step that is currently executing within that job, actually started executing. You can look at dbo.sysjobactivity but it's messy and I could not see where it was exposed in SMO as an object property.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
- Win
- Win
SSChasing Mays
SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)SSChasing Mays (600 reputation)

Group: General Forum Members
Points: 600 Visits: 707
Thanks much for your reply, anymore suggestions or changes on the script, that I have been using ?

It wont result for SQL Server 2012 and R2 versions.
Any help on that please..

Cheers,
- Win
"Dont Judge a Book by its Cover"
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38336 Visits: 14411
You're using an undocumented proc. See if the documented proc I mentioned will be more helpful or research differences in the interface for the one you are using on 2012.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search