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 )

</span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">(</span><span style="color: rgb(0,0,255)">$solutionName</span><span style="color: rgb(0,0,0)">.EndsWith(</span><span style="color: rgb(0,0,128)">'</span><span style="color: rgb(0,0,128)">.cab</span><span style="color: rgb(0,0,128)">'</span><span style="color: rgb(0,0,0)">) </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,128,128)">or</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">$solutionName</span><span style="color: rgb(0,0,0)">.EndsWith(</span><span style="color: rgb(0,0,128)">'</span><span style="color: rgb(0,0,128)">.wsp</span><span style="color: rgb(0,0,128)">'</span><span style="color: rgb(0,0,0)">) </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,128,128)">or</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">$solutionName</span><span style="color: rgb(0,0,0)">.EndsWith(</span><span style="color: rgb(0,0,128)">'</span><span style="color: rgb(0,0,128)">.wpp</span><span style="color: rgb(0,0,128)">'</span><span style="color: rgb(0,0,0)">)))
{
    throw </span><span style="color: rgb(0,0,128)">"</span><span style="color: rgb(0,0,128)">solution name '$solutionName' should end with .cab, .wsp or .wpp</span><span style="color: rgb(0,0,128)">"</span><span style="color: rgb(0,0,0)">
}

</span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">$installed</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,128,128)">and</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">$deployed</span><span style="color: rgb(0,0,0)">)
{
    throw </span><span style="color: rgb(0,0,128)">"</span><span style="color: rgb(0,0,128)">Select either the '-installed' switch parameter or the '-deployed' switch parameter, not both at the same time</span><span style="color: rgb(0,0,128)">"</span><span style="color: rgb(0,0,0)">
}

</span><span style="color: rgb(0,0,255)">$farm</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> [Microsoft.SharePoint.Administration.SPFarm]::get_Local()
</span><span style="color: rgb(0,0,255)">$solutions</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">$farm</span><span style="color: rgb(0,0,0)">.get_Solutions()
</span><span style="color: rgb(0,0,255)">$solutioncheck</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">$false</span><span style="color: rgb(0,0,0)">
</span><span style="color: rgb(0,0,255)">foreach</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">$solution</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">in</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">$solutions</span><span style="color: rgb(0,0,0)">)
{
    </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">$solution</span><span style="color: rgb(0,0,0)">.Name </span><span style="color: rgb(0,0,255)">-ieq</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">$solutionName</span><span style="color: rgb(0,0,0)">)
    {
        </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">$deployed</span><span style="color: rgb(0,0,0)">)
        {
            </span><span style="color: rgb(0,0,255)">$solutioncheck</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">$solution</span><span style="color: rgb(0,0,0)">.Deployed
        }
        </span><span style="color: rgb(0,0,255)">else</span><span style="color: rgb(0,0,0)">
        {
            </span><span style="color: rgb(0,128,0)">#</span><span style="color: rgb(0,128,0)"> installed, is always true if we get here</span><span style="color: rgb(0,128,0)">

$solutioncheck = $true } break } } return $solutioncheck }

Test-SharePointSolution -solutionName wikidiscussionsolution.wsp -installed Test-SharePointSolution -solutionName wikidiscussionsolution.wsp -deployed

9 Comments

Comments have been disabled for this content.