Monday, June 10, 2013

Add Solutions using Powershell script

Hi,

There might be a requirement of adding solutions from Central Admin in bulk.
For this we have written a script as below for the same and log file will get generated to check the status.
Here Add Solutions script folder,0.Packages folder are on same drive where all wsps are placed in the 0.Packages
That’s why 0.Packages folder is provided as input in the batch file.

// AddDeploySolution.bat
cd /d %~dp0
@echo off
powershell.exe -noexit .\PSAddDeploySolution.ps1  ..\0.Packages\ .\AddDeploySolution.csv DeploySolution

// AddDeploySolution.csv
Name,DeploymentScope,Url,Event
Solutionname1.wsp, farm,,
Solutionname2.wsp, farm,,
Solutionname3.wsp, farm,,

// PSAddDeploySolution.ps1
#///<summary>
#/* Script Name                               : PSAddDeploySolution
# * Purpose                                        : Used to automate solution deployment process
#///</summary>

#-----Input parameters to the script
param($wspPath,$inputFile,$depType)

#------To check if Sharepoint cmdlets are registered are registered
if ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
                Add-PSSnapin Microsoft.SharePoint.Powershell 
}
#---------------------------------------------------------------------------------------------------

#-----------------------------------------Start Logging---------------------------------------------
$filepath = $MyInvocation.MyCommand.Definition                                       
$directorypath = [System.IO.Path]::GetDirectoryName($filepath)
$LogTime = Get-Date -Format yyyy-MM-dd_h-mm
$LogFile = $directorypath + "\AddDeploySolution_Log_$LogTime.txt"
Start-Transcript -Path $LogFile -Force
#---------------------------------------------------------------------------------------------------

#---------------------------------------------------------------------------------------------------
#-----Function to wait for job to finish----
function WaitForJobToFinish{
param($strSolutionName)
    $JobName = "*solution-deployment*$SolutionFileName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null)
    {
        write-host 'Timer job not found'
    }
    else
    {
        $JobFullName = $job.Name
        write-host -NoNewLine "Waiting to finish job $JobFullName"
        
        while ((Get-SPTimerJob $JobFullName) -ne $null)
        {
            write-host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        write-host  "Finished waiting for job.."
    }
}
#---------------------------------------------------------------------------------------------------
#-----Function to add and deploy solution--#

function AddDeploySolution {                   
param($strSolutionName,$strSolutionPath,$strDeploymentScope,$webAppUrl)
#------Adding the solution
write-host "**** Adding Solution "$strSolutionName"..."
Add-SPSolution $strSolutionPath
write-host "**** Solution Added Successfully****"
#------Deploying the solution
write-host "**** Installing Solution "$strSolutionName"..."
if($strDeploymentScope -eq "Farm")
{
  Install-SPSolution -Identity $strSolutionName -GACDeployment -confirm:$false -force
}
else
{
  Install-SPSolution -Identity $strSolutionName –webApplication $webAppUrl -GACDeployment -confirm:$false -force
}
write-host 'Waiting for job to finish'
 WaitForJobToFinish
write-host "**** Deployment Successful****"
}
#---------------------------------------------------------------------------------------------------
function UninstallSolution{                         
param($strSolutionName,$strSolutionPath,$strDeploymentScope,$webAppUrl)
write-host "****Uninstalling" $strSolutionName "****:"
if($strDeploymentScope -eq "Farm")
{
  Uninstall-SPSolution –Identity $strSolutionName  -confirm:$false
}
else
{
  Uninstall-SPSolution –Identity $strSolutionName –webApplication $webAppUrl -confirm:$false
}
write-host 'Waiting for job to finish'
 WaitForJobToFinish $strSolutionName
 write-host "****"$strSolutionName "Uninstalled****"
write-host "****Removing Solution "$strSolutionName"****:"
Remove-SPSolution -Identity $strSolutionName -confirm:$false
write-host "**** Solution" $strSolutionName "Removed Successfully****:"
}

#-----------------------------------------------------------------------------------
#loop through a csv file
Import-Csv $inputFile | ForEach{                             
   
                $strSolutionName = $_.Name
                $strDeploymentScope = $_.DeploymentScope
                $url= $_.URL
                $event=$_.Event
                $strSolutionPath = $wspPath +"\" + $strSolutionName                                  
                switch ($depType)
                {
                                DeploySolution {AddDeploySolution $strSolutionName $strSolutionPath $strDeploymentScope $url}
                                UninstallSolution {UninstallSolution $strSolutionName $strSolutionPath $strDeploymentScope $url}
                }
}
#---------------------------------------------------------------------------------------------------

#-----------------------------------------End Logging-----------------------------------------------
Stop-Transcript
#---------------------------------------------------------------------------------------------------


No comments:

Post a Comment