0

我正在使用下面的代码从运行良好的 SSIS 运行 ssis 作业,但我希望在运行作业后它应该等待作业完成然后继续

[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
{ 
    [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 ...";

[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 but it should)...`n`n";
$job.Start();

*$job.Start(); 之后如何等待 看到工作完成,以便我可以继续进行?

我正在执行下面的代码,但它不能正常工作,有什么帮助吗?

   [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")

$server = new-object "Microsoft.SqlServer.Management.Smo.Server" "ServerName"
$job = $server.JobServer.Jobs["YourJobName"]
$now = Get-Date
do
{
 Start-Sleep -Seconds 1
 $job.Refresh()
 $jobstat = $job | select CurrentRunStatus
 $stat = $jobstat.CurrentRunStatus
 WRITE-HOST $stat
}

while($stat -eq "Executing")

$job | select Name,CurrentRunStatus,LastRunDate
4

1 回答 1

0

启动 SQL 代理作业是一个异步事件。您无法将进度消息推送给您。正如 Tab Alleman 所评论的,您需要轮询 msdb 作业表以确定代理是否仍在运行。

也就是说,您是否需要通过代理启动作业?如果您只是在运行一个包,请使用 API 启动包并观察事件,直到它完成/失败/等。

于 2013-05-07T15:29:29.820 回答