Remove, Add and Deploy Single solution

12/01/2012 00:46

#Uninstall and remove one solution, later adds new one
$webAppUrl="https://td_sp_ui"
$solutionName=read-host "Input solution name to remove (e. g. sitecollection.wsp)"
$solution=Get-SPSolution | Where-Object {$_.Name -eq $solutionName}
if($solution -eq $null)
    {
    write-host "Solution " $solution " doesn't exist."
    }
else #if solution exists
{
  Start-SPAssignment -Global;
  if($solution.Deployed)
    {
        write-host "Uninstalling Solution " $solution.Name;
        if($solution.DeployedWebApplications.Count -gt 0)
            {
                Uninstall-SPSolution $solution –AllWebApplications -Local:$Local -Confirm:$false;
            }
        else
           {
                Uninstall-SPSolution $solution -Local:$Local -Confirm:$false;
            }
        do {
            Start-Sleep 5;
            $solution = Get-SPSolution $solution;
            }
        while($solution.JobExists -and $solution.Deployed)
    }
  write-host "Removing Solution " $solution.Name;
  Remove-SPSolution -Confirm:$false
  write-host "Solution " $solutionName " was succesfuly removed."
  Stop-SPAssignment -Global;
}

read-host "Replace your package in C:\GBP.Install folder and when you are ready, press Enter to deploy it..."

$sourceDir="C:\GBP.Install\" + $solutionName
write-host "Adding " $solutionName " solution..."
Add-SPSolution $sourceDir
write-host "Solution " $solutionName " was succesfuly added."
write-host "Deploying " $solutionName " solution..."
Install-SPSolution –Identity $solutionName –WebApplication $webAppUrl -GACDeployment -force
write-host "Solution " $solutionName " was succesfuly deployed."