等待任务运行? /任务完成后跳转下一行?

时间:2012-04-10 14:07:07

标签: windows sharepoint powershell powershell-v2.0

我看到很多关于powershell +等待进程结束的主题,但不知何故它对我不起作用。我有这个:

Add-PsSnapin Microsoft.SharePoint.Powershell

Add-SPSolution sol.wsp

Install-SPSolution -identity $sol -GACDeployment

Install-SPFeature "feature"

我想要做的是添加一个新的Sharepoint(2010)解决方案,然后我尝试安装解决方案,最后我尝试安装功能。

最后一个失败,因为解决方案的安装需要更长时间,但他已经在尝试安装该功能。如果我再次启动脚本,则会收到错误Install-SPFeature : Failed to find the XML file at location,可以安装该功能。我怎么能改变我的脚本来处理这个问题?确定我可以使用Start-Sleep -s 2或其他东西,但这不是最好的方法。 | Out-Null-Wait也不起作用。我认为这是因为该过程或其他任何已经完成的过程,但是Windows需要几秒钟才能意识到安装了该解决方案。有任何想法吗?谢谢

3 个答案:

答案 0 :(得分:2)

以下是我使用的部署脚本的片段:

Write-Host "Deploying solution: $SolutionPackageName"
$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionPackageName) -and ($_.Deployed -eq $false)}
Install-SPSolution -Identity $SolutionPackageName -GACDeployment -Confirm:$false -force
$index = 0 
[DateTime] $startingTime = [DateTime]::Now.AddMinutes(2) 
while($Solution.JobExists) 
{ 
    $index++ 
    if($startingTime -lt [DateTime]::Now) 
    { 
        Write-Host "Deployment job: $SolutionPackageName failed. Deployed = $Solution.Deployed, Index = $index"
        break 
    } 
    Write-Host "Deployment job: $SolutionPackageName is still running. Deployed = $Solution.Deployed, Index = $index"
    Start-Sleep -s 5 
    $Solution = Get-SPSolution | ? {$_.Name -eq $SolutionPackageName}
} 
Write-Host "Deploying solution: $SolutionPackageName - Done."

是的,它使用Start-Sleep,但我认为脚本通过检查解决方案实际部署的时间以智能方式使用它。就个人而言,我喜欢屏幕上的反馈,知道脚本没有挂起。我从来没有遇到超过2分钟的情况,但我肯定已经显示了10个“仍在运行”的消息。

答案 1 :(得分:1)

您是否尝试过使用Start-Job?

$sb = { Install-SPSolution -identity $sol -GACDeployment }
$job = start-job -scriptblock $sb
Wait-Job $job  | Out-Null
$retMsg = Receive-Job $job

答案 2 :(得分:0)

您可以将Start-Process-Wait选项一起使用:

Start-Process Powershell "Install-SPSolution -identity $sol -GACDeployment" -Wait

Install-SPFeature "feature"