Powershell run SQL job, delay, then run the next one

前端 未结 4 1111
南旧
南旧 2021-01-22 19:40

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 t

相关标签:
4条回答
  • 2021-01-22 19:49

    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 讨论(0)
  • 2021-01-22 19:53

    My suggestion would be to wrap your job in a new sproc that starts the job then waits for it to finish by continually polling its status. From the attached article, you can do something like this:

    DECLARE @JobStatus INT
    SET @JobStatus = 0
    EXEC MSDB.dbo.sp_start_job @Job_Name = 'JobName'
    SELECT @JobStatus = current_execution_status  FROM OPENROWSET('SQLNCLI', 'Server=localhost;Trusted_Connection=yes;', 
                              'EXEC MSDB.dbo.sp_help_job @job_name = ''JobName'', @job_aspect = ''JOB'' ')     
    WHILE @JobStatus <> 4
    BEGIN   
        SELECT @JobStatus = current_execution_status  FROM OPENROWSET('SQLNCLI', 'Server=localhost;Trusted_Connection=yes;', 
                                    'EXEC MSDB.dbo.sp_help_job @job_name = ''JobName'', @job_aspect = ''JOB'' ')    
    END
    

    Then, rather than calling sp_start_job from the command line, call your sproc from the command line and PowerShell will be blocked until that sproc finishes. Hope this helps!

    0 讨论(0)
  • 2021-01-22 20:04

    Sure, execute a Start-Sleep -seconds <nn> between invocations of sqlcmd.exe.

    0 讨论(0)
  • 2021-01-22 20:10

    Your job is executed by SQL Server Agent. Either you call the corresponding stored proc (or SQL Statements) directly from your PowerShell script, or you implement something in the line of what is explained here.

    0 讨论(0)
提交回复
热议问题