Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7823001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:05:49+00:00 2026-06-02T08:05:49+00:00

I am writing a Powershell script that does several things with a local SQL

  • 0

I am writing a Powershell script that does several things with a local SQL Server database.

One thing I am doing is running several SQL jobs, one after another. I run them like this:

 sqlcmd -S .\ -Q "EXECUTE msdb.dbo.sp_start_job @job_name = 'Rebuild Content Asset Relationship Data'"

Is there a way to get Powershell to delay running the next job until the first one is completed?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:05:50+00:00Added an answer on June 2, 2026 at 8:05 am

    To get access to SQL Agent Jobs from PowerShell you can use SMO:

    EDIT: Thinking on efficiency if you are going to add this function to your script I would take the SMO loading out and just place it near the top of your script (prior to this function). It will probably slow your script down if every time you call the function it reloads the assembly.

    
    Function Get-SQLJobStatus
    {
        param ([string]$server, [string]$JobName)
        # Load SMO assembly, and if we're running SQL 2008 DLLs load the SMOExtended and SQLWMIManagement libraries
        [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
    
        # Create object to connect to SQL Instance
        $srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $server
    
        # used to allow piping of more than one job name to function
        if($JobName)
        {
            foreach($j in $jobName)
            {
                $srv.JobServer.Jobs | where {$_.Name -match $JobName} | Select Name, CurrentRunStatus
            }
        }
        else #display all jobs for the instance
        {
            $srv.JobServer.Jobs | Select Name, CurrentRunStatus
    } #end of Get-SQLJobStatus
    }

    Example of ways you could use this function:

    
    #will display all jobs on the instance
    Get-SQLJobStatus MyServer
    
    #pipe in more than one job to get status
    "myJob","myJob2" | foreach {Get-SQLJobStatus -Server MyServer -JobName $_}
    
    #get status of one job
    Get-SQLJobStatus -Server MyServer -JobName "MyJob"
    

    You could utilize this function in your script and just repeatedly call it in a while loop or something until your job status shows “Idle”. At least in my head that is what I think could work 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a PowerShell script that will back up several folders from my
I'm writing a powershell script to deploy a number of reports to SQL Server
I am have a powershell script that does a few things that all need
I'm writing a Powershell script that will extract a set of data files from
I am writing a powershell script that will act as a build compliance test
I'm writing a PowerShell script that will execute commands on a remote host using
I'm writing powershell script that simulates actions performed by user on the page. I
I'm writing a powershell v2 script that I'd like to run against a remote
I am writing PowerShell scripts to control SQL Server agent start/stop to ensure all
I'm writing a PowerShell script that uses the SharePoint 2010 New-SPWeb cmdlet to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.