2013-05-07 154 views
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 
+0

我不知道powershell,但不能写一个等待(睡眠)n秒的循环,然后查询作业历史记录表以查看作业是否完成?保持循环,直到获得作业完成的结果。 – 2013-05-07 14:57:37

+0

如果你有第一份工作必须等待完成的任务,为什么不把它作为工作中的第二项任务?与此同时 – HLGEM 2013-05-07 21:23:24

回答

0

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

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

+0

,我已编码此 #负载组件 [空隙] [System.Reflection.Assembly] :: LoadWithPartialName( “Microsoft.SqlServer.ConnectionInfo”) [空隙] [System.Reflection.Assembly] :: LoadWithPartialName( “Microsoft.SqlServer.Smo”) $ server = new-object“Microsoft.SqlServer.Management.Smo.Server”“server” $ job = $ server.JobServer.Jobs [$ JobName] $ now = Get-日期 $ jobstat = $ job |选择CurrentRunStatus 如果($ jobstat -eq'running'){ WRITE-HOST'作业正在运行'#YET TO CODE } – Zerotoinfinity 2013-05-07 15:32:51