Tuesday, April 10, 2012

Wait while task running? / Jump next line when task done?

I saw a lot of topics about powershell + waiting for process ending, but somehow its not working for me. I have this:


Add-PsSnapin Microsoft.SharePoint.Powershell
 
 Add-SPSolution sol.wsp
 
 Install-SPSolution -identity $sol -GACDeployment
 
 Install-SPFeature "feature"
 


What Im trying to do is to add a new Sharepoint (2010) solution, then Im trying to install a solution and finally Im trying to install feature.


The last one is failing, because the installation of the solution takes somehow longer but he is already trying to install the feature. I get the error Install-SPFeature : Failed to find the XML file at location if I start the script again the feature can be installed. How could I change my script to handle this problem? Ok sure I could use Start-Sleep -s 2 or something, but thats not the best way. | Out-Null or -Wait isnt working too. I think thats because the process or whatever it is already done, but windows takes some seconds to realize that the solution is installed. Any ideas? Thank you



Answer:

Here's a snippet from the deployment script I use:
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."
Yes, it uses Start-Sleep, but I think the script uses it in a smart way by checking when the solution is actually deployed. Personally, I like the feedback on screen to know that the script is not hung. I've never had a case where it's exceeded 2 minutes, but I've definitely had it display up to 10 "still running" messages.

No comments:

Post a Comment