2012-04-10 164 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個「仍在運行」的消息。