2012-04-10 166 views
2

我看到很多有关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需要几秒钟才能意识到该解决方案已安装。有任何想法吗?谢谢

回答

1

您是否尝试使用Start-Job?

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

嗯,这个我不能做任何事情,我得到的消息:'术语'Install-SPSolution'不被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果路径包含012dded,请验证路径是否正确,然后重试。 + CategoryInfo:ObjectNotFound:(Install-SPSolution:String)[],CommandNotFoundException + FullyQualifiedErrorId:CommandNotFoundException' – sabisabi 2012-04-10 14:55:46

+0

这仅仅是因为你必须在作为工作开始的scriptblock内添加snappin。你也打电话给工作传递参数。 – JPBlanc 2012-04-10 15:23:59

0

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

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

Install-SPFeature "feature" 
+0

刚刚意识到这可能不适合你的情况,因为你需要做一个'Add-PSSnapin'。好吧... – 2012-04-10 15:12:05

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个“仍在运行”的消息。