Click here to monitor SSC
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
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 601
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
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8291 Visits: 14368
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
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 601
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
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8291 Visits: 14368
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-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45474 Visits: 39948
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
- Win
- Win
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 601
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
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8291 Visits: 14368
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
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 601
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
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8291 Visits: 14368
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