Friday, July 22, 2011

Powershell script to deploy multiple solutions (.wsp) to your SharePoint 2010 web application

The Powershell script below iterates through each .wsp files in your physical folder, installs and deploys them globally into all web-applications thus automating the solution deployment process.

Copy the PowerShell scriptlet below and paste it in a notepad and save it with a .ps1 extension in any of the directories in your hard disk.

NOTE: Do not forget to place all your solution files i.e..wsp files in the same directory as your .ps1 directory.
======================================================================

Add-PsSnapin Microsoft.SharePoint.PowerShell

#Do not modify anything in the script from here onwards
function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

function Deploy-Solution{
param(
[string]$physicalPath,
[string]$name)

$SolutionName = $name
$SolutionPath = Join-Path ($physicalPath) $SolutionName
echo "Extracting information from $physicalPath"

#Admin service
$AdminServiceName = "SPAdminV4"
$IsAdminServiceWasRunning = $true;

if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
    $IsAdminServiceWasRunning = $false;
    Start-Service $AdminServiceName
   Write-Host 'SERVICE WAS STOPPED, SO IT IS NOW STARTED'
}

#Uninstall
Write-Host 'UNINSTALLING SOLUTION ...'

$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $true)}

if ($Solution -ne $null)
{
    if($Solution.ContainsWebApplicationResource)
    {
        Uninstall-SPSolution $SolutionName -AllWebApplications -Confirm:$false
    }
    else
    {
        Uninstall-SPSolution $SolutionName -Confirm:$false
    }
}

while ($Solution.JobExists)
{
    Start-Sleep 2
}

Write-Host 'SOLUTION HAS BEEN UNINSTALLED SUCCESSFULLY.'

Write-Host 'REMOVING SOLUTION ...'

if ($(Get-SPSolution | ? {$_.Name -eq $SolutionName}).Deployed -eq $false)
{
    Remove-SPSolution $SolutionName -Confirm:$false

Write-Host 'SOLUTION HAS BEEN REMOVED SUCCESSFULLY.'
}

Write-Host 'ADDING SOLUTION ...'

Add-SPSolution $SolutionPath  | Out-Null

Write-Host 'SOLUTION HAS BEEN ADDED SUCCESSFULLY.'

Write-Host 'DEPLOYING SOLUTION ...'

$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}

#use '-force' paramater to install all commands in this if statement

if(($Solution -ne $null) -and ($Solution.ContainsWebApplicationResource))
{
Install-SPSolution $SolutionName –AllwebApplications -GACDeployment -Force -Confirm:$false
}
else
{
Install-SPSolution $SolutionName -GACDeployment -Force -Confirm:$false
}

while ($Solution.Deployed -eq $false)
{
    Start-Sleep 2
}

Write-Host 'SOLUTION HAS BEEN DEPLOYED SUCCESSFULLY.'

if (-not $IsAdminServiceWasRunning)
{
    Stop-Service $AdminServiceName
}
}

#Get Current Physical Path
$currentPhysicalPath = Get-ScriptDirectory

#Iterate through all .wsp files in the current Physical Path to deploy solution
get-childitem $currentPhysicalPath -include *.wsp -recurse | foreach ($_) {Deploy-Solution $currentPhysicalPath $_.name}

#Remove SharePoint Snapin
Remove-PsSnapin Microsoft.SharePoint.PowerShell

Echo Finish

====================================================================


Automate the above .ps1 script as a batch utility, Copy and paste code below and save it with a .bat file extension, change the script file name in the highlighted yellow section below to your .ps1 name.

cd /d %~dp0
powershell -noexit -file ".AutomateDeploymentScript.ps1" "%CD%"
pause

Run the batch file and enjoy the automated deployment process:)

8 comments:

  1. Thanks,

    I'm expanding my sharepoint and powershell brains and these code examples help a lot,

    Chris

    ReplyDelete
  2. Very good information solved my purpose

    Thanks,
    Srinivas

    ReplyDelete
  3. I tried running this on my server and it says the ps1 script needs to be digitally signed. Any quick ways to get it signed?

    Thanks

    ReplyDelete
    Replies
    1. Nevermind. I think it's easier to Set-ExecutionPolicy to RemoteSigned or Unrestrcited. Then set it back to what it originally was after running the script.

      Cheers

      Delete
  4. Thanks for this useful information....

    ReplyDelete
    Replies
    1. Can you provide solution to uninstall and remove solution from Sharepoint sites !!!
      Thank in advance

      Delete
  5. Can u share if the solution contains multiple features in it

    ReplyDelete
  6. This is amazing! I couldn't get the automated bat file to work, but the PSScript works flawlessly. Thank you so much for sharing this script...AWESOME!

    ReplyDelete