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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,691 questions
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.
1,244 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 40,741 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