Get SQL Agent Job stepID

Welcome Forums General PowerShell Q&A Get SQL Agent Job stepID

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
2 years, 4 months ago.

  • Author
    Posts
  • #49733

    Participant
    Points: 0
    Rank: Member

    Hi again guys!!!

    Now this time I would like to get the SQL Agent Job stepID from powershell. I only have the StepName and I would like to get information from the Job History as follow:

    write-host "Name: $($job.JobSteps[$stepID].Name) RunDate: $($job.JobSteps[$stepID].LastRunDate) Status: $($job.JobSteps[$stepID].LastRunOutCome)"

    Can I get this information using the stepname?

  • #49737

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    It looks like $stepID contains the step ID, doesn't it?

  • #49743

    Participant
    Points: 0
    Rank: Member

    Well... I have to get it somehow!!
    I have this working...
    Example:
    write-host "Name: $($job.JobSteps[1].Name) RunDate: $($job.JobSteps[1].LastRunDate) Status: $($job.JobSteps[1].LastRunOutCome)"

    However, in my program, the user is entering the StepName only. I tried the following:
    $stepID = $job.CurrentRunStep
    write-host "StepID = $steID"

    But it gives me the following:
    StepID = 0 (unknown)

  • #49745

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    You'd have to query that from SQL Server. Sorry, I thought you had $stepID. I'm not sure how you'd query that – it's really a SQL Server question, not PowerShell. Again, sorry.

  • #50603

    Participant
    Points: 0
    Rank: Member

    I found it!!
    $JobStep = $Job.JobSteps[$StepName]
    Write-Host "Name: $($JobStep.Name) RunDate: $($JobStep.LastRunDate) Status: $($JobStep.LastRunOutCome)"

The topic ‘Get SQL Agent Job stepID’ is closed to new replies.