Pages

Search This Blog

Wednesday, April 1, 2015

PowerShell script for deploying multiple WSP solutions

In case of big SharePoint Farms with large application domain we need to deploy multiple solutions, Doing it manually will be a lot tough and time consuming task. So a more manageable way could be to create a PowerShell script, that runs on the Farm to deploy the listed wsp solutions. I created one of such solution for a client who had around 50-60 such solutions. Below is the script that you can use to deploy wsp's solutions to SharePoint Farm. This script should be placed inside a folder along with the wsp solutions which needs to be deployed. 

Copy-Paste the below script in a text file and rename its extension to - *.ps1


function LoadSharePointPowerShellEnvironment
{
       write-host
       write-host "Setting up PowerShell environment for SharePoint..." -foregroundcolor Yellow
       write-host
       Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
       write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
}

function DeploySolution($packageName, $packagePath, $WebAppUrl)
{
         $spAdminServiceName = "SPAdminV4"
         #check if solution already exists
         $solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
         if ($solution -ne $null)
         {
            #Write-Host
               #Write-Warning "The solution $packageName already exists. Press Enter if you want to deploy that solution,
               #else first uninstall the solution and re-run the exe."
               #Read-Host
         #Deploy the solution
         if ($solution.Deployed)
               {
            Write-Host
               Write-Host " $packageName Already deployed... " -foregroundcolor Red
         }
         else
         {
                 if (!$solution.ContainsWebApplicationResource)
                 {
                      Write-Host "Deploying solution $packageName to the Farm..."
                      $solution | Install-SPSolution -GACDeployment -Force -Confirm:$false
                 }
                 else
                 {
                      if ($WebAppUrl -eq $null -or $WebAppUrl.Length -eq 0)
                      {
                        Write-Warning "The solution $packageName contains web application resources but no web applications were specified to deploy to."
                        return
                      }
                      else
                      {
                        Write-Host "Installing solution $packageName to WebAppUrl"
                        $solution | Install-SPSolution -GACDeployment  -WebApplication $WebAppUrl -Force -Confirm:$false
                      }
                 }

                 Write-Host
                 Write-Host "starting admin service...(this may take few minutes to complete)" -foregroundcolor Yellow
                 Stop-Service -Name $spAdminServiceName
                 Start-SPAdminJob -Verbose
                 Start-Service -Name $spAdminServiceName

                 #Block until we're sure the solution is deployed.
                 do { Start-Sleep 2 } while (!((Get-SPSolution $packageName).Deployed))
        
                 Write-Host
                 Write-Host " $packageName Deployed Sucessfully... " -foregroundcolor Green
          }
         }
         else
         {
              #Add the solution
              #Write-Host
              #Write-Host "Adding solution... $packageName..." -foregroundcolor Yellow
              #$solution = Add-SPSolution $packagePath
        Write-Host "Solution Not Found ... $packageName..." -foregroundcolor Red
         }

        
}

#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                                                                    Deploy the package
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#


LoadSharePointPowerShellEnvironment

Write-host "Deploying Solution..." -foregroundcolor Yellow

Write-Host
write-host "Enter the Web Application Url where you need to deploy the solution..."
[string]$Url = Read-Host
[string]$WebAppUrl =  $Url.Trim()

$objSite = Get-SPSite $WebAppUrl -ErrorAction SilentlyContinue
if($objSite -eq $null)
{
       Write-Host
       Write-Host "Web Application Url not correct or Web application doesnot extsts. Re-run the exe with correct url." -foregroundcolor Red
       Read-Host
}
else
{
     $dir = $(gl)
     $list = Get-ChildItem $dir | where {$_.extension -eq ".wsp"}
     $currentDir = Get-Location
     foreach($my_file in Get-ChildItem $list)
     {
        write-host $my_file.Name
           [string]$packageName = $my_file.Name
        [string]$packagePath = $currentDir.Path + "\" + $packageName
        DeploySolution $packageName $packagePath $WebAppUrl
     }
}





No comments:

Post a Comment