本文介绍了随机运行Sql作业步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PARAM
(
    [Parameter(Mandatory = $false)]
    [String]$HostName = (gc Env:\COMPUTERNAME),

    [Parameter(Mandatory = $false)]
    [String]$JobName = [String]::Empty
);

Write-Host "`nExecSqlServerAgentJob.ps1 v1.0ps1 Paul Williams ([email protected]) Sep. 2011";

if([String]::IsNullOrEmpty($JobName))
{
    Write-Host "`nNo job name provided.  Provide a job name using the -JobName parameter.`n";
    Exit;
}

Write-Host "`nLoading dependent assemblies and instantiating objects (this can take several seconds)...";

# Load SMO and instantiate the server object
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") |Out-Null;
[Microsoft.SqlServer.Management.Smo.Server]$sqlServer = New-Object Microsoft.SqlServer.Management.Smo.Server $HostName;

if([String]::IsNullOrEmpty($sqlServer.Urn))
{
    Write-Host "`nThe hostname provided is not a valid SQL Server instance.  Did you mistype the alias or forget to add the instance name?`n";
    Exit;
}
else
{ # This block uses rudimentary string manipulation to extract the actual host [+instance] of the
  # server.  This is especially useful if you use aliases.  This is for output purposes only.
    [String]$databaseInstance = $sqlServer.JobServer.Urn.Value.Substring($sqlServer.JobServer.Urn.Value.IndexOf("'") + 1, `
        $sqlServer.JobServer.Urn.Value.IndexOf("'", ($sqlServer.JobServer.Urn.Value.IndexOf("'")) `
            - ($sqlServer.JobServer.Urn.Value.IndexOf("'"))));
}

Write-Host "Enumerating jobs on server ...";
# Match the job name (if possible) from the list of jobs (readable by the user context)
[Microsoft.SqlServer.Management.Smo.Agent.Job]$job = ($sqlServer.JobServer.Jobs | ? { $_.Name -eq $JobName });

if($job -eq $null)
{
    Write-Host "`nNo such job on the server.`n";
    Exit;
}

# Finally invoke the job.  Use the job history (in SSMS) to verify the success of the job.
Write-Host "Executing job (the script doesn't wait for the job to finish)...`n`n";
$job.Start();

我有一个要求,我们可以

仅使用powershell脚本在SQL JOB中运行特定步骤。

示例

step1

step2

step3

step4

我想像随机选择一样运行step2和更高版本的步骤4.

我已经插入了启动Sql作业的代码块Powershell脚本。

请告诉我如何实施上述步骤要求。

谢谢

Kiran。

Phanikiran Pisipati

Phanikiran Pisipati

推荐答案


这篇关于随机运行Sql作业步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 21:55