Powershell: Is a SharePoint solution installed or deployed?
I was having some fun with PowerShell to talks against the SharePoint object model. I needed to know if a SharePoint solution was installed and if it was deployed. I got to the code below. Might be handy for someone to see how easy it is to get info out of SharePoint.
And the purpose I need it for? In our Macaw Solutions Factory we have a development build and a package build. Development build for SharePoint deploys everything to the bin folder of one or more web applications. Package build creates a solution package (.wsp file) that does install assemblies to the GAC. If this solutions package is deployed on the development machine, we want to detect that, because you will not see any changes after compile in a development build. The GAC assemblies have precedence over the assemblies in the web application bin folder.
So here is the code, have fun with it.
[void][reflection.assembly]::LoadWithPartialName("Microsoft.SharePoint")
function global:Test-SharePointSolution
{
param
(
$solutionName = $(throw 'Missing: solutionName'),
[switch]$installed,
[switch]$deployed
)
if (!($solutionName.EndsWith('.cab') -or $solutionName.EndsWith('.wsp') -or $solutionName.EndsWith('.wpp')))
{
throw "solution name '$solutionName' should end with .cab, .wsp or .wpp"
}
if ($installed -and $deployed)
{
throw "Select either the '-installed' switch parameter or the '-deployed' switch parameter, not both at the same time"
}
$farm = [Microsoft.SharePoint.Administration.SPFarm]::get_Local()
$solutions = $farm.get_Solutions()
$solutioncheck = $false
foreach ($solution in $solutions)
{
if ($solution.Name -ieq $solutionName)
{
if ($deployed)
{
$solutioncheck = $solution.Deployed
}
else
{
# installed, is always true if we get here
$solutioncheck = $true
}
break
}
}
return $solutioncheck
}
Test-SharePointSolution -solutionName wikidiscussionsolution.wsp -installed
Test-SharePointSolution -solutionName wikidiscussionsolution.wsp -deployed