About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Wednesday 19 December 2012

PowerShell script to deploy Multiple Sandbox solutions

Applies to SPFoundation 2010, SP 2010, SPFoundation 2013,SP 2013

Points to be noted:
1. Replace $siteURL with url
2. All wsps should be in the current directory of the script (.ps1)

Add-PsSnapin Microsoft.SharePoint.PowerShell
$logfile=$CurrentDir + "\log.log"
$siteURL = "
xxxxxxx"
Start-Transcript $logfile
#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"

$Solution = Get-SPUserSolution -site $siteURL | Where { ($SolutionName -eq $_.Name) }  -ErrorAction SilentlyContinue
if ($Solution -ne $null)
{
    if($Solution.Status -eq "Activated")
    {
    Write-Host 'Poweshell Script will now retract solution:' $SolutionName
    Uninstall-SPUserSolution -Identity $SolutionName -Site $SiteURL -Confirm:$false
    Write-Host 'Successfully retracted solution:' $SolutionName
    }
    Write-Host 'Removing Solution:' $SolutionName
    Remove-SPUserSolution -Identity $SolutionName -Site $siteURL -Confirm:$false
    Write-Host 'Successfully removed Solution:' $SolutionName
}

Write-Host 'Poweshell Script will now add solution:' $SolutionName
Add-SPUserSolution –LiteralPath $SolutionPath -Site $SiteURL
Write-Host 'Poweshell Script will now deploy solution:' $SolutionName
Install-SPUserSolution -Identity $SolutionName -Site $SiteURL
Write-Host 'Poweshell Script has finished deployment of solution:' $SolutionName
}

#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}

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


Echo Finish

No comments:

Post a Comment