# Get SQL Agent Job stepID

**URL:** https://forums.powershell.org/t/get-sql-agent-job-stepid/6953
**Category:** PowerShell Help
**Created:** [August 9, 2016, 9:17am UTC](https://forums.powershell.org/t/get-sql-agent-job-stepid/6953 "2016-08-09T09:17:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![m-chalut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/m-chalut/32/606_2.png) [@m-chalut](https://forums.powershell.org/u/m-chalut)
#### Post date: [August 9, 2016, 9:17am UTC](https://forums.powershell.org/t/get-sql-agent-job-stepid/6953/1 "2016-08-09T09:17:47Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)
#### Post date: [August 9, 2016, 9:22am UTC](https://forums.powershell.org/t/get-sql-agent-job-stepid/6953/2 "2016-08-09T09:22:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![m-chalut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/m-chalut/32/606_2.png) [@m-chalut](https://forums.powershell.org/u/m-chalut)
#### Post date: [August 9, 2016, 9:46am UTC](https://forums.powershell.org/t/get-sql-agent-job-stepid/6953/3 "2016-08-09T09:46:45Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)
#### Post date: [August 9, 2016, 10:26am UTC](https://forums.powershell.org/t/get-sql-agent-job-stepid/6953/4 "2016-08-09T10:26:06Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![m-chalut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/m-chalut/32/606_2.png) [@m-chalut](https://forums.powershell.org/u/m-chalut)
#### Post date: [August 16, 2016, 8:36am UTC](https://forums.powershell.org/t/get-sql-agent-job-stepid/6953/5 "2016-08-16T08:36:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/get-sql-agent-job-stepid/6953/6 "2024-05-16T20:40:43Z")

</div>


