Powershell Script that will deploy/upgrade Farm Based Solution to all Site Collections

This Powershell script can be used to deploy/upgrade your farm solutions to all the site collections in the farm.

# SharePoint DLL


[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")



#Cmdlet Install

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue



###REGION FUNCTIONS



#####################################################################

#Adds a farm solution to a site.

#####################################################################

function AddSolution()

{

$myError = $null



Add-SPSolution -LiteralPath $Solution -ErrorAction:SilentlyContinue -ErrorVariable myError -Confirm:$false



if ($myError -ne $null)

{

write-host -ForegroundColor 'red' $myError



$myError = $null

}

else

{

write-host -ForegroundColor 'green' "Sucessfuly added the solution to the site"

}

}



#####################################################################

#Deploys a farm solution to a site.

#####################################################################

function DeploySolution()

{

$myError = $null

Install-SPSolution -Identity $SolutionName -ErrorAction:SilentlyContinue -ErrorVariable myError -Confirm:$false -Force:$true -GACDeployment:$true



if ($myError -ne $null)

{

write-host -ForegroundColor 'red' $myError



$myError = $null

}

else

{

write-host -ForegroundColor 'green' "Sucessfuly activated the solution to the site"

}

}



######################################################################

#Upgrades a existing solution

#######################################################################

function UpgradeSolution()

{

$myError = $null



Update-SPSolution -Identity $SolutionName -LiteralPath $Solution -ErrorAction:SilentlyContinue -ErrorVariable myError -Confirm:$false -GACDeployment:$true



if ($myError -ne $null)

{

write-host -ForegroundColor 'red' "Upgrade Solution Error: " $myError



$myError = $null

}

else

{

write-host -ForegroundColor 'green' 'Sucessfuly upgraded the solution.'

}

}



##ENDREGION FUNCTIONS



$SolutionPath = ""

$SolutionName = ""

$solution = ""



##Get the location of the wsp for the solution

write-host -ForegroundColor 'red' "`nEnter the location of the WSP file (example: c:\deployment\farmSolutions\)"

$SolutionPath = read-host



##Get the solution name

write-host -ForegroundColor 'red' "`nEnter the solution name (example: myfirstsolution.wsp)"

$SolutionName = read-host



$Solution = [System.String]::Concat($SolutionPath, $SolutionName)



$myError = $null

##check to see if the solution is already present

$addedSolution = Get-SPSolution -Identity $SolutionName -ErrorAction:SilentlyContinue -ErrorVariable myError



if ($MyError -eq $null)

{

write-host -ForegroundColor 'yellow' $myError

}



if ($addedSolution -ne $null)

{

write-host -ForegroundColor 'red' "`n Solution already present in the site: " $SiteURL



$upgradeSolution = ""

write-host -ForegroundColor 'red' "`n Upgrade Solution (y/n)"

$upgradeSolution = read-host



if ($upgradeSolution -eq 'y')

{

#upgrade the existing solution.

UpgradeSolution
}
else
{
write-host -foregroundColor 'green' 'Solution not upgraded'
}
}
else
{
write-host -ForegroundColor 'red' "Solution not present on the farm"
#Adds the solution to the farm
AddSolution
#deploys the solution to the farm.
DeploySolution

}

Comments

Popular posts from this blog

Print from WPF using ReportViewer Control

Printing SSRS 2008 R2 Reports from C#.

Using IOptions class in .Net Core Unit Testing.