Wednesday, November 21, 2012

PowerShell Script to uninstall SharePoint solution pkg

Scenario: More often than not when I deliver solution pkg to test team or production, I create a quick PowerShell script that does the installation\uninstallation automatically. Having it here so that it can help me and others to quickly garb it.

Solution: Uninstall script:-

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

# assume the SolutionName.wsp is in the same directory as this script.

$ScriptDir = split-path -parent $MyInvocation.MyCommand.Path

$releaseDir = $ScriptDir + "\"

 

$SolutionID = "00000000-0000-00000-0000-000000000"

$Solution = get-spsolution | Where { ($SolutionID -eq $_.SolutionId) }

 

If($Solution -eq $null)

{ 

    Write-Host "Solution is not installed."

}

else

{

  #Check if solution is deployed in the farm

    If ($Solution.Deployed -match "True")

    {

        #Retract the  solution

        Write-Host "Retract Solution..."

        Uninstall-SPSolution -Identity $Solution

       

        # tell user to wait a few seconds for the retract to complete

        Read-Host "Wait 30 seconds for the solution to retract, then press Ok."

       

        # Remove the solution

        Write-Host "Remove Solution..."

        Remove-SPSolution -Identity $Solution

             

    }

    ElseIf($Solution.Deployed -match "False")

    {

        #retract the solution

        Write-Host "Removing Solution..."

        Remove-SPSolution -Identity $Solution       

    }
}
Install script:-

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

# assume the Solution.wsp is in the same directory as this script.

$ScriptDir = split-path -parent $MyInvocation.MyCommand.Path

$releaseDir = $ScriptDir + "\"

$SolutionId = "000000000-0000-0000-0000-00000000"

$Solution = Get-SPSolution | Where { ($SolutionId -eq $_.SolutionId) }

$wspFileName = "Solution.wsp"

$solutionFilePath   = $releaseDir + $wspFileName    

If($Solution -eq $null)

{

   # Add solution to the farm

    Write-Host "Adding solution to the farm"

    Add-SPSolution  $solutionFilePath

   

     #Deploy solution to the farm

    Write-Host "Deploying solution from " $solutionFilePath

    Install-SPSolution -Identity $wspFileName -GACDeployment 

}