Friday, October 28, 2011

PowerShell: Add-SPSolution -force when wsp is already deployed

[What you have]:
A 2010 SharePoint Farm. A WSP file that you want to deploy. 

[What you want]:
You want to make sure that a wsp file will be deployed even the solution is already installed.

[What you want to know]: 
To add a solution to the farm - make use of Add-SPSolution. Notice that the Add-SPSolution cmdlet returns the SPSolution object. That means that you can pipe the result  - an added wsp - into Install-SPSolution
-LiteralPath - must be the full path.  You can make use of Resolve-Path to get a a full path from a relative

$path= Resolve-Path .\custom.wsp
$wsp = Add-SPSolution -LiteralPath $path

-local attribute in Install-SPSolution means that the wsp file will be deployed only on the server where the command gets run. That means if farm has more than one web front, all others will not get files installed on their SharePoint root. (what is the SharePoint - read on Best Practices 2010)

-GACDeployment . If wsp has a dll to put into GAC, but the parameter is no specified, an error will be returned. The same true for CASPolicies.

Remember that should do only one deployment at a time. The same true for re-traction.
To check the status of the deployment - use $wsp.Deployed property.

Uninstall-SPSolution will retract a wsp file. Remember to deactivate features first if you are not planing to deploy the wsp or a new version doesn't contain those features. Otherwise, you will leave those features in an invalid state.

Remove-SPSolution to delete the wsp file from the farm. The Farm solution must have been fully retracted before deletion.

Upgrade-SPSolution - will upgrade a solution. This is an alternative for retracting and uninstalling a wsp file before adding and installing a new version. Be mindful that Upgrade-SPSolution works only if you haven't  added a new feature\module to your solution. 


Do you want to know whether wsp will trigger IIS reset? Look at a attribute ResetWebServer in the Solution tag in the manifest.xml

[What you want to do]:
Run the following PowerShell script to deploy a wsp when the wsp is already deployed on the farm: PowerShell Script for SP2010: Add-SPSolution -force when wsp is already deployed

A quick note:
start-sleep ,in my case, helped me to avoid an error:
The solution cannot be removed when a job is scheduled or running

I haven't investigated the true nature of the error and its meaning.
The line Uninstall-SPSolution $wsp -AllWebApplications   -Confirm:$false generates an error:
Cannot uninstall the LanguagePack 0 because it is not deployed.
But still the code executes normally and adds the solution. Again, I haven't investigated the cause of the error. I appreciate your comments\feedback on that matter.

[What you want to consider]:
To get more skilled with PowerShell in SharePoint 2010 - highly recommend to read a book from Gary Lapointe.
Buy the book to support my effort in sharing the experience  through amazon associates:
Automating SharePoint 2010 with Windows PowerShell 2.0