Friday, January 1, 2010

How to test if a solution is added to the solution store with PowerShell?

To get the full list of SharePoint solution that is added to the solution store, you can run Get-SPSolution. To just get one solution you can provide Get-SPSolution with a -Identity <SPSolutionPipeBind> . SPSolutionPipeBind is the name for the solution. e.g. Get-SPSolution mySolution.wsp. But if solution is not in the solution store it will throw an exception.

PS C:\Users\andersd> Get-SPSolution mysolution.wsp
Get-SPSolution : Cannot find an SPSolution object with Id or Name: mysolution.wsp.
At line:1 char:15
+ Get-SPSolution <<<< mysolution.wsp
+ CategoryInfo : InvalidData: (Microsoft.Share...dletGetSolution:
SPCmdletGetSolution) [Get-SPSolution], SPCmdletPipeBindException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetSolution

This is not useful at all. So let’s back track a bit. We know the Get-SPSolution return a list of solution that can be piped. So if we pipe the list with Where-Object, alias Where, like is Get-SPSolution | Where { $_.Name -eq "mysolution.wsp" } we will get a solution object if it is found, if not then we get a null object that we can test for insted. The last thing is to wrap this in a function like is

function IsFarmSolutionAddToSolutionStore([string] $WSPName) {
    $WSP = Get-SPSolution | Where { 
        ($WSPName -eq $_.Name) 
    if($WSP -eq $null) { 
        return [Bool]0 
    else { 
        return [Bool]1

And we can use it like this

PS C:\Users\andersd> Write-Host "is solution added" | IsFarmSolutionAddToSolutionStore("mysolution.wsp")
is solution added

1 comment: