I created this PowerShell script that waits until solution is retracted before removing and waits until a solution is properly added until it returns.
Theres still a couple of things i want to add to this, including proof of succesfull provisioning and support for dependent solutions, but it works for me.
function Provision-SPSolution
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[string]
$solutionPath
)
$solutionAbsolutePath = Resolve-Path($solutionPath) -EA 0 -EV err
if ($err)
{
Write-Host "`nException: `n$err" -ForegroundColor Yellow
break
}
$Error.Clear()
Write-Host "Trying to add solution $solutionAbsolutePath"
$solution = Microsoft.Sharepoint.Powershell\Add-SPSolution $solutionAbsolutePath -EA 0 -EV err
if ($err)
{
Write-Host "`nException: `n$err" -ForegroundColor Yellow
break
}
Write-Host "`nSolution added"
Microsoft.Sharepoint.Powershell\Install-SPSolution $solution.Name -GacDeployment
$timeout = 30 # seconds
$startTime = Get-Date
$endTime = $startTime.AddSeconds($timeout)
$timeSpan = New-TimeSpan $startTime $endTime
while ( $solution.JobExists -or ( $timeSpan -gt 0 ) )
{
$timeSpan = new-timespan $(get-date) $endTime
Write-Host "." -NoNewline
Sleep 1
}
Write-Host "`nSolution deployed"
}
function Unprovision-SPSolution
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[string]
$solutionName
)
$solution = Microsoft.Sharepoint.Powershell\Get-SPSolution | ?{$_.Name -eq $solutionName}
if (!$solution)
{
Write-Host "`nNo solution found with name $solutionName" -ForegroundColor Yellow
return
}
Write-Host "`nSolution found!"
if ($solution.Deployed)
{
Write-Host "`nSolution is deployed, so uninstalling..."
Microsoft.Sharepoint.Powershell\Uninstall-SPSolution $solutionName -Confirm:$false
$timeout = 30 # seconds
$startTime = Get-Date
$endTime = $startTime.AddSeconds($timeout)
while ( $solution.JobExists )
{
if ( $startTime -gt $endTime )
{
Write-Host "Timeout reached ($timeout s)"
break
}
Write-Host "." -NoNewline
Sleep 1
}
Write-Host "`nSolution uninstalled"
}
Microsoft.Sharepoint.Powershell\Remove-SPSolution $solutionName -Confirm:$false
Write-Host "`nSolution removed"
}