How to execute agent job step in powershell?

wajika 21 Reputation points
2021-10-20T07:11:38.453+00:00

sql-server-management-objects-smo-programming-guide
smo-object-model-namespaces
using-smo-with-powershell-to-obtain-sql-server-job-information

I want to execute a agent job step through powershell, but I am a newbie for SQL server. I read the document and have some basic understanding, but I still haven't solved the problem.

$srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server "BI-DEV-DB"
$jb = $srv.JobServer.Jobs
$jb.Start("LSD_SC") <<< This is modified according to using-smo-with-powershell-to-obtain-sql-server-job-information, but it didn't work

$jp = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobStep
$jp.OnSuccessAction(QuitWithSuccess)
$jp.OnSuccessStep(QuitWithSuccess) << I do not understand the role and usage of OnSuccessAction and OnSuccessStep, but I think these two functions are helpful to me

$jr = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Agent.JobServer
$jr.Jobs.Start("LSD_SC") <<< According to my understanding, is there a start event in Microsoft.SqlServer.Management.Smo.Agent.JobServer?

Can anyone give me some help?

SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
Windows for business | Windows Server | User experience | PowerShell
SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Olaf Helper 47,586 Reputation points
    2021-10-20T08:12:15.367+00:00

    why that complicated, use DbaTools CmdLet => Start-DbaAgentJob


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.