Powershell Script that will deploy/upgrade Sandbox Solution.

Script that will let you deploy/upgrade a sandbox solution to a SharePoint 2010 site. Make sure that you at least an SharePoint site administrator before you run the script.


# SharePoint DLL


[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")



#Cmdlet Install

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue



###REGION FUNCTIONS



#####################################################################

#Adds a Sandbox solution to a site.

#####################################################################

function AddSolution()

{

$myError = $null



Add-SPUserSolution -LiteralPath $Solution -Site $SiteURL -ErrorAction:SilentlyContinue -ErrorVariable myError -Confirm:$false



if ($myError -ne $null)

{

write-host -foregroundcolor 'red' $myError



$myError = $null

}

else

{

Write-host -foregroundColor 'green' 'Sucessfuly added the solution to the site'

}

}



#####################################################################

#Adds a Sandbox solution to a site.

#####################################################################

function InstallSolution()

{

$myError = $null

Install-SPUserSolution -Identity $SolutionName -Site $SiteURL -ErrorAction:SilentlyContinue -ErrorVariable myError





if ($myError -ne $null)

{

write-host -foregroundcolor 'red' $myError



$myError = $null

}

else

{

Write-host -foregroundColor 'green' 'Sucessfuly activated the solution to the site'

}

}



function UninstallSolution()

{

#Add + Upgrade solution

$myError = $null

#Add-SPUserSolution -LiteralPath $solFile -Site $site -ErrorAction:SilentlyContinue -ErrorVariable myError -Confirm:$false



Uninstall-SPUserSolution -Identity $SolutionName -Site $SiteURL -ErrorAction:SilentlyContinue -ErrorVariable myError -Confirm:$false



if ($myError -ne $null)

{

write-host -foregroundcolor 'red' $myError



$myError = $null

}



$myError = $null



Remove-SPUserSolution -Identity $SolutionName -Site $SiteURL -ErrorAction:SilentlyContinue -ErrorVariable myError -Confirm:$false



if ($myError -ne $null)

{

write-host -foregroundcolor 'red' $myError



$myError = $null

}

else

{

Write-host -foregroundColor 'green' 'Sucessfuly removed the solution from the site'

}



}



##ENDREGION FUNCTIONS



$SiteURL = ""

$SolutionPath = ""

$SolutionName = ""

$solution = ""



##Get the Site URL to where the sandbox solution is to be deployed

write-host -ForegroundColor 'black' "`nEnter the URL to the site collection where the sandbox solution is to be deployed. (example: http://cijs-spap-sv01:48742)"

$SiteURL = Read-Host



##debug line below, comment later on

#$MessageLine = [System.String]::Concat("Site URL is: ", $SiteURL)

#write-host $MessageLine



##Get the location of the wsp for the solution

write-host -foregroundcolor 'black' "`nEnter the location of the WSP file (example: c:\deployment\sandboxsolutions)"

$SolutionPath = read-host



##Get the solution name

write-host -foregoundColor 'black' "`nEnter the solution name (example: myfirstsolution.wsp)"

$SolutionName = read-host



$Solution = [System.String]::Concat($SolutionPath, $SolutionName)



$myError = $null

##check to see if the solution is already present

$addedSolution = Get-SPUserSolution -Identity $SolutionName -Site $SiteURL -ErrorAction:SilentlyContinue -ErrorVariable myError



if ($MyError -eq $null)

{

write-host -foregroundcolor 'yellow' $myError

}



if ($addedSolution -ne $null)

{

write-host -foregroundcolor "red" "Solution already present in the site: " $SiteURL



$upgradeSolution = ""

write-host -foregroundcolor 'black' "`n Upgrade Solution (y/n)"

$upgradeSolution = read-host



if ($upgradeSolution -eq 'y')

{

#uninstal the solution

UninstallSolution



#add the solution

AddSolution



#activate the solution

InstallSolution

}

else

{

write-host -foregroundColor 'green' 'Solution not upgraded'

}



}

else

{



AddSolution

#write-host -ForegroundColor 'green' "`n Sucessfully added the solution to the site: " + $SiteURL



InstallSolution

#write-host -foregroundColor 'green' '`n Sucessfully activated the solution to the site: ' + $SiteURL



}

Comments

Popular posts from this blog

Print from WPF using ReportViewer Control

Printing SSRS 2008 R2 Reports from C#.

Using IOptions class in .Net Core Unit Testing.