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

PowerShell script to deploy Sandbox solution

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

In general we will write a script to deploy farm solutions. But we will upload sandbox solutions into site collection solution gallery. What if, we want to automate the process to deploy sandbox solution without uploading.... here is the script

Points to be noted:
1. The .wsp should be in the current directory of the script (.ps1)
2. Replace $solutionName with your solution name.
3. Replace $SiteURL with your site collection url.

Add-PsSnapin Microsoft.SharePoint.PowerShell
Write-Host 'Poweshell Script will initialize parameters'
$CurrentDir=$args[0]
$solutionName="xxxx.wsp"
$SolutionPath=$CurrentDir + "\"+$solutionName
$logfile=$CurrentDir + "\log.log"
$SiteURL = "xxxxxxx"

Start-Transcript $logfile
$errorActionPreference = 'Inquire'

$solution = Get-SPUserSolution -site $siteURL | Where { ($solutionName -eq $_.Name) }  -ErrorAction SilentlyContinue 
try
{
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
}

catch [system.Exception]
{
Write-Error 'An Unknown error occured while trying to deploy/redeploying the solution:' $_.Exception.Message
}


Stop-Transcript
Remove-PsSnapin Microsoft.SharePoint.PowerShell

Happy powershell programming......