Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Server Jobs, status, duration running, step Expand / Collapse
Author
Message
Posted Wednesday, April 10, 2013 1:24 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:45 PM
Points: 119, Visits: 490
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"
Post #1440677
Posted Wednesday, April 10, 2013 3:46 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:21 AM
Points: 7,125, Visits: 12,721
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
Post #1441051
Posted Friday, April 12, 2013 8:26 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:45 PM
Points: 119, Visits: 490
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"
Post #1441997
Posted Saturday, April 13, 2013 6:16 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:21 AM
Points: 7,125, Visits: 12,721
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
Post #1442028
Posted Saturday, April 13, 2013 1:23 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:53 PM
Points: 35,366, Visits: 31,905
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1442050
Posted Saturday, April 13, 2013 6:47 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:45 PM
Points: 119, Visits: 490
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"
Post #1442072
Posted Sunday, April 14, 2013 11:12 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:21 AM
Points: 7,125, Visits: 12,721
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
Post #1442117
Posted Monday, April 15, 2013 7:55 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:45 PM
Points: 119, Visits: 490
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"
Post #1442308
Posted Monday, April 15, 2013 8:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:21 AM
Points: 7,125, Visits: 12,721
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
Post #1442326
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse