I'm trying to create a custom function that can uninstall and reinstall any SharePoint solution.
Some solutions requires the -GACDeployment switch, some requires the -AllWebApplications and some requires the -WebApplication switch.
How can I properly handle this multiple combinations?
By now, I have a function that works well with the two first possibilities (Gac and all web applications). But I having some troubles to make the multiple web application possibles.
Here is a sample of how the function should be called:
# Already working :
Update-SPSolutionWithCompleteReinstall .\SolutionwithGac.wsp
Update-SPSolutionWithCompleteReinstall .\SolutionwithWebRes.wsp -AllWebApplications
Update-SPSolutionWithCompleteReinstall .\SolutionwithWebRes.wsp -WebApplications "http://app1"
$webApp = Get-SPWebApplication "http://app1"
Update-SPSolutionWithCompleteReinstall .\SolutionwithWebRes.wsp -WebApplications $webApp
# Not working :
Update-SPSolutionWithCompleteReinstall .\SolutionwithWebRes.wsp -WebApplications "http://app1","http://app2"
$webApp1 = Get-SPWebApplication "http://app1"
$webApp2 = Get-SPWebApplication "http://app2"
Update-SPSolutionWithCompleteReinstall .\SolutionwithWebRes.wsp -WebApplications $webApp1,$webApp2
And here is the function itself :
function Update-SPSolutionWithCompleteReinstall
{
[CmdLetBinding(DefaultParametersetName="NoWebApplication")]
param(
[Parameter(Mandatory=$true,ParameterSetName="NoWebApplication", Position=0)]
[Parameter(Mandatory=$true,ParameterSetName="WebApplications", Position=0)]
[Parameter(Mandatory=$true,ParameterSetName="AllWebApplications", Position=0)]
[string]$filePath,
[Parameter(ParameterSetName="AllWebApplications", Position=1)]
[switch]$AllWebApplications,
[Parameter(ParameterSetName="WebApplications", Position=1)]
[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]$WebApplications
)
$filePath = Resolve-Path $filePath
$wspFileName = Split-Path -leaf $filePath
$currentSol = Get-SPSolution | ? { $_.Name -match $wspFileName }
# 1. Uninstall the solution
if($currentSol)
{
Write-Host "The solution $($currentSol.Name) has been found. Uninstalling it"
if($currentSol.ContainsWebApplicationResource)
{
Uninstall-SPSolution $currentSol -AllWebApplications -Confirm:$false
}
else
{
Uninstall-SPSolution $currentSol -Confirm:$false
}
Wait-SPSolutionDeploymentJobToFinish $currentSol.Name
Write-Host "Remove $($currentSol.Name) from the solution store"
Remove-SPSolution $currentSol -Confirm:$false
}
# 2. install and deploy the new solution
Write-Host "Adding the solution $filePath"
$newSol = Add-SPSolution -LiteralPath $filePath
$expr = 'Install-SPSolution $newSol'
if($newSol.ContainsWebApplicationResource)
{
switch ($PsCmdlet.ParameterSetName)
{
"AllWebApplications" {
$expr += " -AllWebApplications"
}
"WebApplications" {
$expr += " -WebApplication $($WebApplications.Read().Url)"
}
}
}
if($newSol.ContainsGlobalAssembly)
{
$expr += ' -GACDeployment'
}
Write-Host "Install the solution $filePath"
Invoke-Expression -Command $expr
Wait-SPSolutionDeploymentJobToFinish $newSol.Name
}
The specific part of the function where the trouble is located, is where I'm building a custom expression to be invoked.