Deploy and Upgrade SharePoint solution with PowerShell script

/*----------- Package information in XML file : package.xml -------------------*/
<?xml version="1.0" encoding="utf-8"?>
<project>
<packages>
<package name="SharePoint.Communities.BrandingMySite.wspliteralPath="./SharePoint.Communities.BrandingMySite.wsp"
deploysTo="global"/>
</packages>
</project>

/*------------------- Deploy Solution : deployMySite.ps1 ----------------------*/
function Provision-InitialStructure([string]$configFilePath)
{
   #### Get SharePoint PowerShell ####

   $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
Write-Host "`nLoading SharePoint Powershell Snapin`n"
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}


Write-Host "****************************************************************"
Write-Host "*                                                              *"
Write-Host "* Deploying My Site Solution - Globally                        *"
Write-Host "*                                                              *"
Write-Host "****************************************************************"


write-host "`nParsing config file`n";


[System.Xml.XmlDocument] $xd = new-object System.Xml.XmlDocument;
$file = resolve-path($configFilePath);
$xd.load($file);


###### ADD SOLUTION PACKAGES TO SHAREPOINT SOLUTION STORE ##########


$packages = $xd.selectnodes("project/packages/package");


foreach ($package in $packages)
{
$pkg_name = $package.getAttribute("name");
$pkg_literalpath = $package.getAttribute("literalPath");
$pkg_deploysto = $package.getAttribute("deploysTo");


$packageexists = (Get-SPSolution $pkg_name -ErrorAction SilentlyContinue) -ne $null;

if ($packageexists)
{
Write-Host -ForegroundColor Red "Solution already exist in solution store.";
return;
}
else
{
Write-Host "Adding solution package $pkg_name to Solution Store ..."
Add-SPSolution -LiteralPath $pkg_literalpath
Write-Host "`nDone. Deploying solution package ...`n"
Install-SPSolution -Identity $pkg_name -GACDeployment –force
}
}
Start-Sleep -Seconds 10;

###### END ADD SOLUTION PACKAGES TO SHAREPOINT SOLUTION STORE ##########
} #end function


# call the function
Provision-InitialStructure ./package.xml
Start-Sleep -Seconds 40;

/*------------------------ Upgrade Solution --------------------------*/

if ($packageexists)
{
Write-Host "Updating solution package $pkg_name to Solution Store ..."
Update-SPSolution -Identity $pkg_name -LiteralPath $pkg_literalpath -GACDeployment - ErrorAction Inquire;
}
else
{
Write-Host -ForegroundColor Red "Solution doesn't exist in solution store.";
return;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s