PowerShell Script to install MOSS 2013 solution packages

I wrote a script that will completely remove and then deploy a solution package in MOSS2013 with compatibility level set to All.  This ensures that the solution package files are installed onto both the 14 and 15 hive.

The script will wait until the solution is uninstalled before reinstalling the package.  Then, it will continuously check the deployment status, until the solution shows up as deployed.


PowerShell Script for installing MOSS 2013 Solution Package:

Add-PSSnapin 'Microsoft.SharePoint.PowerShell';
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
###################################################################################
# Function to display the solution status of a solution package repeatedly until it reaches the end state
# EndStates: NotDeployed, GlobalAndWebApplicationDeployed
####################################################################################
Function DisplaySolutionStatusUntilEndState($SolutionPackage, $endstate)
{
  $continueLoop=$TRUE;
while ($continueLoop -eq $TRUE)
{
cls
$currentDate=Get-Date;
Write-Host '*********************' $currentDate '*****************************';
Write-Host('Displaying solution status and waiting for solution status to change to');
Write-Host('    [' + $endstate + ']');
Write-Host;
$farm=[Microsoft.SharePoint.Administration.SPFarm]::Local
$bFoundSolution=$FALSE;
foreach ($solution in $farm.Solutions)
        {
        $compareresult=[string]::Compare($solution.DisplayName,$SolutionPackage, $TRUE);
       # Write-Host $compareresult + $solution.DisplayName + " vs " + $SolutionPackage;
        if ($compareresult -eq 0)
  {    
    $bFoundSolution=$TRUE;                     
                  Write-Host("**"+ $solution.DisplayName + ': ' + $solution.Status + ', ' + $solution.DeploymentState );
    if ($solution.DeploymentState -eq $endstate)
   {
      $continueLoop=$FALSE;
   }
                  else
                        {
        Start-Sleep -s 1;
   }
  }
  else
  {
   #Write-Host($solution.DisplayName + ': ' + $solution.Status + ', ' + $solution.DeploymentState );
  }

        }

 if ($bFoundSolution -eq $FALSE -and $endstate -eq 'NotDeployed')
  {
   $continueLoop=$FALSE;
  }
 Start-Sleep -s 1;

Write-Host;
}
}
Function WaitForNoDeployments()
{
  $continueLoop=$true;
  $starttime=Get-Date;
  while ($continueLoop -eq $true)
   {
     $list=(Get-SPTimerJob | ?{$_.Name.IndexOf("solution-deployment") -ne -1})
       if ($list.Count -eq 0)
        {
          $continueLoop=$false;
        }
       else
        {
     $now=Get-Date;
     $elapsed=$now-$starttime;
           Write-Output "$($list.Count) deployments are processing....Elapsed Time: $($elapsed.TotalSeconds) seconds...";
           Start-Sleep -s 2;
        }
     }
}

###########################################################################################
echo off
$SolutionPackage = 'YourSolutionPackage.wsp';
$BINFOLDER = 'C:\Program Files\Common Files\Microsoft Shared\web server extensions\15\BIN';
#$SITEURL = 'http://localhost';
#$DEPLOYURL = $SITEURL;
#**********CODE TO PROMPT FOR Solution Package Name**************
$SolutionPackage=Read-Host 'Enter in the name of the solution package'
If ($SolutionPackage -eq '') {Write-Host 'Solution Package Name Required...'; exit;}
#Write-Host $DEPLOYURL , $SITEURL
$REINSTALLSOLUTION=Read-Host 'Are you sure you want to re-install the '$SolutionPackage'? [Y] or [N]'
if ($REINSTALLSOLUTInstallION -eq 'Y' -or $REINSTALLSOLUTION -eq 'y')
{
Write-Host Uninstalling solution.
Uninstall-SPSolution -Identity $SolutionPackage -AllWebApplications -CompatibilityLevel All -Confirm:$false
$bContinue='n'
DisplaySolutionStatusUntilEndState $SolutionPackage "NotDeployed";
WaitForNoDeployments;
Write-Host Remove-SPSolution...
Remove-SPSolution -Identity $SolutionPackage -Confirm:$false
WaitForNoDeployments;
Write-Host Add-SPSolution...
Add-SPSolution -LiteralPath $pwd'\'$SolutionPackage
WaitForNoDeployments;
Write-Host Install-SPSolution...
Install-SPSolution -Identity $SolutionPackage -GACDeployment -force -CompatibilityLevel {All} -AllWebApplications
}
$bContinue='n'
DisplaySolutionStatusUntilEndState $SolutionPackage "GlobalAndWebApplicationDeployed";






Comments

Popular posts from this blog

Getting Authentication Prompt When Accessing SharePoint Web Services

How To use ASPNET_SetReg to store encrypted data in the registry and then decrypt the data for use in your app

PowerShell Script to Clean the Windows Installer Directory