Friday, 23 August 2013

How to Make sure a Solution is added before Activating it

How to Make sure a Solution is added before Activating it

When you deploy .wsp solutions and  you script your “Add-SpSolution  and Install-SpSolution”  in a row, PowerShell might throw an error because the Solution didn’t finish uploading on the farm before you try to activate it.
Here is a example of a do-while loop to make sure that the solution you are adding is fully uploaded and ready to be deployed before deploying it.
If you use farm solutions, use SPSolution instead of SpUserSolution
$mainurl = “http://vlad.test.loc”
Write-Host -ForegroundColor White ” – Adding and Installing vladsolution…” -NoNewline
Add-SPUserSolution -LiteralPath C:solutionsvladsolution.wsp -Site $mainurl
$ErrorActionPreference = “silentlycontinue”
do
{
Write-Host “.” -NoNewline -ForeGroundColor White;
Start-Sleep -Seconds 5; 
try
{
$testsolution = Get-SPUserSolution -Identity  vladsolution .wsp -Site $mainurl
}
catch
{}
}while(!$testsolution);

$ErrorActionPreference = “stop”
Install-SPUserSolution -Identity  vladsolution .wsp -Site $mainurl
Write-Host -ForegroundColor GREEN “Done.” 
Write-Host 
Say Thanks if it helped :)

No comments:

Post a Comment