Download farm solution

28/04/2013 11:07

If you need to look inside wsp package from farm solution, you have to download it.

 

function Download-FarmSolution
{
 [CmdletBinding()]
 param(
  [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
  [string] $WspName,
    
  [string] $Destination,
  
  [switch] $Replace
  )
 begin {}
 process
 {
  if (!(Test-Path -LiteralPath $Destination))
  {
   $void = New-Item $Destination -ItemType 'directory'
  }
  
  $farm = Get-SPFarm
  $solution = $farm.Solutions | where {$_.Name -eq $WspName}
  
  if($solution) {
   $file = $solution.SolutionFile
   $downloadedFileName = Join-Path -Path $Destination -ChildPath $WspName
   if(Test-Path -Path $downloadedFileName)
   {
    if($Replace)
    {
     $void = Remove-Item $downloadedFileName -Confirm:$false
     $file.SaveAs($downloadedFileName)
    }
   }
   else
   {
    $file.SaveAs($downloadedFileName)
   }
   Write-Verbose ("File '{0}' was downloaded to '{1}'" -f $WspName, $Destination)
   Write-Output $downloadedFileName
  }
  
 }
 end {}
}

 

#Downloads 'myCustom.wsp' farm solution to c:/temp

Download-FarmSolution -Destination c:\temp -WspName myCustom.wsp

 

#Downloads all solutions what name match 'custom' string.to c:\temp
Get-SPSolution | where {$_.Name -match "custom"} | select -ExpandProperty Name | Download-FarmSolution -Destination c:\temp