SQL Server Jobs, status, duration running, step

  • 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"

  • 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

  • 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"

  • 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

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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"

  • 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

  • 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"

  • 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

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply