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”
Write-Host “.” -NoNewline -ForeGroundColor White;
Start-Sleep -Seconds 5;
$testsolution = Get-SPUserSolution -Identity vladsolution .wsp -Site $mainurl
$ErrorActionPreference = “stop”
Install-SPUserSolution -Identity vladsolution .wsp -Site $mainurl
Write-Host -ForegroundColor GREEN “Done.”
Say Thanks if it helped 🙂